+ All Categories
Home > Technology > BTLE (Bluetooth Low Energy) and CoreBluetooth

BTLE (Bluetooth Low Energy) and CoreBluetooth

Date post: 11-Jan-2015
Category:
Upload: zach-dennis
View: 6,622 times
Download: 5 times
Share this document with a friend
Description:
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.
90
Bluetooth LE, CoreBluetooth @zachdennis @zdennis mutuallyhuman.com
Transcript
Page 1: BTLE (Bluetooth Low Energy) and CoreBluetooth

Bluetooth LE, CoreBluetooth

@zachdennis @zdennis

!mutuallyhuman.com

Page 2: BTLE (Bluetooth Low Energy) and CoreBluetooth

Bluetooth LE

Page 3: BTLE (Bluetooth Low Energy) and CoreBluetooth

What is BTLE?

Page 4: BTLE (Bluetooth Low Energy) and CoreBluetooth

A short range, ultra-low power consuming

wireless technology.

Page 5: BTLE (Bluetooth Low Energy) and CoreBluetooth

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

Page 6: BTLE (Bluetooth Low Energy) and CoreBluetooth

Power ConsumptionYears, not hours or

days.

Page 7: BTLE (Bluetooth Low Energy) and CoreBluetooth

Short range~ 50m

Page 8: BTLE (Bluetooth Low Energy) and CoreBluetooth

Packet-basedShort bursts of data.

Page 9: BTLE (Bluetooth Low Energy) and CoreBluetooth

Intervals

Ad Ad AdData Data

Page 10: BTLE (Bluetooth Low Energy) and CoreBluetooth

Multiple channels

Ad Ad AdData Data

Ad Ad AdData Data

Ad Ad AdData Data#1

#2

#3

Page 11: BTLE (Bluetooth Low Energy) and CoreBluetooth

Device A is looking.

Ad AdData Data

Ad AdData Data

Scan

Ad

Ad

Scan Data

Device B is advertising.

Page 12: BTLE (Bluetooth Low Energy) and CoreBluetooth

Frequency hopping

Page 13: BTLE (Bluetooth Low Energy) and CoreBluetooth

Connection-lessDevices do not need to maintain

connections.

Page 14: BTLE (Bluetooth Low Energy) and CoreBluetooth

Pairinge.g. iOS - happens when Insufficient

Authentication error code is sent from the peripheral.

Page 15: BTLE (Bluetooth Low Energy) and CoreBluetooth

SecurityDevices pair, keys are distributed, and the

connection is encrypted. !

Encryption is AES-128.

Page 16: BTLE (Bluetooth Low Energy) and CoreBluetooth
Page 17: BTLE (Bluetooth Low Energy) and CoreBluetooth

Why BTLE?

Page 18: BTLE (Bluetooth Low Energy) and CoreBluetooth

*Borrowed from 2012 WWDC CoreBluetooth Talk

Page 19: BTLE (Bluetooth Low Energy) and CoreBluetooth

1 billion+ devices

Page 20: BTLE (Bluetooth Low Energy) and CoreBluetooth

*Borrowed from 2013 Apple WWDC talk

Page 21: BTLE (Bluetooth Low Energy) and CoreBluetooth

How BTLE Works

Page 22: BTLE (Bluetooth Low Energy) and CoreBluetooth

The Stack

Page 23: BTLE (Bluetooth Low Energy) and CoreBluetooth

Key Terms• Central

• Peripheral

• Service

• Characteristic

• Descriptor

Page 24: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheral(has data)(wants data)

Page 25: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

Page 26: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

scan

Page 27: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

advertising

scan

Page 28: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

scan advertising

discover services Heart Monitor

Page 29: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

scan advertising

discover services

discover characteristics

Heart Monitor

BPM

Page 30: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

scan advertising

discover services

discover characteristics

Heart Monitor

BPM

read value 95

Page 31: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

scan advertising

discover services

discover characteristics

Heart Monitor

BPM

read value 95

observe value BPM 95

Page 32: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheraladvertising

scan advertising

discover services

discover characteristics

Heart Monitor

BPM

read value 95

observe value BPM 95BPM 95BPM 98value changed

Page 33: BTLE (Bluetooth Low Energy) and CoreBluetooth

Service

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

Page 34: BTLE (Bluetooth Low Energy) and CoreBluetooth

Two kinds of services

There are primary services and secondary services.

Page 35: BTLE (Bluetooth Low Energy) and CoreBluetooth

Nested services

Services can contain other services.

Page 36: BTLE (Bluetooth Low Energy) and CoreBluetooth

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.

Page 37: BTLE (Bluetooth Low Energy) and CoreBluetooth

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.

Page 38: BTLE (Bluetooth Low Energy) and CoreBluetooth

Heart Rate ProfileCollector Heart Rate Sensor

Heart Rate Service

Device Information Service

Page 39: BTLE (Bluetooth Low Energy) and CoreBluetooth

Heart Rate ProfileCollector Heart Rate Sensor

Heart Rate Service

Device Information Service

GATT CLIENT GATT SERVER

Page 40: BTLE (Bluetooth Low Energy) and CoreBluetooth

Profiles & Services

Profiles contain services.

Services can be contained by multiple profiles.

Page 41: BTLE (Bluetooth Low Energy) and CoreBluetooth

Peripheral

Characteristic

Characteristic

Service

Characteristic

Anatomy of a Peripheral

Descriptor

Service

Page 42: BTLE (Bluetooth Low Energy) and CoreBluetooth

