Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Bluetooth LE

Slide 3

Slide 3 text

What is BTLE?

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Power Consumption Years, not hours or days.

Slide 7

Slide 7 text

Short range ~ 50m

Slide 8

Slide 8 text

Packet-based Short bursts of data.

Slide 9

Slide 9 text

Intervals Ad Ad Ad Data Data

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Frequency hopping

Slide 13

Slide 13 text

Connection-less Devices do not need to maintain connections.

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Why BTLE?

Slide 18

Slide 18 text

*Borrowed from 2012 WWDC CoreBluetooth Talk

Slide 19

Slide 19 text

1 billion+ devices

Slide 20

Slide 20 text

*Borrowed from 2013 Apple WWDC talk

Slide 21

Slide 21 text

How BTLE Works

Slide 22

Slide 22 text

The Stack

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Central Peripheral (has data) (wants data)

Slide 25

Slide 25 text

Central Peripheral advertising

Slide 26

Slide 26 text

Central Peripheral advertising scan

Slide 27

Slide 27 text

Central Peripheral advertising advertising scan

Slide 28

Slide 28 text

Central Peripheral advertising scan advertising discover services Heart Monitor

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Nested services Services can contain other services.

Slide 36

Slide 36 text

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.

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Peripheral Characteristic Characteristic Service Characteristic Anatomy of a Peripheral Descriptor Service

Slide 42

Slide 42 text

Apple’s Approach to BTLE

Slide 43

Slide 43 text

Simple Powerful

Slide 44

Slide 44 text

Technology Stack

Slide 45

Slide 45 text

*Borrowed from 2012 WWDC CoreBluetooth Talk

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

CoreBluetooth

Slide 48

Slide 48 text

Object Model

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Being a Central

Slide 51

Slide 51 text

Central Peripheral

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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:

Slide 54

Slide 54 text

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:

Slide 55

Slide 55 text

> code <

Slide 56

Slide 56 text

Being a Peripheral

Slide 57

Slide 57 text

Central Peripheral

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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:

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

> code <

Slide 62

Slide 62 text

Foreground vs. Background

Slide 63

Slide 63 text

Two background modes.

Slide 64

Slide 64 text

bluetooth-central “Uses Bluetooth LE accessories” in Xcode

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Specify background modes in Info.plist.

Slide 67

Slide 67 text

Nuances of backgrounding

Slide 68

Slide 68 text

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.

Slide 69

Slide 69 text

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.

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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.

Slide 72

Slide 72 text

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.

Slide 73

Slide 73 text

iBeacons

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

A part of CoreLocation

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

Tips

Slide 78

Slide 78 text

CoreBluetooth lives in IOBluetooth for Mac apps.

Slide 79

Slide 79 text

No more simulator support as of iOS7

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

YOSO! YOCO! DSWYDNT

Slide 82

Slide 82 text

Done with peripheral, disconnect.

Slide 83

Slide 83 text

CBPeripheral and CBCentral objects can be dictionary keys.

Slide 84

Slide 84 text

If you’re a peripheral support characteristic notifications.

Slide 85

Slide 85 text

Require paired connections to acquire sensitive information

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

MTU Exchange Requests *Borrowed from 2013 Apple WWDC talk

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

@zachdennis @zdennis ! mutuallyhuman.com