Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bluetooth Low Energy

Bluetooth Low Energy

Talk given at CocoaHeads Strasbourg.

Frédéric Maquin

February 09, 2017
Tweet

More Decks by Frédéric Maquin

Other Decks in Programming

Transcript

  1. Bluetooth 2.0+ Classic Original iPhone (2007) RelaFvely high bandwidth RelaFvely

    high power consumpFon ⚠ Require MFi cer9fica9on 3
  2. Bluetooth 4.0+ Low energy iPhone 4s (2011) Handle a small

    amount of data Low power consumpFon Available with Core Bluetooth 4 “Always off”
  3. Hello, I’m a super cool device, can you see me?

    I want to connect! Adver9ses PERIPHERAL 8
  4. Hello, I’m a super cool device, can you see me?

    I want to connect! Adver9ses PERIPHERAL Listens Is anyone around? CENTRAL 8
  5. Ini9alize 9 import CoreBluetooth class BluetoothDiscoveryManager: NSObject, CBCentralManagerDelegate { init(peripheralPairingDelegate:

    PeripheralPairingDelegation) { centralManager = CBCentralManager(delegate: self, queue: nil) } }
  6. Scan // MARK: - CBCentralManagerDelegate func centralManager(_ central: CBCentralManager, didDiscover

    peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) centralManager.scanForPeripherals(withServices: nil, options: nil) centralManager.stopScan() 1 2 3 10
  7. Adver9se SERVICE 128f47c1-1fee-4c42-9d93-edd0b88f3291 CHARACTERISTIC fd31fe71-b944-4248-aa04-aef43fc69add CHARACTERISTIC fd31fe72-b944-4248-aa04-aef43fc69add CHARACTERISTIC fd31fe73-b944-4248-aa04-aef43fc69add SERVICE

    128f47c2-1fee-4c42-9d93-edd0b88f3291 CHARACTERISTIC fd31fe74-b944-4248-aa04-aef43fc69add CHARACTERISTIC fd31fe75-b944-4248-aa04-aef43fc69add Characteris7cs and services are idenFfied with UUIDs. 14 GATT SUMMARY
  8. CharacterisFcs are a]ributes which can be read and wriBen to.

    AddiFonally, peripherals can also no7fy the central when an a]ribute changes. 15 GATT SUMMARY
  9. Some characterisFcs are predefined in the BLE Standard. Data is

    exchanged through frames which must be smaller or equal to 20 bytes. GATT SUMMARY 16
  10. 00002A08-0000-1000-8000-00805F9B34FB Date Time (0x2A08) org.bluetooth.characterisFc.date_Fme 128bits UUID equivalent: Byte 0

    1 2 3 4 5 6 Descrip9on Year Month Day Hours Minutes Seconds Minimum 1582 1 1 0 0 0 Maximum 9999 12 31 23 59 59 GATT SUMMARY 17
  11. ProTip™ Everyone is usually expecFng liBle-endian. Be nice, use li]le-endian

    for words longer than 8 bits! ⚠ UInt16 here GATT SUMMARY 18 Byte 0 1 2 3 4 5 6 Descrip9on Year Month Day Hours Minutes Seconds Minimum 1582 1 1 0 0 0 Maximum 9999 12 31 23 59 59
  12. ProTip™ CBUUID automagically converts 16-bits/32-bits UUIDs to 128-bits UUIDs. let

    uuid32 = CBUUID(string: "2a08") let uuid128 = CBUUID(string: "00002a08-0000-1000-8000-00805f9b34fb") uuid32 == uuid128 // true GATT SUMMARY 19
  13. Discover Before wriFng and reading characterisFcs, you need to discover

    them on the peripheral. (Even if you own the hardware and already know which characterisFcs are available.) GATT DISCOVERY 20
  14. Discover Services class BluetoothPeripheralManager: NSObject, CBPeripheralDelegate { } peripheral.delegate =

    self // BluetoothPeripheralManager peripheral.discoverServices(nil) // MARK: - CBPeripheralDelegate func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) 1 2 3 GATT DISCOVERY 21
  15. Discover Characteris9cs // MARK: - CBPeripheralDelegate func peripheral(_ peripheral: CBPeripheral,

    didDiscoverServices error: Error?) { If let error = error { fatalError(error.localizedDescription) } guard let services = peripheral.services else { fatalError("") } for service in services { peripheral.discoverCharacteristics(nil, for: service) } } 1 2 // MARK: - CBPeripheralDelegate peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) GATT DISCOVERY 22
  16. When scanning, or discovering, you can always filter specific UUIDs.

    let serviceUUID = CBUUID(string: "128f47ce-1fee-4c42-9d93-edd0b88f3291") let characteristicUUID = CBUUID(string: "fd31fe78-b944-4248-aa04-aef43fc69add") central.scanForPeripherals(withServices: [serviceUUID], options: nil) peripheral.discoverServices([serviceUUID]) peripheral.discoverCharacteristics([serviceUUID], for: service) 23 ProTip™ GATT DISCOVERY
  17. peripheral.writeValue(value, for: characteristic, type: .withResponse) peripheral.readValue(for: characteristic) 1 2 25

    Read/Write READ/WRITE CHARACTERISTICS func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) characteristic.value // Data
  18. 26 No9fica9ons NOTIFIED CHARACTERISTICS To limit baBery drain, it’s be]er

    to wait for the peripheral to no7fy a change, rather than periodically read a characterisFc.
  19. 27 No9fica9ons NOTIFIED CHARACTERISTICS peripheral.setNotifyValue(true, for: characteristic) // Enable notification

    Requested reads and noFfied changes are both reported in peripheral(_:didUpdateValueFor:error:)
  20. Broadcast 28 No9fica9ons NOTIFIED CHARACTERISTICS Not all characterisFcs support noFficaFons.

    It’s up to the hardware to decide which characterisFcs can be no7fied.
  21. Broadcast 30 Security SECURITY The process of defining a secure,

    encrypted communicaFon channel is called bonding. Bonding is also oken referred as pairing.
  22. Broadcast 31 Security SECURITY There are four types of pairing

    methods: Just Works Passkey Numeric comparison Out-of-Band Core Bluetooth supports only two pairing methods.
  23. Broadcast 31 Security SECURITY There are four types of pairing

    methods: Just Works Passkey Numeric comparison Out-of-Band Core Bluetooth supports only two pairing methods.
  24. Background Modes 37 Virtually everything can be done in the

    background. BACKGROUND MODES Scanning ConnecFng Reading WriFng Discovering
  25. Background Modes 38 iOS will also wake the process when

    a no7fica7on is sent. BACKGROUND MODES
  26. iOS supports state preserva7on and restora7on, in case your app

    is killed and later relaunched in the background. 40 Apple also provides many useful tools to debug bluetooth exchanges. (But that’s beyond the scope of this talk.)