practical core bluetooth in iot & wearable projects @ uikonf 2016

222
Practical Core Bluetooth in IoT & Wearable projects Shuichi Tsutsumi @shu223 iOS Freelancer UIKonf 2016 - Berlin

Upload: shuichi-tsutsumi

Post on 18-Jan-2017

4.882 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Practical Core Bluetooth

in IoT & Wearable projects

Shuichi Tsutsumi @shu223 iOS FreelancerUIKonf 2016 - Berlin

Page 2: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Overview

• Basics of Core Bluetooth

• Practical Core Bluetooth

Page 3: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Bluetooth Low Energy?

Page 4: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Bluetooth Low Energy (BLE)• Wireless technology

- no network infrastructure required

Page 5: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Bluetooth Low Energy (BLE)• Wireless technology

- no network infrastructure required

Wi-Fi

Page 6: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Bluetooth Low Energy (BLE)• Wireless technology

- no network infrastructure required

BLE

Page 7: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Low energy

Page 8: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Low energy

• NOT compatible with Classic BT

Page 9: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Low energy

• NOT compatible with Classic BT

• Uses 2.4 GHz radio frequencies

Page 10: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Low energy

• NOT compatible with Classic BT

• Uses 2.4 GHz radio frequencies

• Marketed by the Bluetooth SIG

Page 11: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Low energy

• NOT compatible with Classic BT

• Uses 2.4 GHz radio frequencies

• Marketed by the Bluetooth SIG

• etc…

Page 12: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Low energy

• NOT compatible with Classic BT

• Uses 2.4 GHz radio frequencies

• Marketed by the Bluetooth SIG

• etc…

Page 13: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

The most important thing for

iOS developers

Page 14: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

The API ‘Core Bluetooth’ is open for developers

Page 15: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

The API ‘Core Bluetooth’ is open for developers

• API for Classic BT is NOT open.

- Requires MFi certification.

Page 16: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE is almost the ONLY way to enable iOS apps to communicate with external hardware without infrastructure or MFi.

Page 17: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Basics of Core Bluetooth

Page 19: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

Page 20: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

BLE Connection

Page 21: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

BLE Connection

Sensors - Gyroscope - Accelerometer

Page 22: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

Sensor Data

BLE Connection

Sensors - Gyroscope - Accelerometer

Page 23: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

Sensor Data

BLE Connection

Sensors - Gyroscope - Accelerometer

Analyze sensor data

Page 24: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

Sensor Data

BLE Connection

Sensors - Gyroscope - Accelerometer

Analyze sensor data

Recognize - Gesture - Posture

Page 25: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Band Moff App

Sensor Data

BLE Connection

Sensors - Gyroscope - Accelerometer

Analyze sensor data

Recognize - Gesture - Posture

Play sounds

Page 26: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Sensor Data

How sensor data is sent with Core Bluetooth

BLE Connection

Page 27: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 1. Scan

Page 28: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 1. Scan: Search for nearby BLE devices

Page 29: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 1. Scan: Search for nearby BLE devices

Page 30: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 1. Scan

Advertise

: Search for nearby BLE devices

Page 31: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Scan

Step 1. Scan

Advertise

: Search for nearby BLE devices

Page 32: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Scan

Step 1. Scan

Advertise

: Search for nearby BLE devices

centralManager.scanForPeripheralsWithServices(nil, options: nil)

Page 33: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Scan

Step 1. Scan

Advertise

Discover

: Search for nearby BLE devices

Page 34: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Scan

Step 1. Scan

Advertise

Discover

: Search for nearby BLE devices

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(“Discovered a BLE device!”) }

Page 35: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Scan

Step 1. Scan

Advertise

Discover

: Search for nearby BLE devices

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(“Discovered a BLE device!”) }

Central

Page 36: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Scan

Step 1. Scan

Advertise

Discover

: Search for nearby BLE devices

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(“Discovered a BLE device!”) }

Peripheral Central

Page 37: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 2. Connect

Page 38: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Connect

Step 2. Connect

Page 39: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Connect

Step 2. Connect

centralManager.connectPeripheral(peripheral, options: nil)

Page 40: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Connect

Step 2. Connect

centralManager.connectPeripheral(peripheral, options: nil)

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { print(“Connected!”) }

Page 41: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 3. Subscribe

Page 42: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 3. Subscribe: Ready to receive data

