URL : http://www.icapps.com/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 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
.....
CoreBluetooth
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: https://developer.apple.com/library/ios/#documentation/CoreBluetooth/Reference/CoreBluetooth_Framework/_index.html
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];
}
IMPORTANT
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.
.....