n recent years, "IoT" or "Wearable" are one of buzzwords, so you 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 (BLE), even if they are not familiar with hardware layer.
In this session, you can learn the basics of Core Bluetooth / BLE (what it is, why we use it, and how it works), and practical knowledges to build apps for hardware products (how to design the apps, how to test without actual hardware prototypes, troubleshooting tips, and how the apps can be reviewed by Apple) which I learned through actual IoT/Wearable projects.
This would be interesting & understandable even if you are not familiar with or have no interests in Core Bluetooth because of the actual examples.
52. Moff Service
xx Service
Button Characteristic
xx Characteristic
Sensor Characteristic
Step 3. Subscribe: Ready to receive data
GATT = Generic Attribute Profile
53. 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
54. 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)
91. 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
92. 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
93. 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
94. 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
95. 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
96. 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
97. 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
98. 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
99. 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
100. 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
101. 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
102. 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
103. 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
104. 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)
105. 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)
106. 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
107. 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
108. 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
110. GATT for generic commands
Electronic Stimulation Control Characteristic
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties: Write
Value: 1 byte
111. GATT for generic commands
Electronic Stimulation Control Characteristic
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties: Write
Value: 1 byte
112. GATT for generic commands
Electronic Stimulation Control Characteristic
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties: Write
Value: 1 byte
4 Commands:
- Change the pulse frequency
- Change the electric current
- Right channels on/off
- Left channels on/off
113. GATT for generic commands
Electronic Stimulation Control Characteristic
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties: Write
Value: 1 byte
4 Commands:
- Change the pulse frequency
- Change the electric current
- Right channels on/off
- Left channels on/off
114. GATT for generic commands
Electronic Stimulation Control Characteristic
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties: Write
Value: 1 byte
High-order 2 bits
- 00: Change the pulse frequency
- 01: Change the electric current
- 10: Left channels on/off
- 11: Right channels on/off
115. GATT for generic commands
Electronic Stimulation Control Characteristic
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties: Write
Value: 1 byte
High-order 2 bits
- 00: Change the pulse frequency
- 01: Change the electric current
- 10: Left channels on/off
- 11: Right channels on/off
117. How to define GATT
1. Create UUID for the service & characteristic
118. How to define GATT
1. Create UUID for the service & characteristic
$ uuidgen
CEEA31BC-BEAC-4A78-B7ED-FC96B6254D4C
119. How to define GATT
1. Create UUID for the service & characteristic
2. Define the properties (Read, Notify, Write, etc.)
$ uuidgen
CEEA31BC-BEAC-4A78-B7ED-FC96B6254D4C
120. 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
$ uuidgen
CEEA31BC-BEAC-4A78-B7ED-FC96B6254D4C
121. 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
$ uuidgen
CEEA31BC-BEAC-4A78-B7ED-FC96B6254D4C
122. 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
134. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
135. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
• Reading characteristics’ value
136. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
• Reading characteristics’ value
• Writing characteristics’ value
137. What functions can be used
in the background?
• Scanning peripherals
• Connecting with peripherals
• Reading characteristics’ value
• Writing characteristics’ value
• Receiving notifications
138. 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!
145. Limitations
• Longer intervals for scanning
• Must explicitly specify one or more services to scan
• CBCentralManagerOptionShowPowerAlertKey option is
ignored
146. 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)
147. If the app in the background
is killed by the system…
150. State Preservation and Restoration
The system takes over the BLE tasks
even after the app is killed.
151. 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.
152. 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.
153. 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!
156. • The app is killed by the system
• The user pushes the button on the
peripheral device
157. • The app is killed by the system
• The user pushes the button on the
peripheral device
• The system receives the notification
158. • 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.
159. 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.
160. 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!
162. Implementation
• Add an option when initializing the
CBCentralManager object.
let options = [
CBCentralManagerOptionRestoreIdentifierKey: "somekey"
]
centralManager = CBCentralManager(
delegate: self,
queue: queue,
options: options)
163. Implementation
• Implement the delegate method which is called when
the app is restored.
func centralManager(central: CBCentralManager,
willRestoreState dict: [String : AnyObject])
{
print("Restored")
}
167. 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
196. • Connection dropped
• Can’t discover services or characteristics
• Fail to write
• Incorrect characteristic values
197. • Connection dropped
• Can’t discover services or characteristics
• Fail to write
• Incorrect characteristic values
• Incorrect behaviors in the background
198. • Connection dropped
• Can’t discover services or characteristics
• Fail to write
• Incorrect characteristic values
• Incorrect behaviors in the background
• etc…
199. 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’
200. Can’t find the peripheral
• Scanning, but can’t find the peripheral device
201. Can’t find the peripheral
• Scanning, but can’t find the peripheral device
Scan
202. Can’t find the peripheral
• Scanning, but can’t find the peripheral device
Scan
but ‘didDiscover’
method isn’t called
203. Can’t find the peripheral
• Scan with another iOS app
Scan
204. Can’t find the peripheral
• Scan with another iOS app
Scan
Discovered peripherals
217. 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
e.g. Can’t see the ‘Manufacture Data’ field of iBeacon
241. Recap
• What is Bluetooth Low Energy?
• Basics of Core Bluetooth with an actual product
242. Recap
• What is Bluetooth Low Energy?
• Basics of Core Bluetooth with an actual product
• Practical Core Bluetooth with specific examples
243. Recap
• What is Bluetooth Low Energy?
• Basics of Core Bluetooth with an actual product
• Practical Core Bluetooth with specific examples
- Defining GATT
244. 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
245. 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
246. 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
247. 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