Page 43: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 3. Subscribe: Ready to receive data

GATT = Generic Attribute Profile

Page 44: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Service

xx Service

Step 3. Subscribe: Ready to receive data

GATT = Generic Attribute Profile

Page 45: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Service

xx Service

Button Characteristic

xx Characteristic

Sensor Characteristic

Step 3. Subscribe: Ready to receive data

GATT = Generic Attribute Profile

Page 46: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Service

xx Service

Button Characteristic

xx Characteristic

Sensor Characteristic

Subscribe (Request to be notified)

Step 3. Subscribe: Ready to receive data

GATT = Generic Attribute Profile

Page 47: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Moff Service

xx Service

Button Characteristic

xx Characteristic

Sensor Characteristic

Subscribe (Request to be notified)

Step 3. Subscribe: Ready to receive data

GATT = Generic Attribute Profile

peripheral.setNotifyValue(true, forCharacteristic: c)

Page 48: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 4. Notify

Moff Service

Sensor Characteristic

xxxx Characteristic

Page 49: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 4. Notify

Moff Service

Sensor Characteristic

xxxx Characteristic

Update the value

Page 50: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 4. Notify

Notify subscribers

Moff Service

Sensor Characteristic

xxxx Characteristic

Update the value

Page 51: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 4. Notify

Notify subscribers

Moff Service

Sensor Characteristic

xxxx Characteristic

Update the value

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { print(“Received sensor data!”) }

Page 52: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Step 4. Notify

Notify subscribers

Moff Service

Sensor Characteristic

xxxx Characteristic

Update the value

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { print(“Received sensor data!”) }

Page 53: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Hardware Side?

Page 54: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Hardware Side?I’m sorry, I don’t know…

Page 55: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE

Page 56: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE

iOS Engineer

Page 57: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE

iOS Engineer

Page 58: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE

Firmware Engineer iOS Engineer

Page 59: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE

Firmware Engineer iOS Engineer

Page 60: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

HW Projects I’ve worked on

as an iOS engineer

Page 61: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Wheelchair ‘WHILL’

Page 62: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Wheelchair ‘WHILL’

Page 63: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Wearable Walkie-Talkie ‘BONX’

Page 64: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Wearable Walkie-Talkie ‘BONX’

Page 65: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Wearable Walkie-Talkie ‘BONX’

Page 66: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Group conversation system with VoIP

- Detects the human voice

- Cuts out all background noise

Page 67: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

→ Can be used even in areas with poor coverage!

Group conversation system with VoIP

- Detects the human voice

- Cuts out all background noise

Page 68: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE for BONX

Page 69: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE for BONX

Page 70: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

BLE for BONX

Page 71: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

IoT for Cars ‘SmartDrive’

Page 73: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Music for the DeafDaito Manabe/Motoi Ishibashi/Teruoka Masaki/Shuichi Tsutsumi x SOUL FAMILY

Page 74: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Music for the DeafDaito Manabe/Motoi Ishibashi/Teruoka Masaki/Shuichi Tsutsumi x SOUL FAMILY

iPhone

Page 75: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Music for the DeafDaito Manabe/Motoi Ishibashi/Teruoka Masaki/Shuichi Tsutsumi x SOUL FAMILY

Electronic Stimulation DeviceiPhone

Page 76: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Music for the DeafDaito Manabe/Motoi Ishibashi/Teruoka Masaki/Shuichi Tsutsumi x SOUL FAMILY

Commands

Electronic Stimulation DeviceiPhone

Page 77: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Music for the DeafDaito Manabe/Motoi Ishibashi/Teruoka Masaki/Shuichi Tsutsumi x SOUL FAMILY

Page 78: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Practical Core Bluetooth

Page 79: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Defining GATT

Page 80: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send sensor data

Foo Service

Sensor Data Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Bar Service

xx Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: acc x, acc y, acc z, gyro x, gyro y, gyro z (2bytes for each)

xx Characteristic

Page 81: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send sensor data

Foo Service

Sensor Data Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Bar Service

xx Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: acc x, acc y, acc z, gyro x, gyro y, gyro z (2bytes for each)

xx Characteristic

Page 82: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send sensor data

Foo Service

Sensor Data Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Bar Service

xx Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: acc x, acc y, acc z, gyro x, gyro y, gyro z (2bytes for each)

xx Characteristic

