Almost everyone has a smart device nowadays. A good portion of them is apple devices. Therefore, it is likely that you will find yourself working on projects that involve communication with Apple devices beyond an app.
For this post, my goal is to introduce you to Bluetooth Low Energy (BLE) communication between the iPhone and custom hardware using BLE. This post will focus at the higher level meaning there will not be any code examples.
Terminologies
Let us get the terminologies out of the way. BLE has many terminologies, which can make you feel overwhelmed. Among the many terms associated with BLE, there are a few that I believe are essential for you to understand.
Central
A BLE central mode device acts as the client. The client wants data from the peripheral (server). The central is almost always going to be the iPhone.
Peripheral
A BLE peripheral mode device acts as the server. It has data to give to its client. The peripheral is almost always the custom hardware device.
Service
A service breaks data up into logic entities. A service holds specific chunks of data known as characteristics. A service can have multiple characteristics. In addition, each service has a UUID (universally unique identifier). Officially adopted services have 16-bit long UUID, while custom services have 128-bit long UUID.
Characteristic
A characteristic encapsulates a single data point. Similar to a service, each characteristic has an UUID (16-bit for official and 128-bit for custom). A characteristic is the main point where your app will interact with custom hardware. Your app can read and or write to a characteristic depending on if it is writeable. Characteristics are usually readable by default.
Core Bluetooth Framework
When developing your iOS app, you will be working with the Core Bluetooth framework. The framework handles most of the intricate details that are part of BLE to allow you to develop as fast as possible. The Core Bluetooth framework provides you with the objects that represent each component part of BLE communication.
Main Objects
The main components in BLE communication are the Central and Peripheral. The Bluetooth Core framework provides an object representation of each.
CBCentralManager
The class part of the Bluetooth Core framework that represents a central. This object is where your app scans for peripherals and their advertisement data and can connect to peripherals.
CBPeripheralManager
The class part of the Bluetooth Core framework that represents a peripheral. This object is where your app communicates with the peripheral’s services and characteristics.
Data Objects
The Bluetooth Core framework provides object representation for Service and Characteristic of a peripheral.
CBService
CBService is the representation of a service pertaining to a connected peripheral. Your app can find out specifics about the peripheral’s service such as its characteristic(s) through this object.
CBCharacteristic
CBCharacteristic is the representation of a characteristic pertaining to a service of a connected peripheral. Your app can communicate with the peripheral by reading values from and writing values to this object. Do note that the BLE version your hardware supports will affect communication speed.
BLE Procedure
The setup process for BLE communication between your iOS app (central) and different peripherals is the same. The difference comes after you start reading and interpreting the data from the peripheral. The connection procedure consists of three main steps.
1. Discovery and Connection
In the discovery and connection phase, the peripheral(s) sends out advertisement data. When the central (your app) sees one that it is interested in then it will try to connect with the peripheral. Here is a visual representation of what happens with the CBCentralManager and Delegate during this phase:
2. Exploration
In the exploration phase, the central and peripheral has established a connection successfully. This means you can start to see what service(s) the peripheral offers. If there is a service that is of interest, you can go deeper and check all the characteristics and even subscribe to some characteristics for updates. Here is a visual representation of what happens with CBPeripheral and Delegate during the exploration phase:
3. Interaction
In the interaction phase, the central has found at least one characteristic of interest. The central has two options of interacting with the characteristic(s). One is to read the value from the characteristic on its own. The second is to subscribe to the characteristic(s) to get an update when the value changes at the characteristic end. Here is a visual representation of what happens with CBPeripheral and Delegate during the interaction phase:
Additional Resources
- How to tackle learning a new framework
- The learning process for a software developer
- How to learn efficiently
I hope you found this post helpful. If you found this post helpful, share it with others so they can benefit too. To stay in touch, follow me on Twitter.
Pingback: iOS Bluetooth Low Energy and Custom Hardware - Part 2: Communication - bright developers
Pingback: iOS Bluetooth Low Energy and Custom Hardware - Part 3: Optimizing Data Throughput - bright developers