Short question:
When BluetoothLECallbackInterface.onCharacteristicRead()
is triggered, how do I know which request this is an answer to?
A bit more detailed:
when calling BluetoothGatt.readCharacteristic()
, the onCharacteristicRead()
method of the BluetoothGattCallback
object I passed when connecting the device will be called
(the one I passed to BluetoothDevice.connectGatt()
)
Now let's imagine I'm making ten different readCharacteristic() calls.
onCharacteristicRead()
will be triggered 10 times.
How can I know which callback-call belongs to which request?
How or when or to which class/method could I pass a request ID, when calling readCharacteristic()
, on which I could rely in onCharacteristicRead()
to identify the request I'm just being handling the result?
I'm just discovering bluetooth connection and communication, maybe my question makes no sense, maybe I'm supposed to be waiting for a request to be finished before I start calling a new request?
But when using asynchronous calls, I assume I can make all the calls I want to, then sort out the responses, because something in the response tells me which request it is an answer to.
This "something in the response" is what I can't find anywhere in android documentation.
The contrary would be like having to wait the delivery confirmation of an email before being able to send a new one, without risking not to know which one was delivered... :-(
A scenario maybe more convincing with writing characteristics.
Imagine I want to give a bluetooth device some values he then has to store, sum or whatever, device-side.
I send 1, 3, 4, 2 and 7 to this device.
Let's say onCharacteristicWrite()
is triggered 4 times with success and one time with failure status.
How can I know which request hasn't been taken in account device-side?
Simultaneous reads or writes are not supported. You need to wait for a callback before starting next request. A typical implementation uses a Queue to simplify it.
Check this: https://stackoverflow.com/a/21358451/1860868