I'm working on an android BLE app. I have a BLE device which storing the coordinates of where the device went.
In order to get the data from the device after connected, we will need to send several commands and read its response. The steps are as follow:
Send first command to device
Read response (some data..)
Send second command to device
Read response (coordinates)
And this is how I did
After connected, sending a command to the device by using writeCharacteristic
When receiving GATT_SUCCESS in onCharacteristicWrite callback, call readCharacteristic to read its response
When receiving GATT_SUCCESS in onCharacteristicRead callback, continue to send second command to the device by using writeCharacteristic
When receiving GATT_SUCCESS in onCharacteristicWrite callback, call readCharacteristic to read its response
So here's the problem, when I issued readCharacteristic after write operation succeeded, the response is not expected as always and sometime it return the same command that I wrote. However, if I put some delay (e.g. 1 second) after I write, the response is correct when I read but the delay may different every time.
I am wondering if anyone having the same issue like this or anyone can explain to me why is this happening? Is that possible that we can know when is the correct time to read the characteristic?
Note: The device does not provide any notification/indication.
Related
i'm developping an android app that communicate with a BLE device(in kotlin).
i'm sending a command to the ble device, the device usually give the following response:
-OK(confirming the command was received and valid)
-one or multiple line of data
-Done
in onCharacteristicWrite i tryed to show this.value to read the response but look like it's not the way to do it. i googled a lot without lot of success.
so my question is how could i get the data from the response.the device send?
i try to call a readCharateristic without success either.
thank you for your help
GATT does not have any protocol feature of a request - response transaction where both messages embed data.
For Write Request - Write Response, only the request can contain data. The response can only be empty or an error code.
For Read Request - Read Response, the request contain only what characteristic is to be read and the response contains some data or an error code.
To implement a request - response mechanism using GATT, one usually follows one of the following strategies:
Use Write Request or Write Without Response for the request message and Notification for the response message.
Use Write Request or Write Without Response for the request message. When the GATT server produces a response message, it is put in a characteristic that the client will then read using a Read Request.
The first method has lowest overhead and best throughput.
What you should do is to investigate what method your peripheral actually uses. If it sends its responses in notifications, you must enable these and implement the onCharacteristicChanged callback.
After connecting to BLE device and discovering its services, I check that a certain characteristic is readable, then, I call readCharacteristic. This call returns true, but the callback onCharacteristicRead is not called.
After merely 30 seconds, the onCharacteristicRead is called with characteristic == null and the onDeviceDisconnected is called with status code equal to 22.
I have tested nRF Connecte to read the same characteristic and it worked fine.
What could possibly be wrong ?
The fact that it timeouts after 30 seconds and disconnects with error 22 (local device terminated the connection) indicates that the peripheral did not respond within 30 seconds, as required by the GATT standard. You should debug the peripheral to find the cause.
The reason it works in nRF Connect might be that the previous GATT sent / received put the peripheral in a different state.
You could check out the HCI log in Android to see all raw packets to maybe figure out what's going on.
I am invoking the writeCharacteristic both using the WRITE_TYPE_NO_RESPONSE and without it.Am I supposed to get the response out of both or one or none?
Is there any callback to catch this response?
Does onReliableWriteCompleted callBack ensures the successful delivery of the Message at the BLE device side(from the Phone) or does it implies that the message has been sent to the device(From the Phone) successfully and has been released in air.
If you use the WRITE_TYPE_NO_RESPONSE, you will get the onCharacteristicWrite callback as soon as the stack is ready and has space to accept a new request. This does not mean the other side has received it.
If you don't use WRITE_TYPE_NO_RESPONSE, you will get the onCharacteristicWrite callback as soon as the remote device has sent back a Write Response.
The onReliableWriteCompleted will be called when the remote side has acknowledged everything.
After successful ble connection, I am writing a Characteristic with DefaultWriteType.
Just after that onCharacteristicChanged triggers and after that onCharacteristicWrite triggers with same Characteristics UUID which was write but with values came as response in that Characteristics are same as response of onCharacteristicChanged.
Summary:
How onCharacteristicChanged triggers before onCharacteristicWrite ?
As I believe onCharacteristicWrite indicates that your write operation was successful or not.
And onCharacteristicChanged responds/notify to the command for which we write to ble device.
Is this possible or is it going out of Ble cycle?
I think this is expected.
Because onCharacteristicWrite only indicates, that your write operation was successful or not.
A BluetoothGattCharacteristic can hold only one value, which is the last one send or received.
Both operations are using the same BluetoothGattCharacteristic instance.
So regarding to your described sequence, it is "normal" that you have the same value in both callbacks. Since the last operation, which manipulates the value in your characteristic was a BLE notification.
I think that was not the best idea, how it was implemented in the BLE stack. It would be better if the operations would not interfere each other and would be more idempotent/immutable.
How do I get the write response in an android application of a write to a characteristic which has write response?
I can't find any callback for this. Only way I can think of is if onCharacteristicWrite status returns failed when the response is not received, difficult to confirm if it works like this though.
Note: I don't mean for Write Without Response, but the 4.9.3 Write Characteristic Value in Bluetooth Core Specification 5.0.
When onCharacteristicWrite is called with success status and the write type was Write With Response, that means you got the write response ;)
If no write response is received within 30 seconds the link gets disconnected.