Program the Internet of Things with Swift for iOS by Ahmed Bakir

Program the Internet of Things with Swift for iOS by Ahmed Bakir

Author:Ahmed Bakir
Language: eng
Format: epub
ISBN: 9781484235133
Publisher: Apress


Listing 6-17Discovering and Saving a Bluetooth LE Peripheral

While it would be ideal to start using the peripheral after finding it, there is still a bit more effort required to retrieve data from the device. Once the connection is established, the centralManager(didConnect:) method will fire. Inside this method, you must begin scanning for the services you want to use on the peripheral (door and battery status). After you have connected to the IOTHome peripheral, you no longer have to scan for devices, so you should tell the Central Manager to stop scanning and update the status of the connection status property for the BluetoothService class.

Similar to the CBCentralManager, the CBPeripheral sends its updates back through delegate methods. In Listing 6-18, I have updated the BluetoothService class to include the completed centralManager(didConnect:) method and a stub for the CBPeripheralDelegate protocol.extension BluetoothService: CBCentralManagerDelegate {

func centralManagerDidUpdateState(_ central:

CBCentralManager) {

switch(central.state) {

case .poweredOn:

NSLog("It's showtime")

default:

NSLog("Device is not ready")

}

}

...



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.