Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BLE, read and write characteristics

People also ask

How do Android apps communicate with custom BLE?

Create a new project Open Android Studio and you should be greeted with the following screen. Select Import an Android code sample. On the next screen select the sample Bluetooth Le Gatt under Connectivity. This project will set us up with a framework to build off of for our application.

What is GATT characteristic?

A GATT characteristic is a basic data element used to construct a GATT service, BluetoothGattService . The characteristic contains a value as well as additional information and optional GATT descriptors, BluetoothGattDescriptor .

What are BLE settings?

Android provides built-in platform support for Bluetooth Low Energy (BLE) in the central role and provides APIs that apps can use to discover devices, query for services, and transmit information. Common use cases include the following: Transferring small amounts of data between nearby devices.


Each of the callback from the Android BLE has its functions;

onDescriptorRead and onDescriptorWrite

This is used to write/read the configuration settings for the BLE device, some manufactures might require to send some data to the BLE device and acknowledge it by reading, before you can connect to the BLE device

onCharacteristicWrite

This is used to send data to the BLE device, usually in data mode for the BLE device. This callback is called when you type

gatt.writeCharacteristic(characteristics);

onCharacteristicRead

This is used to read data from the BLE device The callback is called when you write this code

gatt.readCharacteristic(characteristics);

onCharacteristicChanged

This callback is called when you are trying to send data using writeCharacteristic(characteristics) and the BLE device responds with some value.

Usually a BLE device has few characteristics, to make it simple, I name a few characteristics

  • WRITE - write Characteristics
  • READ - read Characteristics

To make it clear, when you send data, you will need to use WRITE characteristics and then when the BLE device responds Android app will call READ characteristics

A very important point to note is Android BLE stack allows you to write characteristics one at a time only!!

Example: IF you try to call write characteristics twice at a same time

gatt.writeCharacteristic(characteristics);
gatt.writeCharacteristic(characteristics);

The Android BLE stack will not issue the 2nd write characteristics!