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

BTLE (Bluetooth Low Energy) and Core Bluetooth

Zach Dennis
December 04, 2013

BTLE (Bluetooth Low Energy) and Core Bluetooth

This presentation is on Bluetooth low energy and how Apple's Core Bluetooth framework works to expose the higher level application layer to iOS and Mac developers.

Zach Dennis

December 04, 2013
Tweet

More Decks by Zach Dennis

Other Decks in Technology

Transcript

  1. Bluetooth LE,
    CoreBluetooth
    @zachdennis
    @zdennis
    !
    mutuallyhuman.com

    View Slide

  2. Bluetooth LE

    View Slide

  3. What is BTLE?

    View Slide

  4. A short range, ultra-low
    power consuming
    wireless technology.

    View Slide

  5. Shares the “Bluetooth”
    name, but has different
    design goals in mind.

    View Slide

  6. Power
    Consumption
    Years, not hours or
    days.

    View Slide

  7. Short range
    ~ 50m

    View Slide

  8. Packet-based
    Short bursts of data.

    View Slide

  9. Intervals
    Ad Ad Ad
    Data Data

    View Slide

  10. Multiple channels
    Ad Ad Ad
    Data Data
    Ad Ad Ad
    Data Data
    Ad Ad Ad
    Data Data
    #1
    #2
    #3

    View Slide

  11. Device A is looking.
    Ad Ad
    Data Data
    Ad Ad
    Data Data
    Scan
    Ad
    Ad
    Scan Data
    Device B is advertising.

    View Slide

  12. Frequency hopping

    View Slide

  13. Connection-less
    Devices do not need to maintain
    connections.

    View Slide

  14. Pairing
    e.g. iOS - happens when Insufficient
    Authentication error code is sent from the
    peripheral.

    View Slide

  15. Security
    Devices pair, keys are distributed, and the
    connection is encrypted.
    !
    Encryption is AES-128.

    View Slide

  16. View Slide

  17. Why BTLE?

    View Slide

  18. *Borrowed from 2012 WWDC CoreBluetooth Talk

    View Slide

  19. 1 billion+ devices

    View Slide

  20. *Borrowed from 2013 Apple WWDC talk

    View Slide

  21. How BTLE Works

    View Slide

  22. The Stack

    View Slide

  23. Key Terms
    • Central
    • Peripheral
    • Service
    • Characteristic
    • Descriptor

    View Slide

  24. Central Peripheral
    (has data)
    (wants data)

    View Slide

  25. Central Peripheral
    advertising

    View Slide

  26. Central Peripheral
    advertising
    scan

    View Slide

  27. Central Peripheral
    advertising
    advertising
    scan

    View Slide

  28. Central Peripheral
    advertising
    scan advertising
    discover services Heart Monitor

    View Slide

  29. Central Peripheral
    advertising
    scan advertising
    discover services
    discover characteristics
    Heart Monitor
    BPM

    View Slide

  30. Central Peripheral
    advertising
    scan advertising
    discover services
    discover characteristics
    Heart Monitor
    BPM
    read value 95

    View Slide

  31. Central Peripheral
    advertising
    scan advertising
    discover services
    discover characteristics
    Heart Monitor
    BPM
    read value 95
    observe value BPM 95

    View Slide

  32. Central Peripheral
    advertising
    scan advertising
    discover services
    discover characteristics
    Heart Monitor
    BPM
    read value 95
    observe value BPM 95
    BPM 95
    BPM 98
    value changed

    View Slide

  33. Service
    A service is a human-readable specification of a
    set of characteristics and their associated
    behavior.

    View Slide

  34. Two kinds of services
    There are primary services and secondary
    services.

    View Slide

  35. Nested services
    Services can contain other services.

    View Slide

  36. Characteristic
    A characteristic is a bit of data that has a known
    format labeled with a UUID.
    They are intended for computer-readable format
    as opposed to human-readable text.

    View Slide

  37. Profiles
    A profile is a specification that describe two or
    more devices, with one or more services on each
    device, how they discover each other, connect,
    and otherwise interact.
    Profiles define roles for devices to play.

    View Slide

  38. Heart Rate Profile
    Collector Heart Rate Sensor
    Heart Rate Service
    Device Information Service

    View Slide

  39. Heart Rate Profile
    Collector Heart Rate Sensor
    Heart Rate Service
    Device Information Service
    GATT CLIENT GATT SERVER

    View Slide

  40. Profiles & Services
    Profiles contain services.
    Services can be contained by multiple profiles.

    View Slide

  41. Peripheral
    Characteristic
    Characteristic
    Service
    Characteristic
    Anatomy of a Peripheral
    Descriptor
    Service

    View Slide

  42. Apple’s Approach to
    BTLE

    View Slide

  43. Simple
    Powerful

    View Slide

  44. Technology Stack

    View Slide

  45. *Borrowed from 2012 WWDC CoreBluetooth Talk

    View Slide

  46. Supported Profiles
    • Generic Attribute Profile Service
    • Generic Access Profile Service
    • Bluetooth Low Energy HID Service
    • Battery Service
    • Time Service
    • Apple Notification Center Service

    View Slide

  47. CoreBluetooth

    View Slide

  48. Object Model

    View Slide

  49. CBCharacteristic
    CBService
    CBCentral
    CBMutableCharacteristic
    CBMutableService
    Main Objects
    Data Objects
    Helper Objects
    CBUUID CBATTRequest
    CBCentralManagerDelegate CBPeripheralManagerDelegate
    CBCentralManager CBPeripheralManager
    CBPeripheralDelegate
    CBPeripheral

    View Slide

  50. Being a Central

    View Slide

  51. Central Peripheral

    View Slide

  52. CBCentralManager
    Scanning!
    – scanForPeripheralsWithServices:options
    !
    Stop scanning!
    – stopScan
    !
    Connecting to peripherals!
    – connectPeripheral:options
    – cancelPeripheralConnection:
    !
    Retrieving known peripherals!
    – retrieveConnectedPeripheralsWithServices:
    – retrievePeripheralsWithIdentifiers:

    View Slide

  53. CBCentralManagerDelegate
    Monitoring Connections with Peripherals!
    – centralManager:didConnectPeripheral:
    – centralManager:didDisconnectPeripheral:error:
    – centralManager:didFailToConnectPeripheral:error:
    !
    Discovering and Retrieving Peripherals!
    – centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    – centralManager:didRetrieveConnectedPeripherals:
    – centralManager:didRetrievePeripherals:
    !
    Monitoring Changes to the Central Manager’s State!
    – centralManagerDidUpdateState:
    – centralManager:willRestoreState:

    View Slide

  54. CBPeripheralDelegate
    Discovering Services!
    – peripheral:didDiscoverServices:
    – peripheral:didDiscoverIncludedServicesForService:error:
    !
    Discovering Characteristics and Characteristic Descriptors!
    – peripheral:didDiscoverCharacteristicsForService:error:
    – peripheral:didDiscoverDescriptorsForCharacteristic:error:
    !
    Retrieving Characteristic and Characteristic Descriptor Values!
    – peripheral:didUpdateValueForCharacteristic:error:
    – peripheral:didUpdateValueForDescriptor:error:
    !
    Writing Characteristic and Characteristic Descriptor Values!
    – peripheral:didWriteValueForCharacteristic:error:
    – peripheral:didWriteValueForDescriptor:error:
    !
    Managing Notifications for a Characteristic’s Value!
    – peripheral:didUpdateNotificationStateForCharacteristic:error:
    !
    Retrieving a Peripheral’s Received Signal Strength Indicator (RSSI) Data!
    – peripheralDidUpdateRSSI:error:
    !
    Monitoring Changes to a Peripheral’s Name or Services!
    – peripheralDidUpdateName:
    – peripheral:didModifyServices:

    View Slide

  55. > code <

    View Slide

  56. Being a Peripheral

    View Slide

  57. Central Peripheral

    View Slide

  58. CBPeripheralManager
    Services!
    – addService:
    – removeService:
    – removeAllServices:
    !
    Advertising!
    – startAdvertising:
    – stopAdvertising
    – isAdvertising
    !
    Notifying observing devices of updates!
    – updateValue:forCharacteristic:onSubscribedCentrals:
    !
    Responding to Read/Write Requests!
    – respondToRequest:withResult

    View Slide

  59. CBPeripheralManagerDelegate
    Monitoring Changes to the Peripheral Manager’s State!
    – peripheralManagerDidUpdateState:
    – peripheralManager:willRestoreState:
    !
    Adding Services!
    – peripheralManager:didAddService:error:
    !
    Advertising Peripheral Data!
    – peripheralManagerDidStartAdvertising:error:
    !
    Monitoring Subscriptions to Characteristic Values!
    – peripheralManager:central:didSubscribeToCharacteristic:
    – peripheralManager:central:didUnsubscribeFromCharacteristic:
    – peripheralManagerIsReadyToUpdateSubscribers:
    !
    Receiving Read and Write Requests!
    – peripheralManager:didReceiveReadRequest:
    – peripheralManager:didReceiveWriteRequests:

    View Slide

  60. CBMutableService
    Initializing a Mutable Service!
    – initWithType:primary:
    !
    Managing a Mutable Service!
    – UUID
    – isPrimary
    – characteristics
    – includedServices
    Initializing a Mutable Characteristic!
    – initWithType:properties:value:permissions:
    !
    Managing a Mutable Characteristic!
    UUID
    value
    descriptors
    properties
    permissions
    subscribedCentrals
    CBMutableCharacteristic

    View Slide

  61. > code <

    View Slide

  62. Foreground vs.
    Background

    View Slide

  63. Two background
    modes.

    View Slide

  64. bluetooth-central
    “Uses Bluetooth LE accessories” in Xcode

    View Slide

  65. bluetooth-peripheral
    “Acts as Bluetooth LE accessory” in Xcode

    View Slide

  66. Specify background
    modes in Info.plist.

    View Slide

  67. Nuances of
    backgrounding

    View Slide

  68. Central Scanning
    • Scanning (as a Central) acts differently in the
    background.
    • Scan options are ignored. Multiple discoveries
    of a peripheral are coalesced into one.
    • The scan interval may increase and your app
    may take longer to discover a peripheral.

    View Slide

  69. Peripheral Advertising
    • Advertising in the background differs from
    foreground mode:
    • The CBAdvertisementDataLocalNameKey is not
    advertised.
    • The frequency at which your app advertises
    may decrease.
    • Service UUIDs may not be advertised. Apple
    does best effort.

    View Slide

  70. Peripheral Events
    • iOS will wake up your app to receive events:
    read, write, and subscribe events.

    View Slide

  71. Caching
    • Services, characteristics and characteristic
    descriptors are cached
    • Characteristic value is kind of cached. When
    discovered the last read value will be provided,
    but it’s put to you to use it (static values) or read
    the value (dynamic values) from the peripheral.

    View Slide

  72. State Preservation and
    Restoration
    • Optional feature.
    • Why? if your app is background it can be terminated
    the OS
    • iOS will store the state of the application and act on
    behalf of it as a proxy. When it receives an event your
    app is waiting for it will start the app back up in the
    background to allow it to process it
    • Single method for you to implement to restore the
    state of your app.

    View Slide

  73. iBeacons

    View Slide

  74. View Slide

  75. A part of CoreLocation

    View Slide

  76. Just a data format in the
    advertising packets.
    !
    Apple to release actual
    profile on ___________.

    View Slide

  77. Tips

    View Slide

  78. CoreBluetooth lives in
    IOBluetooth for Mac
    apps.

    View Slide

  79. No more
    simulator
    support
    as of
    iOS7

    View Slide

  80. Keep references to
    CBPeripheral and CBCentrals
    if you plan on using them.

    View Slide

  81. YOSO!
    YOCO!
    DSWYDNT

    View Slide

  82. Done with peripheral,
    disconnect.

    View Slide

  83. CBPeripheral and
    CBCentral objects can be
    dictionary keys.

    View Slide

  84. If you’re a peripheral
    support characteristic
    notifications.

    View Slide

  85. Require paired
    connections to acquire
    sensitive information

    View Slide

  86. Finding peripherals the system
    already knows about
    – retrieveConnectedPeripheralsWithServices:
    – retrievePeripheralsWithIdentifiers:
    CentralManager

    View Slide

  87. MTU Exchange Requests
    *Borrowed from 2013 Apple WWDC talk

    View Slide

  88. • Allows more data to be sent in one go
    • Less packet overhead
    • up to 20% increase in throughput
    • Free.
    MTU Exchange Requests

    View Slide

  89. App store recommendations
    from Apple
    • include the device with your submission
    • be explicit about services the device provides
    • provide instructions for how to use the device/
    app
    • or don’t and cross your fingers

    View Slide

  90. @zachdennis
    @zdennis
    !
    mutuallyhuman.com

    View Slide