33

I am currently developing Android BLE, and encounters a lot of problems with the Android BLE stacks..

My development tool is Samsung Galaxy J with Android 4.3.

I want to know how can I read a characteristics from the BLE and the write the characteristics (is like i verify what data I have received, and then I send another data using the BLE)

and I have serious problem understanding how the Android BLE callbacks works, I dont understand these 5 functions...and the manual is not clear, can anyone good soul explain in simple form???

onCharacteristicWrite
onCharacteristicRead
onCharacteristicChanged
onDescriptorRead
onDescriptorWrite

My current situation is, I managed to read the data in onCharacteristicChanged() callback and then I verified the received the data I try to send the data by using

characteristics.setValue(data)
gatt.writeCharacteristic(characteristics)

But, the Android BLE stack is not calling onCharacteristicsWrite() and in fact, Android just hangs there..

I try to google about Android BLE, there is not much information and only bunch of complains on how unstable the BLE stacks is......

Tim
  • 3,755
  • 3
  • 36
  • 57

2 Answers2

56

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!

  • 1
    I have made a wrapper function for `writeCharacteristic` for each certain commands I sent. Each of these are triggered by both the users and the app itself. Should I labeled all these functions `synchronized` to avoid stacking more than one `writeCharacteristic`? – mr5 Aug 18 '15 at 03:13
  • 1
    Yes.. either `synchronized` or calling them after some delay between each request – Kushal Sep 01 '15 at 08:56
  • Hi @Kushal!. How much delay? – Marie Amida May 11 '17 at 16:15
  • 2
    @Marie Amida The delay depends on buffer processing capability of ble device hardware, this way is not full-proof, you may be have to change delay anytime in future, so it is better to go with `synchronized` way, i will suggest – Kushal May 15 '17 at 06:36
  • I would like to understand this more: "A very important point to note is Android BLE stack allows you to write characteristics one at a time only!!". Is there a source for this in the official docs? Is that limit per-characteristic, per-device, or? It seems to imply I need to wait for onCharacteristicWrite before making another call to writeCharacteristic but I don't understand how using synchronized would address that. – Frank Schwieterman Apr 25 '18 at 17:52
  • 1
    _"A very important point to note is Android BLE stack allows you to write characteristics one at a time only!!"_ Is this also true when BLE write type is "WRITE_TYPE_NO_RESPONSE"? – Will Wang Aug 15 '18 at 06:59
-3

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
xiao lu
  • 1
  • 1