So You Wanna Be A Bluetooth Samurai
~ Bluetooth 101 for App Developers ~
Created by
Lijun
, Feb 2018
(
View the slides in one piece
and print)
## Roadmap * I. [What is Bluetooth](#/what-is-bluetooth) * II. [How We Get Here](#/history) * III. [Protocol Stack](#/protocol-stack) * IV. [Roles](#/roles) * V. [Profiles](#/profiles) * VI. [Connectivity](#/connectivity) * VII. [Bluetooth on Android and iOS](#/android-ios) * VIII. [Debugging](#/debugging) * IX. [Challenges](#/challenges)
# I. What is Bluetooth
<--- Back to roadmap
## I. What is Bluetooth * "Bluetooth is a cable-replacement technology designed to wirelessly connect peripherals, such as mice and mobile phones, to your desktop or laptop computer and to each other." (Quoted from OS X [Bluetooth Technology Basics](https://developer.apple.com/library/content/documentation/DeviceDrivers/Conceptual/Bluetooth/BT_Bluetooth_Basics/BT_Bluetooth_Basics.html))
## I. What is Bluetooth * Also widely used in IoT space (Bluetooth Smart / Bluetooth Low Energy) * Using 2.4~2.485 GHz ISM band (unlicensed but regulated)
## I. What is Bluetooth
Popular IoT protocol with balance among power and data rate.
Image source: [learn.spark.com](https://learn.sparkfun.com/tutorials/bluetooth-basics)
# II. How We Get Here
<--- Back to roadmap
## II. How We Get Here
(Major events)
| Year | Event | | ---- | ----- | | 1994 | Origin | | 1998 | Bluetooth Special Interest Group (SIG) formed | | 1999 | Bluetooth V1.0: Basic Rate (BR); followed by V1.1, V1.2 | | 2004 | Bluetooth V2.0: Enhanced Data Rate (EDR); followed by V2.1 |
Bluetooth was named after a [king](https://en.wikipedia.org/wiki/Harald_Bluetooth) who unified the Nordic countries 1000+ years ago
## II. How We Get Here
(Major events continued)
| Year | Event | | ---- | ----- | | 2009 | Bluetooth V3.0: High Speed (HS) | | 2010 | Bluetooth V4.0: Low Energy; IoT; followed by V4.1, V4.2 | | 2016 | Bluetooth V5.0: enhanced IoT (data rate, advertising, [mesh](https://blog.bluetooth.com/introducing-bluetooth-mesh-networking) etc.) |
# III. Protocol Stack
<--- Back to roadmap
## III. Protocol Stack * Host-Controller Model - Divided by host-controller interface (HCI) - Controller is usually provided by chip vendor as a closed source binary - Host is often the SDK developers can work on
## III. Protocol Stack
(Very high-level)
# IV. Roles
<--- Back to roadmap
## IV. Roles * Master & Slave - Master initiates the connection and updates the connection parameters - One master can connect to multiple slaves - One slave can connect to one (Piconet) or multiple (Scatternet) masters
## IV. Roles * Master & Slave Piconet
## IV. Roles * Central & Peripheral - BLE (almost) equivalence of Master & Slave
## IV. Roles * Server & Client - Server contains database - Client interacts with the database on the server - Example: SDP, GATT
## IV. Roles * Server & Client
# V. Profiles
<--- Back to roadmap
## V. Profiles Profile: a specification (protocol, data structure, etc.) of a certain Bluetooth feature
## V. Profiles * Common BR/EDR profiles - Music playback: Advanced Audio Distribution Profile (A2DP) - Phone call, voice message: Hands-free Profile (HFP) - Media control (music, phone call, etc.): Audio/Video Remote Control Profile (AVRCP) - Data streaming: Serial Port Profile (SPP) - Contact access: Phone Book Access Profile (PBAP)
## V. Profiles * BLE related - Bluetooth connectivity: Generic Access Profile (GAP) - Attribute data interaction: Generic Attribute Profile (GATT) * Full [list of Bluetooth profiles](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles)
## V. Profiles GATT plays a crucial role in BLE application. Let's talk about it a little more ...
## V. Profiles: GATT * A GATT profile is made of functionally related GATT services * A GATT service is made of one or a few GATT characteristics and maybe also descriptors
## V. Profiles: GATT
Example: GATT profile, services and characteristics
Image source: [docs.mbed.com](https://docs.mbed.com/docs/ble-intros/en/latest/Introduction/BLEInDepth/)
## V. Profiles: GATT * A GATT characteristic is made of a few attributes * A GATT descriptor is made of a few attributes that configure the application behavior of a GATT characteristic * An attribute is made of a handle, a UUID, and a value: the atomic unit of GATT
## V. Profiles: GATT
Example: GATT service and characteristics template
Image source: [docs.mbed.com](https://docs.mbed.com/docs/ble-intros/en/latest/Introduction/BLEInDepth/)
# VI. Connectivity
<--- Back to roadmap
## VI. Connectivity Two core use cases: First-time connection and Reconnection
## VI. Connectivity BR/EDR first-time connection
BR/EDR pairing is mandatory
## VI. Connectivity BR/EDR reconnection
## VI. Connectivity BLE first-time connection
BLE bonding is optional for a connection
## VI. Connectivity BLE reconnection
GATT discovery is optional (e.g. iOS)
# VII. Bluetooth Mobile App
<--- Back to roadmap
## VII. Bluetooth Mobile App Too much low-level techies thus far. I want to build a Bluetooth phone app. Programmers, I hear you...
## VI. Android BR/EDR Flow
## VI. Android BLE Flow
## VI. iOS BR/EDR If the device is MFi certified
Image source: Internet
## VI. iOS BR/EDR If the device is NOT MFi certified... LSS, you cannot do much.
Image source: Internet
## VI. iOS BLE Flow
## VI. A Tale of Two OSes With the love from Android and iOS, Bluetooth is a harmonious romance
## VI. A Tale of Two OSes Except it is not
## VI. A Tale of Two OSes | | Android | iOS | | ---------- | --------- | ------------- | | BR/EDR MAC | Available | Not available | | BR/EDR profile data | Available | Not available except for audio routing | | BLE ADV data | Available | Not available | | SPP | Available | MFi needed (iAP) | | Beacon technology | Eddystone | iBeacon |
## VI. A Tale of Two OSes * Android Bluetooth: Good and bad - Lots of freedom compared with iOS - Fragmented implementation - Vendor-specific quirks; huge interoperatibility (IOP) problem
## VI. A Tale of Two OSes * iOS Bluetooth: Good and bad - Worry less about the IOP - iBeacon is nice (e.g., keep iOS app alive) - Restricted: dual-mode devices will hit MFi wall eventually
# VIII. Debugging
<--- Back to roadmap
## VIII. Debugging Something is wrong with my Bluetooth, Murphy said...
## VIII. Debugging: Mobile app level * Log.d in various places * Stacktrace analysis
## VIII. Debugging: Mobile OS level * Bluetooth JNI messages in Android * Bluetooth HCI snoop log * Log analysis tools: Frontline Protocol Analysis System, Wireshark * [Profiles and logs on iOS](https://developer.apple.com/bug-reporting/profiles-and-logs/?platform=ios)
## VIII. Debugging: Device firmware * Firmware local log (equivalence of “Log.d in various places”) * HCI log dump (vendor support)
## VIII. Debugging: Over-the-air
(If you have a deep pocket)
* Bluetooth sniffer (e.g. Ellisys) to dump airtrace
# IX. Challenges & Lessons
<--- Back to roadmap
## IX. Challenges / Lessons Be ready for this when working with Bluetooth
## IX. Challenges / Lessons * Connectivity is not something taken for granted
## IX. Challenges / Lessons * Interoperatibility (IOP) is not something taken for granted
## IX. Challenges / Lessons * Pairing/bonding process has a high UX impact
## IX. Challenges / Lessons * OTA DFU takes time and effortsto stabilize
## IX. Challenges / Lessons * HCI log is really helpful
## IX. Challenges / Lessons * Phone OS nuances (Android and iOS) - Execute GATT tasks on UI thread in Android - BluetoothGatt is not thread-safe in Android. Use a queue to schedule GATT tasks.
## Additional Links * [Bluetooth Core Specs 5.0](https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=421043): the Bible of Bluetooth * [Wikipedia Bluetooth page](https://en.wikipedia.org/wiki/Bluetooth) and [a short history of Bluetooth](https://www.nordicsemi.com/eng/News/ULP-Wireless-Update/A-short-history-of-Bluetooth) * [Bluetooth Technology Basics](https://developer.apple.com/library/content/documentation/DeviceDrivers/Conceptual/Bluetooth/BT_Bluetooth_Basics/BT_Bluetooth_Basics.html) (OS X Bluetooth developing notes) * [Ten Important Differences Between Bluetooth BREDR And Bluetooth Smart](https://blog.bluetooth.com/ten-important-differences-between-bluetooth-bredr-and-bluetooth-smart)
## Additional Links * UW CSE466 Bluetooth overview [slides](https://courses.cs.washington.edu/courses/cse466/01au/Projects/BlueToothOverview.ppt) * [Bluetooth: ATT and GATT](https://epxx.co/artigos/bluetooth_gatt.html) * [BLE modes and profiles](https://docs.mbed.com/docs/ble-intros/en/latest/Introduction/BLEInDepth/) (mBed docs) * [Core Bluetooth Programming Guide](https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html) (iOS)
## The End Comments? Errors? [Leave me a note](http://www.lij.uno/contact)! [View the slides in one piece](?print-pdf) and print (Ctrl+P) [Start over](#/cover)