Bluetooth Low Energy Communication In IOS

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 16-bit 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:

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

      [bluetoothCentralManager scanForPeripheralsWithServices:nil options:nil];

2) Then connect to your BLE peripheral in the list:

      [bluetoothCentralManager connectPeripheral:yourCBPeripheral options:nil];

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

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

4) Discover all services available in BLE peripheral by the following method:

  [self.yourCBPeripheral discoverServices:nil];

5) Then respective listeners will be alerted.

     (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

for (CBService *service in peripheral.services) {

[peripheral discoverCharacteristics:nil forService:service];

}

6) 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];

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

  (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error: (NSError *)error {

NSLog(@”Response  %@”,characteristic.value);

}

8) After successful data communication disconnects the peripheral from the mobile.

  [bluetoothCentralManager cancelPeripheralConnection:yourCBPeripheral]

Leave a Comment

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

Scroll to Top