Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SV iOS Meetup Slides: YmsCoreBluetooth and Deep Core Bluetooth

341 views

Published on

These slides were presented at the April 17, 2017 SV iOS Developer Meetup at Tile HQ in San Mateo, CA.
Two presentations are included:
* YmsCoreBluetooth
* Deep Core Bluetooth

Published in: Software
  • Be the first to comment

  • Be the first to like this

SV iOS Meetup Slides: YmsCoreBluetooth and Deep Core Bluetooth

  1. 1. YmsCoreBluetooth A block-based API for Core Bluetooth
  2. 2. YmsCoreBluetooth Core Bluetooth ▸ Bluetooth 4.0 Low Energy (BLE) API ▸ Released 2012 (iOS 5, macOS 10.10) ▸ Delegate-based ▸ Many updates, but very few API changes ▸ Established BLE as the standard Personal Area Network (PAN) for mobile
  3. 3. Gadgets!
  4. 4. TI SensorTag Mid-2013
  5. 5. YmsCoreBluetooth TI SensorTag ▸ IR Temperature Sensor ▸ Humidity Sensor ▸ Gyroscope ▸ Accelerometer ▸ Magnetometer ▸ Barometric Pressure Sensor ▸ Temperature Sensor ▸ Battery/voltage Sensor ▸ Push Buttons BLE! Core Bluetooth!
  6. 6. Using the Core Bluetooth API felt kinda awkward.
  7. 7. YmsCoreBluetooth Programming the SensorTag with Core Bluetooth ▸ Delegation pattern for CBCentralManager and CBPeripheral was clumsy to use ▸ 8 sensors = 8 services ▸ Each service had at least two characteristics ▸ Control, Data ▸ A lot of state to keep track of in different places
  8. 8. In 2013, ObjC blocks were a thing. Why not use them here?
  9. 9. History YmsCoreBluetooth ▸ Inspired by AFNetworking ▸ Use Obj-C block to process response from a BLE request ▸ Captures state ▸ In-line syntax describes control flow in “one place” ▸ Released late 2013 on GitHub ▸ https://github.com/kickingvegas/YmsCoreBluetooth
  10. 10. Design Goals YmsCoreBluetooth Design Goals ▸ Get a BLE app up and running fast ▸ Be ready to use in a production app ▸ Use SensorTag as reference model to sanity check above
  11. 11. Design Goals API Design ▸ Wrapper classes around each Core Bluetooth class ▸ YMSCBCentralManager ▸ YMSCBPeripheral ▸ YMSCBService ▸ YMSCBCharacteristic ▸ YMSCBDescriptor ▸ Subclass off appropriate YMSCB* to personalize YMSCBCentralManager CBCentralManager
  12. 12. Design Goals Hierarchical Object Operation ▸ Core Bluetooth API designed operations in terms of 
 sender sends message to receiver with matching delegate response ▸ CBCentralManager connects to CBPeripheral ▸ CBPeripheral discovers its CBServices ▸ CBPeripheral discovers the CBCharacteristics for a CBService ▸ CBPeripheral writes to CBCharacteristic ▸ High cognitive load
  13. 13. Design Goals Hierarchical Object Operation ▸ YmsCoreBluetooth API designed operations in terms of 
 sending a command to an object ▸ [YMSCBPeripheral connectWithOptions:withBlock:] ▸ [YMSCBPeripheral discoverServices:withBlock:] ▸ [YMSCBService discoverCharacteristics:withBlock:] ▸ [YMSCBCharacteristic writeValue:withBlock:]
  14. 14. Where are we now YmsCoreBluetooth in the Wild ▸ First released on GitHub late 2013 ▸ Last update 2015 ▸ As of 4/12/17 ▸ 498 stars ▸ 128 forks
  15. 15. Coming Real Soon YmsCoreBluetooth Updates Coming! ▸ Improved YMSCBPeripheral collection handling ▸ Improved BLE scanning API ▸ Improved YMSCBCharacteristic subscription API ▸ Dependency Injection Everywhere ▸ Testability ▸ Decouple from Core Bluetooth objects ▸ Obj-C Nullability & Generics ▸ May 2017
  16. 16. github.com/
 kickingvegas/
 YmsCoreBluetooth twitter: cy_choi
  17. 17. Deep Core Bluetooth Learning stuff the hard way
  18. 18. HMM… THAT’S FUNNY, IT WORKS FOR ME. Any developer writing a Core Bluetooth app Deep Core Bluetooth
  19. 19. Learning 1: Your app doesn’t have direct access to the hardware
  20. 20. CBCentralManager CBPeripheral App A BLE Peripheral
  21. 21. CBCentralManager CBPeripheral App A BT Server BLE Peripheral BLE HW
  22. 22. CBCentralManager CBPeripheral CBCentralManager CBPeripheral App A BT Server BLE Peripheral BLE HW
  23. 23. CBCentralManager CBPeripheral CBCentralManager CBPeripheral App A CBCentralManager CBPeripheral CBCentralManager CBPeripheral App B BT Server BLE Peripheral BLE HW
  24. 24. Deep Core Bluetooth Learning 1: Your app doesn’t have direct access to the hardware ▸ CBPeripheral connection is a logical connection ▸ BT Server manages the physical connection to an actual BLE peripheral ▸ Core Bluetooth state restoration works because of this ▸ Core Bluetooth background execution is decoupled from the app state
  25. 25. Learning 2: Unpaired CBPeripheral identifiers are cached values
  26. 26. BLE Peripheral BT Server App advertising MAC: 1235 didDiscoverPeripheral: FRED advertising MAC: 1235 didDiscoverPeripheral: FRED Map MAC to Identifier Map MAC to Identifier Map MAC to Identifier Full advertising MAC: 1235 didDiscoverPeripheral: JANE
  27. 27. Deep Core Bluetooth Learning 2: Unpaired CBPeripheral Identifiers are Cached ▸ There is a fixed size table for mapping MAC addresses to CBPeripheral identifiers in BT Server ▸ When this table is full, an identifier will be invalidated ▸ To test an identifier, you must call 
 [CBCentralManager retrievePeripheralsWithIdentifiers:] ▸ iOS 10 made this table really big
  28. 28. Learning 3: If BT Server resets when your backgrounded BLE app is not running, you won’t hear of it.
  29. 29. BLE Peripheral BT Server App connectPeripheral: connectPeripheral: didConnectPeripheral: didConnectPeripheral: Resets! App terminated 
 in background centralManagerDidUpdateState:
  30. 30. Deep Core Bluetooth Learning 3: If BT Server resets when your background app is not running, you won’t hear of it ▸ Core Bluetooth background execution is reliant on BT Server always running perfectly ▸ If BT Server resets and your app that supports background execution is not running, you will not receive the centralManagerDidUpdateState: message
  31. 31. Learning 4: BLE state restoration is a lot of work
  32. 32. Text Learning 4: BLE state restoration is a lot of work ▸ Restore ServiceUUIDs that were scanned for ▸ You must restore your scan handler ▸ Restore Peripherals that central manager was trying to connect to or had already connected to ▸ You must restore your connection and discovery handlers ▸ Restore Characteristics the central manager was subscribed to ▸ You must restore your notification handlers ▸ Run-time state you keep for a connected peripheral must be restored too!
  33. 33. Deep Core Bluetooth Core Bluetooth Abstracts BLE ▸ Core Bluetooth is an abstraction API on top of the BLE protocol ▸ Mimics BLE link-layer transactions ▸ Exercises only a sub-set of BLE protocol ▸ Your central never has direct communication with your peripheral
  34. 34. We ❤ Core Bluetooth

×