CoreBluetooth unraveled

CoreBluetooth unraveled

Since the release of iOS 5, Apple offers a framework that provides access to Bluetooth 4.0 low energy devices. The framework is a wrapper around ATT, GATT and L2Cap (which all are the default Bluetooth protocols).
An app developer shouldn’t know all these protocols, he/she should only use the framework and don’t bother about these low level protocols.

In this post, I’m going to talk about Bluetooth 4.0 and its benefits to the older versions of Bluetooth, followed by a brief intro to what corebluetooth can contribute to your app.

Bluetooth 4.0 LE

4.0… This means that the standard has been changed several times. IT also means that the technology is way more stable, and less energy consuming.
So what’s the big idea in Bluetooth 4.0? => Use very little energy.
How is this accomplished?
- Less time in the air -> broadcast in intervals
- Less energy is used when on the air -> more efficient energy management
- A completely new architecture (hardware level)

Even though this looks great, the disadvantage is that the max data rate per second is 7 times smaller with the low energy devices than on classic Bluetooth 3.0 devices.

Bluetooth 4.0 key terms

Client/ Server

A client in Bluetooth is the one who wants data, so it can be processed(and shown to the user).
Device: iPhone

Server (Peripheral)

A server in Bluetooth is the one who has the data and transmits it.
Device: Device which offers an external Bluetooth interface. For example a heart rate meter or a temperature sensor

Connecting client and server

Step 1. Advertising

Advertising is done by broadcasting packages on a given time interval. A small interval has the advantage that the broadcaster is discovered instantly but uses more energy.
Why is advertising done in intervals? So that the device isn’t broadcasting all the time and uses even more energy

© Apple

Step 2. Setting up the connection

After the advertising process, an observer can send a connection request to a certain broadcaster. When the connection is accepted, observer becomes Central, broadcaster becomes Peripheral. Both devices can now start sending data to each other, this also happens with a certain interval, again to reduce energy consumption.

© Apple

Services/ Characteristics


A service in Bluetooth is the interface that is used to communicate to external devices.
For example: a temperature sensor has a service :“Temperature Sensor”


A characteristic in Bluetooth is something that the service provides.
For example: The temperature sensor – service has 2 characteristics:
- provides a characteristic that tells the temperature in the room
- provides a characteristic that contains general info of the device (battery level, manufacturer,…)

When drawing this structure you get the following hierarchy:


    • Service
    • Service

With this short introduction to how Bluetooth 4.0 actually works, you will see that CoreBluetooth also uses the same terminology.


CoreBluetooth was introduced together with iOS5, but now with the release of iOS6, Apple expanded this library with the following very important functionality:
-in iOS5, an iDevice could only be a Central and never a Peripheral. But with the new API in iOS6, this all changes. Now iDevices can offer Bluetooth services with their own characteristics.
This feature might come in handy when an iDevice serves as a manager for multiple external Bluetooth devices.

iOS5 CoreBluetooth object model:
Main objects: CBCentralManager , CGPeripheral
Data objects: CBService,CBCharacteristic
Helper objects: CBUUID

iOS6 CoreBluetooth object model:
Main objects: CBCentralManager , CGPeripheral,CBPeripheralManager,CBCentral
Data objects: CBService,CBCharacteristic,CBMutableService,CBMutableCharacteristic
Helper objects: CBUUID,CBATTRequest

For all the other class references:

Code example

This example writes a value to an external Bluetooth interface

Step 1. Setup CBCentralManager

CBCentralManager: to start discovering and connection your app to peripherals, a new instance of CGCentralManager is made and configured.

CBCentralManager *manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

- Make sure that your class is a delegate of the CBCentralManager. This delegate gets called for example when a new peripheral is discovered.
- nil is passed to the ‘queue’ argument, corebluetooth will spot this and makes sure that the code is executed on the main queue.

Step 2. Scan for devices
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[manager scanForPeripheralsWithServices:nil options:dictionary];
Step 3. Process peripherals and pick random one
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{

            // chose peripheral and connect
            [manager connectPeripheral:[perpherals objectAtIndex:0]options:[NSDictionary dictionary]];
Step 4. Get notified when connection with peripheral is complete and write a value to a characteristic on the peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{

 //Write value to a characteristic
int i = 1;
[peripheral writeValue:[NSData dataWithBytes:&i length:sizeof(i)] forCharacteristic:[[service characteristics ] objectAtIndex:0] type:CBCharacteristicWriteWithoutResponse];

This code just uses random peripherals,services and characteristics. When you connect to your own Bluetooth interface, all the previous objects have to be checked.
Services have to be checked on their name, characteristics on their UUID.

Thanks for reading!