Bluetooth Low Energy

Bluetooth Low Energy

Talk given at CocoaHeads Strasbourg.

07cace5d941b67f22d030088283a10f0?s=128

Frédéric Maquin

February 09, 2017
Tweet

Transcript

  1. CocoaHeads Strasbourg Frédéric Maquin @ephread February 9th, 2017

  2. CLASSIC LOW ENERGY VS. 2

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

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

    amount of data Low power consumpFon Available with Core Bluetooth 4 “Always off”
  5. There is no way around BLE in the current IoT/wearable

    age. 5
  6. Generic Access Profile Define how devices can be discovered and

    connected together. 6
  7. Devices can act as Central or Peripheral. 7

  8. Devices can act as Central or Peripheral. iDevices will most

    of Fme act as Centrals. 7
  9. Hello, I’m a super cool device, can you see me?

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

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

    PeripheralPairingDelegation) { centralManager = CBCentralManager(delegate: self, queue: nil) } }
  12. 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
  13. Connect centralManager.connect(peripheral, options: nil) // MARK: - CBCentralManagerDelegate func centralManager(_

    central: CBCentralManager, didConnect peripheral: CBPeripheral) 1 2 11
  14. Generic ATTribute profile Define how devices can exchange data once

    they are connected together. 12
  15. Peripherals define characteris7cs, grouped together in services. 13 GATT SUMMARY

  16. 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
  17. CharacterisFcs are a]ributes which can be read and wriBen to.

    15 GATT SUMMARY
  18. 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
  19. Data is exchanged through frames which must be smaller or

    equal to 20 bytes. GATT SUMMARY 16
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. Read/Write Values are read or wriBen using the Data struct.

    READ/WRITE CHARACTERISTICS 24
  29. 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
  30. 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.
  31. 27 No9fica9ons NOTIFIED CHARACTERISTICS peripheral.setNotifyValue(true, for: characteristic) // Enable notification

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

    It’s up to the hardware to decide which characterisFcs can be no7fied.
  33. Security InteracFng with certain characteris7cs may require a secured communicaFon

    channel. 29
  34. Broadcast 30 Security SECURITY The process of defining a secure,

    encrypted communicaFon channel is called bonding. Bonding is also oken referred as pairing.
  35. 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.
  36. 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.
  37. Broadcast 32 Security SECURITY iOS handles the pairing automa7cally, when

    required. Just Works Passkey
  38. The WATCH uses out-of-bound pairing. ProTrivia™ SECURITY 33

  39. Background Modes In prac7ce, a lot of the communicaFon will

    happen in the background. 34
  40. Background Modes 35 BACKGROUND MODES

  41. Background Modes 36 That’s it. BACKGROUND MODES

  42. Background Modes 37 Virtually everything can be done in the

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

    a no7fica7on is sent. BACKGROUND MODES
  44. Background Modes 39 Good Guy iOS makes our lives easier!

    BACKGROUND MODES
  45. 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.)
  46. Ques9ons? 41