Android BLE, read and write characteristics

偶尔善良 提交于 2019-11-27 18:10:39

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!

xiao lu

Before setValue:characteristics.setValue(data) you should use gatt.setCharacteristicNotification(Char,true) to setNotification.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!