Apple’s Approach to BTLE

Page 43: BTLE (Bluetooth Low Energy) and CoreBluetooth

Simple

Powerful

Page 44: BTLE (Bluetooth Low Energy) and CoreBluetooth

Technology Stack

Page 45: BTLE (Bluetooth Low Energy) and CoreBluetooth

*Borrowed from 2012 WWDC CoreBluetooth Talk

Page 46: BTLE (Bluetooth Low Energy) and CoreBluetooth

Supported Profiles

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

Page 47: BTLE (Bluetooth Low Energy) and CoreBluetooth

CoreBluetooth

Page 48: BTLE (Bluetooth Low Energy) and CoreBluetooth

Object Model

Page 49: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBCharacteristic

CBService

CBCentral

CBMutableCharacteristic

CBMutableService

Main Objects

Data Objects

Helper ObjectsCBUUID CBATTRequest

CBCentralManagerDelegate CBPeripheralManagerDelegate

CBCentralManager CBPeripheralManager

CBPeripheralDelegate

CBPeripheral

Page 50: BTLE (Bluetooth Low Energy) and CoreBluetooth

Being a Central

Page 51: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheral

Page 52: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBCentralManager

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

Page 53: BTLE (Bluetooth Low Energy) and CoreBluetooth

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:

Page 54: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBPeripheralDelegateDiscovering 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:

Page 55: BTLE (Bluetooth Low Energy) and CoreBluetooth

> code <

Page 56: BTLE (Bluetooth Low Energy) and CoreBluetooth

Being a Peripheral

Page 57: BTLE (Bluetooth Low Energy) and CoreBluetooth

Central Peripheral

Page 58: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBPeripheralManager

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

Page 59: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBPeripheralManagerDelegateMonitoring 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:

Page 60: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBMutableServiceInitializing 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

Page 61: BTLE (Bluetooth Low Energy) and CoreBluetooth

> code <

Page 62: BTLE (Bluetooth Low Energy) and CoreBluetooth

Foreground vs. Background

Page 63: BTLE (Bluetooth Low Energy) and CoreBluetooth

Two background modes.

Page 64: BTLE (Bluetooth Low Energy) and CoreBluetooth

bluetooth-central“Uses Bluetooth LE accessories” in Xcode

Page 65: BTLE (Bluetooth Low Energy) and CoreBluetooth

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

Page 66: BTLE (Bluetooth Low Energy) and CoreBluetooth

Specify background modes in Info.plist.

Page 67: BTLE (Bluetooth Low Energy) and CoreBluetooth

Nuances of backgrounding

Page 68: BTLE (Bluetooth Low Energy) and CoreBluetooth

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.

Page 69: BTLE (Bluetooth Low Energy) and CoreBluetooth

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.

Page 70: BTLE (Bluetooth Low Energy) and CoreBluetooth

Peripheral Events

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

Page 71: BTLE (Bluetooth Low Energy) and CoreBluetooth

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.

Page 72: BTLE (Bluetooth Low Energy) and CoreBluetooth

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.

Page 73: BTLE (Bluetooth Low Energy) and CoreBluetooth

iBeacons

Page 74: BTLE (Bluetooth Low Energy) and CoreBluetooth
Page 75: BTLE (Bluetooth Low Energy) and CoreBluetooth

A part of CoreLocation

Page 76: BTLE (Bluetooth Low Energy) and CoreBluetooth

Just a data format in the advertising packets.

!

Apple to release actual profile on ___________.

Page 77: BTLE (Bluetooth Low Energy) and CoreBluetooth

Tips

Page 78: BTLE (Bluetooth Low Energy) and CoreBluetooth

CoreBluetooth lives in IOBluetooth for Mac

apps.

Page 79: BTLE (Bluetooth Low Energy) and CoreBluetooth

No more simulator support

as of iOS7

Page 80: BTLE (Bluetooth Low Energy) and CoreBluetooth

Keep references to CBPeripheral and CBCentrals

if you plan on using them.

Page 81: BTLE (Bluetooth Low Energy) and CoreBluetooth

YOSO!YOCO!

DSWYDNT

Page 82: BTLE (Bluetooth Low Energy) and CoreBluetooth

Done with peripheral, disconnect.

Page 83: BTLE (Bluetooth Low Energy) and CoreBluetooth

CBPeripheral and CBCentral objects can be

dictionary keys.

Page 84: BTLE (Bluetooth Low Energy) and CoreBluetooth

If you’re a peripheral support characteristic

notifications.

Page 85: BTLE (Bluetooth Low Energy) and CoreBluetooth

Require paired connections to acquire sensitive information

Page 86: BTLE (Bluetooth Low Energy) and CoreBluetooth

Finding peripherals the system already knows about

– retrieveConnectedPeripheralsWithServices: – retrievePeripheralsWithIdentifiers:

CentralManager

Page 87: BTLE (Bluetooth Low Energy) and CoreBluetooth

MTU Exchange Requests

*Borrowed from 2013 Apple WWDC talk

Page 88: BTLE (Bluetooth Low Energy) and CoreBluetooth

• Allows more data to be sent in one go

• Less packet overhead

• up to 20% increase in throughput

• Free.

MTU Exchange Requests

Page 89: BTLE (Bluetooth Low Energy) and CoreBluetooth

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

Page 90: BTLE (Bluetooth Low Energy) and CoreBluetooth

@zachdennis @zdennis

!mutuallyhuman.com


Recommended