Skip to content

Instantly share code, notes, and snippets.

@jacks205
Created March 18, 2015 09:41
Show Gist options
  • Save jacks205/3eab86d7ed97c557c97e to your computer and use it in GitHub Desktop.
Save jacks205/3eab86d7ed97c557c97e to your computer and use it in GitHub Desktop.
Bluetooth LE
//
// ViewController.swift
// BluetoothExample
//
// Created by Mark Jackson on 3/18/15.
// Copyright (c) 2015 Mark Jackson. All rights reserved.
//
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate {
required init(coder aDecoder: NSCoder) {
self.myCentralManager = CBCentralManager()
super.init(coder: aDecoder)
}
var myCentralManager : CBCentralManager
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myCentralManager = CBCentralManager(delegate: self, queue: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*!
* @method centralManagerDidUpdateState:
*
* @param central The central manager whose state has changed.
*
* @discussion Invoked whenever the central manager's state has been updated. Commands should only be issued when the state is
* <code>CBCentralManagerStatePoweredOn</code>. A state below <code>CBCentralManagerStatePoweredOn</code>
* implies that scanning has stopped and any connected peripherals have been disconnected. If the state moves below
* <code>CBCentralManagerStatePoweredOff</code>, all <code>CBPeripheral</code> objects obtained from this central
* manager become invalid and must be retrieved or discovered again.
*
* @see state
*
*/
func centralManagerDidUpdateState(central: CBCentralManager!){
println(central.state)
myCentralManager.scanForPeripheralsWithServices(nil, options: nil)
println("called scanForPeripheralsWithServices")
}
/*!
* @method centralManager:willRestoreState:
*
* @param central The central manager providing this information.
* @param dict A dictionary containing information about <i>central</i> that was preserved by the system at the time the app was terminated.
*
* @discussion For apps that opt-in to state preservation and restoration, this is the first method invoked when your app is relaunched into
* the background to complete some Bluetooth-related task. Use this method to synchronize your app's state with the state of the
* Bluetooth system.
*
* @seealso CBCentralManagerRestoredStatePeripheralsKey;
* @seealso CBCentralManagerRestoredStateScanServicesKey;
* @seealso CBCentralManagerRestoredStateScanOptionsKey;
*
*/
func centralManager(central: CBCentralManager!, willRestoreState dict: [NSObject : AnyObject]!){
}
/*!
* @method centralManager:didRetrievePeripherals:
*
* @param central The central manager providing this information.
* @param peripherals A list of <code>CBPeripheral</code> objects.
*
* @discussion This method returns the result of a {@link retrievePeripherals} call, with the peripheral(s) that the central manager was
* able to match to the provided UUID(s).
*
*/
func centralManager(central: CBCentralManager!, didRetrievePeripherals peripherals: [AnyObject]!){
}
/*!
* @method centralManager:didRetrieveConnectedPeripherals:
*
* @param central The central manager providing this information.
* @param peripherals A list of <code>CBPeripheral</code> objects representing all peripherals currently connected to the system.
*
* @discussion This method returns the result of a {@link retrieveConnectedPeripherals} call.
*
*/
func centralManager(central: CBCentralManager!, didRetrieveConnectedPeripherals peripherals: [AnyObject]!){
}
/*!
* @method centralManager:didDiscoverPeripheral:advertisementData:RSSI:
*
* @param central The central manager providing this update.
* @param peripheral A <code>CBPeripheral</code> object.
* @param advertisementData A dictionary containing any advertisement and scan response data.
* @param RSSI The current RSSI of <i>peripheral</i>, in dBm. A value of <code>127</code> is reserved and indicates the RSSI
* was not available.
*
* @discussion This method is invoked while scanning, upon the discovery of <i>peripheral</i> by <i>central</i>. A discovered peripheral must
* be retained in order to use it; otherwise, it is assumed to not be of interest and will be cleaned up by the central manager. For
* a list of <i>advertisementData</i> keys, see {@link CBAdvertisementDataLocalNameKey} and other similar constants.
*
* @seealso CBAdvertisementData.h
*
*/
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!){
println(peripheral.name)
}
/*!
* @method centralManager:didConnectPeripheral:
*
* @param central The central manager providing this information.
* @param peripheral The <code>CBPeripheral</code> that has connected.
*
* @discussion This method is invoked when a connection initiated by {@link connectPeripheral:options:} has succeeded.
*
*/
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!){
}
/*!
* @method centralManager:didFailToConnectPeripheral:error:
*
* @param central The central manager providing this information.
* @param peripheral The <code>CBPeripheral</code> that has failed to connect.
* @param error The cause of the failure.
*
* @discussion This method is invoked when a connection initiated by {@link connectPeripheral:options:} has failed to complete. As connection attempts do not
* timeout, the failure of a connection is atypical and usually indicative of a transient issue.
*
*/
func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!){
}
/*!
* @method centralManager:didDisconnectPeripheral:error:
*
* @param central The central manager providing this information.
* @param peripheral The <code>CBPeripheral</code> that has disconnected.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method is invoked upon the disconnection of a peripheral that was connected by {@link connectPeripheral:options:}. If the disconnection
* was not initiated by {@link cancelPeripheralConnection}, the cause will be detailed in the <i>error</i> parameter. Once this method has been
* called, no more methods will be invoked on <i>peripheral</i>'s <code>CBPeripheralDelegate</code>.
*
*/
func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment