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
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
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. In 2013, ObjC blocks were
a thing. Why not use them
here?
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. 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. 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. 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. 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. 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. 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
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
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. 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. Learning 3: If BT Server resets
when your backgrounded BLE app
is not running, you won’t hear of it.
29. BLE Peripheral BT Server App
connectPeripheral:
connectPeripheral:
didConnectPeripheral: didConnectPeripheral:
Resets!
App terminated
in background
centralManagerDidUpdateState:
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
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. 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