Page 83: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send sensor data

Foo Service

Sensor Data Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Bar Service

xx Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: acc x, acc y, acc z, gyro x, gyro y, gyro z (2bytes for each)

xx Characteristic

Page 84: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send sensor data

Foo Service

Sensor Data Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Bar Service

xx Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: acc x, acc y, acc z, gyro x, gyro y, gyro z (2bytes for each)

xx Characteristic

Page 85: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send sensor data

Foo Service

Sensor Data Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Bar Service

xx Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: acc x, acc y, acc z, gyro x, gyro y, gyro z (2bytes for each)

xx Characteristic

Page 86: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Page 87: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Page 88: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Page 89: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Button interactions

Page 90: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Button interactions

Page 91: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Button interactions

Page 92: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT to send button interactions

Foo Service

Button Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Notify

Value: 0x01 or 0x00

Button interactions

0x01: Pushed 0x00: Released

Page 93: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT for remote control

Foo Service

Remote Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Write

Value: Horizontal (-100 ~ 100), Vertical (-100 ~ 100)

Page 94: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT for remote control

Foo Service

Remote Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Write

Value: Horizontal (-100 ~ 100), Vertical (-100 ~ 100)

Page 95: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT for remote control

Foo Service

Remote Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Write

Value: Horizontal (-100 ~ 100), Vertical (-100 ~ 100)

values

Page 96: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT for remote control

Foo Service

Remote Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Write

Value: Horizontal (-100 ~ 100), Vertical (-100 ~ 100)

values

Page 97: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

GATT for remote control

Foo Service

Remote Control Characteristic

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Properties: Write

Value: Horizontal (-100 ~ 100), Vertical (-100 ~ 100)

values

Page 98: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to define GATT1. Create UUID for the service & characteristic

2. Define the properties (Read, Notify, Write, etc.)

Peripheral → Central: Notify Central → Peripheral: Write

3. Define the value format

Usually limited to 20 bytes

$ uuidgen CEEA31BC-BEAC-4A78-B7ED-FC96B6254D4C

Page 99: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Reference: GATT profiles by Bluetooth SIG

Page 100: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Defining background behaviors

Page 101: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Background behaviors on iOS• Very limited

- Listening to music

- Getting location data

- Downloading data

Page 102: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Imagine

Page 103: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

If BLE didn’t work in the background…

Page 104: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 105: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

You would have to always keep the app in the foreground, while snowboarding!

Page 106: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 107: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

You would have to launch the app

whenever you go driving!

Page 108: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

Page 109: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

• Scanning peripherals

Page 110: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

• Scanning peripherals

• Connecting with peripherals

Page 111: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

• Scanning peripherals

• Connecting with peripherals

• Reading characteristics’ value

Page 112: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

• Scanning peripherals

• Connecting with peripherals

• Reading characteristics’ value

• Writing characteristics’ value

Page 113: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

• Scanning peripherals

• Connecting with peripherals

• Reading characteristics’ value

• Writing characteristics’ value

• Receiving notifications

Page 114: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

What functions can be used in the background?

• Scanning peripherals

• Connecting with peripherals

• Reading characteristics’ value

• Writing characteristics’ value

• Receiving notifications

Almost all functions can be used even in the background!

Page 115: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to support background mode

Page 116: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 117: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Just check this box!

Page 118: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Limitations

Page 119: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Limitations• Longer intervals for scanning

Page 120: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Limitations• Longer intervals for scanning

• Must explicitly specify one or more services to scan

Page 121: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Limitations• Longer intervals for scanning

• Must explicitly specify one or more services to scan

• CBCentralManagerOptionShowPowerAlertKey option is ignored

Page 122: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Limitations• Longer intervals for scanning

• Must explicitly specify one or more services to scan

• CBCentralManagerOptionShowPowerAlertKey option is ignored

Resources

- Core Bluetooth Programming Guide

- Core Bluetooth Framework Reference (also in headers)

Page 123: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

If the app in the background is killed by the system…

Page 124: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 125: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

State Preservation and Restoration

Page 126: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

State Preservation and Restoration

The system takes over the BLE tasks even after the app is killed.

Page 127: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

State Preservation and Restoration

The system takes over the BLE tasks even after the app is killed.

- Preserves the state of the app’s central managers and continues the BLE tasks on their behalf.

Page 128: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

State Preservation and Restoration

