Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical Core Bluetooth in IoT & Wearable proj...

Practical Core Bluetooth in IoT & Wearable projects

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?
- 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

shu223

May 26, 2016
Tweet

More Decks by shu223

Other Decks in Programming

Transcript

  1. Practical Core Bluetooth in IoT & Wearable projects Shuichi Tsutsumi

    @shu223 iOS Freelancer iOSCon 2016 - London
  2. • Low energy • NOT compatible with Classic BT •

    Uses 2.4 GHz radio frequencies • Marketed by the Bluetooth SIG
  3. • Low energy • NOT compatible with Classic BT •

    Uses 2.4 GHz radio frequencies • Marketed by the Bluetooth SIG • etc…
  4. • Low energy • NOT compatible with Classic BT •

    Uses 2.4 GHz radio frequencies • Marketed by the Bluetooth SIG • etc…
  5. The API ‘Core Bluetooth’ is open for developers • API

    for Classic BT is NOT open. - Requires MFi certification.
  6. BLE is almost the ONLY way to enable iOS apps

    to communicate with external hardware without infrastructure or MFi.
  7. Moff Band Moff App Sensor Data BLE Connection Sensors -

    Gyroscope - Accelerometer Analyze sensor data
  8. Moff Band Moff App Sensor Data BLE Connection Sensors -

    Gyroscope - Accelerometer Analyze sensor data Recognize - Gesture - Posture
  9. Moff Band Moff App Sensor Data BLE Connection Sensors -

    Gyroscope - Accelerometer Analyze sensor data Recognize - Gesture - Posture Play sounds
  10. Scan Step 1. Scan Advertise : Search for nearby BLE

    devices centralManager.scanForPeripheralsWithServices(nil, options: nil)
  11. 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!”) }
  12. 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
  13. 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
  14. Moff Service xx Service Step 3. Subscribe: Ready to receive

    data GATT = Generic Attribute Profile
  15. Moff Service xx Service Button Characteristic xx Characteristic Sensor Characteristic

    Step 3. Subscribe: Ready to receive data GATT = Generic Attribute Profile
  16. 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
  17. 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)
  18. 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!”) }
  19. 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!”) }
  20. BLE

  21. → Can be used even in areas with poor coverage!

    Group conversation system with VoIP - Detects the human voice - Cuts out all background noise
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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 Y1VTIFE Y3FMFBTFE
  35. 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)
  36. 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)
  37. 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
  38. 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
  39. 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
  40. 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 - OnOff the right channels - OnOff the left channels
  41. 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 - OnOff the right channels - OnOff the left channels
  42. 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: OnOff the left channels - 11: OnOff the right channels
  43. 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: OnOff the left channels - 11: OnOff the right channels
  44. How to define GATT 1. Create UUID for the service

    & characteristic $ uuidgen CEEA31BC-BEAC-4A78-B7ED-FC96B6254D4C
  45. 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
  46. 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
  47. 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
  48. 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
  49. Background behaviors on iOS • Very limited - Listening to

    music - Getting location data - Downloading data
  50. You would have to always keep the app in the

    foreground, while snowboarding!
  51. What functions can be used in the background? • Scanning

    peripherals • Connecting with peripherals
  52. What functions can be used in the background? • Scanning

    peripherals • Connecting with peripherals • Reading characteristics’ value
  53. What functions can be used in the background? • Scanning

    peripherals • Connecting with peripherals • Reading characteristics’ value • Writing characteristics’ value
  54. What functions can be used in the background? • Scanning

    peripherals • Connecting with peripherals • Reading characteristics’ value • Writing characteristics’ value • Receiving notifications
  55. 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!
  56. Limitations • Longer intervals for scanning • Must explicitly specify

    one or more services to scan • CBCentralManagerOptionShowPowerAlertKey option is ignored
  57. 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)
  58. 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.
  59. 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.
  60. 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!
  61. • The app is killed by the system • The

    user pushes the button on the peripheral device
  62. • The app is killed by the system • The

    user pushes the button on the peripheral device • The system receives the notification
  63. • 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.
  64. 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.
  65. 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!
  66. Implementation • Add an option when initializing the CBCentralManager object.

    let options = [ CBCentralManagerOptionRestoreIdentifierKey: "somekey" ] centralManager = CBCentralManager( delegate: self, queue: queue, options: options)
  67. Implementation • Implement the delegate method which is called when

    the app is restored. func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) { print("Restored") }
  68. Testing the restoration • Kill the app as if it

    was killed by the iOS kill(getpid(), SIGKILL);
  69. 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
  70. Emulator App • Develop another iOS app as the alternative

    to the peripheral device • Use CBPeripheralManager • Easier for iOS engineers
  71. =

  72. • Connection dropped • Can’t discover services or characteristics •

    Fail to write • Incorrect characteristic values • Incorrect behaviors in the background • etc…
  73. 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’
  74. Can’t find the peripheral • Scanning, but can’t find the

    peripheral device Scan but ‘didDiscover’ method isn’t called
  75. 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
  76. 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
  77. Videos for review • Show how the app works with

    the peripheral device • Not need to be cool!
  78. Recap • What is Bluetooth Low Energy? • Basics of

    Core Bluetooth with an actual product
  79. Recap • What is Bluetooth Low Energy? • Basics of

    Core Bluetooth with an actual product • Practical Core Bluetooth with specific examples
  80. Recap • What is Bluetooth Low Energy? • Basics of

    Core Bluetooth with an actual product • Practical Core Bluetooth with specific examples - Defining GATT
  81. 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
  82. 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
  83. 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
  84. 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
  85. ଋɿ31mm ද 4 ද 1 iOSʷBLE Core Bluetooth ϓϩάϥ ϛ

    ϯά iOSʷBLE Core Bluetooth ϓ ϩ ά ϥ ϛ ϯ ά అ म Ұ দ ଜ ྱ ԝ ử ஶ అमҰʷদଜྱԝ ʹ ஶ Shuichi TSUTSUMI ʷ Reo MATSUMURA iOSʷBLE Core Bluetooth Programming iOSʷBLE 定価:本体4,000円 (税別) Resources
  86. ଋɿ31mm ද 4 ද 1 iOSʷBLE Core Bluetooth ϓϩάϥ ϛ

    ϯά iOSʷBLE Core Bluetooth ϓ ϩ ά ϥ ϛ ϯ ά అ म Ұ দ ଜ ྱ ԝ ử ஶ అमҰʷদଜྱԝ ʹ ஶ Shuichi TSUTSUMI ʷ Reo MATSUMURA iOSʷBLE Core Bluetooth Programming iOSʷBLE 定価:本体4,000円 (税別) Resources • 500 pages about iOS x BLE
  87. ଋɿ31mm ද 4 ද 1 iOSʷBLE Core Bluetooth ϓϩάϥ ϛ

    ϯά iOSʷBLE Core Bluetooth ϓ ϩ ά ϥ ϛ ϯ ά అ म Ұ দ ଜ ྱ ԝ ử ஶ అमҰʷদଜྱԝ ʹ ஶ Shuichi TSUTSUMI ʷ Reo MATSUMURA iOSʷBLE Core Bluetooth Programming iOSʷBLE 定価:本体4,000円 (税別) Resources • 500 pages about iOS x BLE • Japanese only
  88. 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
  89. 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.
  90. 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
  91. 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…
  92. Thank you! Shuichi Tsutsumi - iOS Freelancer • Twitter: @shu223

    • GitHub: shu223 • Blog: https://medium.com/@shu223/ • Email: [email protected]