BLE Communication in iOS

Posted by   June 20th, 2017

Facebooktwitterlinkedinmail

BLE or Bluetooth Low Energy which more commonly referred to as ‘Smart Bluetooth’, is a device communication method that uses radio waves instead of cables to connect with mobiles and computers.  The latest entrant in the Bluetooth technology is Bluetooth 5, with enhancement made for IoT. Bluetooth 5 comes with a lot of advantages and very few disadvantages.

Pros:

  • It can be used without wires and cables

  • 90% of mobiles in the market are BLE enabled, which help in data transfer with multiple accessories (eg: Headset,Printers, TV remotes etc)

  • The battery consumption is low

Cons:

  • Cannot be used beyond 100 meters

  • The data transfer is slow

The different bluetooth protocols are:

  • Generic Access Profile (GATT)

  • Generic Attribute Profile (GAP)

GATT stands for the Generic Attribute Profile. It is two-way data transfer between two BLE devices using Services and Characteristics. It uses Attribute Protocol (ATT) to store services and characteristics in 16bits format.  Each BLE peripheral can connect to one central device(Android mobile or iOS mobile). So, when a peripheral connection is successful, it will stop advertising and another device will no longer see the BLE peripheral.

Work Flow

  • Search

  • Connect

  • Transfer data

  • Disconnect

This is how you can connect the bluetooth to an iOS device:

Search for the available BLE peripheral around you using below code in iOS:

[bluetoothCentralManager scanForPeripheralsWithServices:nil options:nil];

Then connect to your BLE peripheral in the list:

[bluetoothCentralManager connectPeripheral:yourCBPeripheral options:nil];

The moment the connection is successful, following message will be triggered

(void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;

Discover all services available in BLE peripheral by following method:

[self.yourCBPeripheral discoverServices:nil];

Then respective listeners will be alerted.

(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}

Then comes the communication part for BLE peripheral:

Send data to BLE  peripheral

CBUUID *servieuuid= [CBUUID UUIDWithString:FTServiceConstant];
CBUUID *writeChar= [CBUUID UUIDWithString:FTWriteCharacteristic];
;

Note: This may vary based on the hardware vendor

uint8_t  sendMsgpackets[] = {16 bytes data packet};
NSMutableData *dataWithChecksum = [NSMutableData dataWithBytes:sendMsgpackets length:16];
[yourCBPeripheral writeValue:dataWithChecksum forCharacteristic:deviceWriteCharacteristic type:CBCharacteristicWriteWithResponse];

Then you may monitor the response from the hardware in the below listener convert the 16 bytes of data hex to decimal received from above listener

(void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"Response  %@",characteristic.value);
}

After successful data communication disconnects the peripheral from the mobile.

[bluetoothCentralManager cancelPeripheralConnection:yourCBPeripheral]

References:   https://learn.adafruit.com/introduction-to-bluetooth-low-energy/gatt

Leave a Reply

Your email address will not be published. Required fields are marked *