The system takes over the BLE tasks even after the app is killed.

- Preserves the state of the app’s central managers and continues the BLE tasks on their behalf.

- Relaunches the app into the background and calls the corresponding delegate method.

Page 129: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

State Preservation and Restoration

The system takes over the BLE tasks even after the app is killed.

- Preserves the state of the app’s central managers and continues the BLE tasks on their behalf.

- Relaunches the app into the background and calls the corresponding delegate method.

→ The app can handle BLE events!

Page 130: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 131: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• The app is killed by the system

Page 132: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• The app is killed by the system

• The user pushes the button on the peripheral device

Page 133: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• The app is killed by the system

• The user pushes the button on the peripheral device

• The system receives the notification

Page 134: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• The app is killed by the system

• The user pushes the button on the peripheral device

• The system receives the notification

• The app is relaunched in the BG and the delegate method is called.

Page 135: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { print(“Received the characteristic data!”) }

• The app is killed by the system

• The user pushes the button on the peripheral device

• The system receives the notification

• The app is relaunched in the BG and the delegate method is called.

Page 136: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { print(“Received the characteristic data!”) }

• The app is killed by the system

• The user pushes the button on the peripheral device

• The system receives the notification

• The app is relaunched in the BG and the delegate method is called.

The app can process the button interaction!

Page 137: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Implementation

Page 138: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Implementation• Add an option when initializing the

CBCentralManager object.

let options = [ CBCentralManagerOptionRestoreIdentifierKey: "somekey" ] centralManager = CBCentralManager( delegate: self, queue: queue, options: options)

Page 139: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Implementation• Implement the delegate method which is called when

the app is restored.

func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) { print("Restored") }

Page 140: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Testing the restoration

Page 141: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Testing the restoration• Kill the app as if it was killed by the iOS

kill(getpid(), SIGKILL);

Page 142: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to test without HW prototypes

Page 143: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Did the HW prototypes exist when develop the apps?

Projects HW prototypes existed?

Moff YES

WHILL NOBONX NOSmartDrive YES

PLEN2 NOMusic for the Deaf NO

Page 144: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development Kit

Page 145: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development KitBLE Module

Page 146: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development KitBLE Module

Display

Page 147: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development KitBLE Module

Display

USB interface

Page 148: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development KitBLE Module

Display

USB interface

Battery box

Page 149: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development Kit

Can start development without creating a circuit ourselves

BLE ModuleDisplay

USB interface

Battery box

Page 150: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 151: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development Kit

Page 152: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development Kit App

Page 153: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Development Kit AppBLE

Page 154: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Horizontal (-100~100) / Vertical (-100~100)

Development Kit AppBLE

Page 155: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 156: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Emulator App

Page 157: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Emulator App

• Develop another iOS app as the alternative to the peripheral device

Page 158: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Emulator App

• Develop another iOS app as the alternative to the peripheral device

• Use CBPeripheralManager

Page 159: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Emulator App

• Develop another iOS app as the alternative to the peripheral device

• Use CBPeripheralManager

• Easier for iOS engineers

Page 160: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 161: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 162: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 163: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

=

Page 164: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Multi-Function Control • Single Tap • Double Tap • Long Press • Very Long Press

=

Page 165: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Troubleshooting

Page 166: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t find the peripheral

Scan

?

Page 167: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t connect

Find

Page 168: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t connect

Connect

×

Page 169: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Connection dropped

Page 170: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Connection dropped

• Can’t discover services or characteristics

Page 171: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Connection dropped

• Can’t discover services or characteristics

• Fail to write

Page 172: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Connection dropped

• Can’t discover services or characteristics

• Fail to write

• Incorrect characteristic values

Page 173: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Connection dropped

• Can’t discover services or characteristics

• Fail to write

• Incorrect characteristic values

• Incorrect behaviors in the background

Page 174: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Connection dropped

• Can’t discover services or characteristics

• Fail to write

• Incorrect characteristic values

• Incorrect behaviors in the background

• etc…

Page 175: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Identify which side the problem is on

Is the problem on the central side or on the peripheral side?

→ Compare with another iOS app like ‘LightBlue’

Page 176: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t find the peripheral

• Scanning, but can’t find the peripheral device

Page 177: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t find the peripheral

• Scanning, but can’t find the peripheral device

Scan

Page 178: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t find the peripheral

