android BLE write command with response, no response receive - android

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.

Related

How to handle a connection issue during a .GET request?

I have an android app which do some .GET calls by using Ion library.
The server on each call returns a web page which i will then format and transform in real object data.
When the server receive the .GET request it locks the access to that request.
So if i make the .GET to /example/?PAGE=1 the first request will return all needed data while if i will try to get it again i will receive a string like LOCKED until i will send another request like /example/?UNLOCKPAGE=1
The main issue of that is that the content of the page coult be very large and the device which is trying to fetch it could have a very bad network connection from which he could be disconnected at any time.
So if i make the .GET request and the device immediatly goes offline the server will lock the page but in facts the device didn't received that data.
How could a situation like this be solved?
I tought about a situation where on the .GET request the content is not locked until the device didn't send another request that confirms that he get all the data, but at this point what will happen if two devices will try to get both the same data and one of them get it first, will both of them get the data? Is that the right way?

Getting Response for Android BLE Write Characteristic

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.

Android BLE write response

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.

Android http json response is coming in pieces

I have a problem - I am trying to make a POST request from my app, but it always drop IOexception error on response(like response code, message etc...).
I made that POST request (what i am trying to make from my app) from my PC and I used WIRESHARK to see the response, but the response comes in multiple PIECES, not in one as usual.
In my app I use httpulrconnection in acync task.
How do I manage to catch all of the response?
I've added a pics of my WIRESHARK file and make it red which contains the response:
Wireshark http post with response in pieces
This is just how anything over a networks is working.
The data is splitted into smaller packets which will get routed through the network. The maximum of the packet size is defined by the different hops in the network.
In most cases those packets are something like 1460bytes of actual payload.
TCP will make this packet-handling transparent, as it is a protocol on top of this raw layer. So normally you shouldn't take care of this.
If you want to see the whole response (like your application will see it), you can view single tcp streams in wireshark:
https://www.wireshark.org/docs/wsug_html_chunked/ChAdvFollowTCPSection.html
Edit
The errors you are seeing don't have anything to do with the packaging.

Response from readCharacteristic is not always as expected after issue writeCharacteristic

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.

Categories

Resources