In recent years, "IoT" or "Wearable" are one of buzzwords, so many people might have interests in building hardware products. But learning how to develop electric circuits, mechanical systems or embedded systems etc. from zero is so difficult. However, iOS developers can contribute to projects of hardware products with the knowledge of Core Bluetooth / Bluetooth Low Energy (BTLE), even if they are not familiar with hardware layer. In this session, he will introduce BTLE, show easy examples of Core Bluetooth, and share knowledges with his experiences developing more than 10 apps for IoT and Wearable products.
What is Bluetooth Low Energy? Why use this?
Very easy examples of how to communicate using Core Bluetooth
What part was my responsibility in the projects? Communication with firmware engineer.
Designing GATT
Designing the behavior of the app in background
Limitations in background. What are possible and impossible?
State Preservation and Restoration
Develop without prototypes of the hardware
BTLE Module's Developer Kit
Prototyping tools
Build emulator apps
Trouble Shootings
Debugging tools
Each cases: Can't find / connect / send or receive information
45. Moff Service
xx Service
Button Characteristic
xx Characteristic
Sensor Characteristic
Step 3. Subscribe: Ready to receive data
GATT = Generic Attribute Profile
46. 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
47. 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)
80. 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
81. 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
82. 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
83. 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
84. 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
85. 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
86. 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
87. 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
88. 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
89. 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
90. 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
91. 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
92. 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
93. 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)
94. 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)
95. 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
96. 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
97. 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
98. How to define GATT
1. 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
110. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
111. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
• Reading characteristics’ value
112. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
• Reading characteristics’ value
• Writing characteristics’ value
113. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
• Reading characteristics’ value
• Writing characteristics’ value
• Receiving notifications
114. 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!
121. Limitations
• Longer intervals for scanning
• Must explicitly specify one or more services to scan
• CBCentralManagerOptionShowPowerAlertKey option is
ignored
122. 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)
123. If the app in the background
is killed by the system…
126. State Preservation and Restoration
The system takes over the BLE tasks
even after the app is killed.
127. 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.
128. 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.
129. 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!
132. • The app is killed by the system
• The user pushes the button on the
peripheral device
133. • The app is killed by the system
• The user pushes the button on the
peripheral device
• The system receives the notification
134. • 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.
135. 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.
136. 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!
138. Implementation
• Add an option when initializing the
CBCentralManager object.
let options = [
CBCentralManagerOptionRestoreIdentifierKey: "somekey"
]
centralManager = CBCentralManager(
delegate: self,
queue: queue,
options: options)
139. Implementation
• Implement the delegate method which is called when
the app is restored.
func centralManager(central: CBCentralManager,
willRestoreState dict: [String : AnyObject])
{
print("Restored")
}
143. Did the HW prototypes exist when develop the apps?
Projects HW prototypes existed?
Moff YES
WHILL NO
BONX NO
SmartDrive YES
PLEN2 NO
Music for the Deaf NO
172. • Connection dropped
• Can’t discover services or characteristics
• Fail to write
• Incorrect characteristic values
173. • Connection dropped
• Can’t discover services or characteristics
• Fail to write
• Incorrect characteristic values
• Incorrect behaviors in the background
174. • Connection dropped
• Can’t discover services or characteristics
• Fail to write
• Incorrect characteristic values
• Incorrect behaviors in the background
• etc…
175. 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’
176. Can’t find the peripheral
• Scanning, but can’t find the peripheral device
177. Can’t find the peripheral
• Scanning, but can’t find the peripheral device
Scan
178. Can’t find the peripheral
• Scanning, but can’t find the peripheral device
Scan
but ‘didDiscover’
method isn’t called
179. Can’t find the peripheral
• Scan with another iOS app
Scan
180. Can’t find the peripheral
• Scan with another iOS app
Scan
Discovered peripherals
194. 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
218. 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
219. 束:31mm
表 4 表 1
iOS×BLE
Core Bluetooth
プログラミング
iOS×BLECoreBluetooth
プ
ロ
グ
ラ
ミ
ン
グ
堤
修
一
松
村
礼
央
=
著
堤修一×松村礼央=著
Shuichi TSUTSUMI×Reo MATSUMURA
iOS×BLE
Core Bluetooth Programming
iOS×BLE
定価:本体4,000円(税別)
Resources
• 500 pages about
iOS x BLE
• Japanese only
220. 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.
221. 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…