• Scanning, but can’t find the peripheral device

Scan

but ‘didDiscover’ method isn’t called

Page 179: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t find the peripheral• Scan with another iOS app

Scan

Page 180: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t find the peripheral• Scan with another iOS app

Scan

Discovered peripherals

Page 181: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t connect, etc.

Connect

Page 182: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Can’t connect, etc.

Connect

Services & Characteristics

Page 183: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Other debugging tools

Page 184: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Bluetooth Explorer

Page 185: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Bluetooth ExplorerAn OS X app made by Apple

Page 186: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 187: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Low Energy Devices

Page 188: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Low Energy Devices

Scan, Connect

Page 189: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Low Energy Devices

Scan, Connect Details of services and characteristics

Page 190: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Low Energy Devices

Scan, Connect Details of services and characteristics

Write, Read, Register Notify (Subscribe)

Page 191: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

func centralManager( central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(advertisementData) }

Page 192: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

func centralManager( central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(advertisementData) }

Filtered by iOS!

Page 193: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

func centralManager( central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(advertisementData) }

Filtered by iOS! → Can’t see all of the advertisement data

Page 194: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

func centralManager( central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber!) { print(advertisementData) }

Filtered by iOS!e.g. Can’t see the ‘Manufacture Data’ field of iBeacon

→ Can’t see all of the advertisement data

Page 195: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 196: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Not filtered → Can see the ‘Manufacture Data’ field of iBeacon

Page 197: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Packet Logger

Page 198: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Packet Logger

An OS X app made by Apple

Page 199: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 200: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 201: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to get

Page 202: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to get

Page 203: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to get

Download ‘Hardware IO Tools for Xcode’

Page 204: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to get

Download ‘Hardware IO Tools for Xcode’

Page 205: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to get

Download ‘Hardware IO Tools for Xcode’

Page 206: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

How to get

Download ‘Hardware IO Tools for Xcode’

Page 207: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

App Review

Page 208: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 209: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

MSRP $13,995

Page 210: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Costing too much

Page 211: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Not enough devices when submitting

Page 212: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Videos for review

Page 213: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Videos for review

• Show how the app works with the peripheral device

Page 214: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Videos for review

• Show how the app works with the peripheral device• Not need to be cool!

Page 215: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016
Page 216: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Shoot with iPhone

Page 217: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

• Shoot with iPhone • Edit with iMovie

Page 218: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Recap• What is Bluetooth Low Energy?

• Basics of Core Bluetooth with an actual product

• Practical Core Bluetooth with specific examples

- Defining GATT

- Defining background behaviors

- Testing without HW prototypes

- Troubleshooting

- App Review

Page 219: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

iOS×BLE

カバーデザイン

束:31mm

袖:100mm 袖:100mm

表 4 表 1

iOS×BLECore Bluetoothプログラミング

iOS× BLECore Bluetooth

プログラミング

堤修一

松村礼央=著

堤修一×松村礼央 = 著Shuichi TSUTSUMI × Reo MATSUMURA

iOS×BLECore Bluetooth Programming

iOS×BLE

定価:本体4,000円(税別)

Resources• 500 pages about

iOS x BLE

• Japanese only

Page 220: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Resources• Getting Started with Bluetooth Low Energy

- Covers large area of BLE

• Core Bluetooth Programming Guide

- Good to know the overview of Core Bluetooth

• Core Bluetooth Framework Reference

- Details of Core Bluetooth

• Bluetooth Accessory Design Guidelines for Apple Products

- For HW side, but helpful also for iOS engineers to know specs or limitations due to HW side such as connection intervals.

Page 221: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Resources• WWDC 2012 - Session 703 - Core Bluetooth 101

- Helpful to know about BLE

• WWDC 2012 - Session 705 - Advanced Core Bluetooth

- Connection interval, Caching, etc…

• WWDC 2013 - Session 703 - Core Bluetooth

- Retrieving peripherals, State preservation & restoration etc..

- Includes advanced and detailed information

• Bluetooth Core Specification by Bluetooth SIG

- The official specification / Total 2000 pages…

Page 222: Practical Core Bluetooth in IoT & Wearable projects @ UIKonf 2016

Thank you!

Shuichi Tsutsumi - iOS Freelancer

• Twitter: @shu223

• GitHub: shu223

• Blog: https://medium.com/@shu223/

• Email: [email protected]