Android Question Bluetooth serial

yo3ggx

Active Member
Licensed User
Longtime User
Hi all,

I'm using the Serial library version 1.23 to communicate over Bluetooth with a device.
My application send a byte array and expects to receive 5 bytes from the device.
I'm using AsyncStream. In the callback function (Astream_newdata) the 5 bytes are received sometimes in a single call, sometimes in two calls. I can rebuild the data based on the expected number of bytes to be received. Unfortunately, when I receive the data in two chunks, sometimes the two chunks are reversed (second one is received first) which makes impossible to rebuild the data in the right way.

As an example:
I expect the following byte sequence:
00,19,02,00,00

Sometimes I receive:
00 then 19,02,00,00
but somethimes I receive:
19,02,00,00 then 00

Any idea about the root cause of this behavior?

Thank you,
Dan
 

yo3ggx

Active Member
Licensed User
Longtime User
I forgot to add something.
According with the nites from this thread:
https://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/
"When a new message is available the background thread that is responsible for reading data sends a message to the main thread message queue. This message causes the NewData event to be raised. If you are receiving many messages repeatedly and you show a modal dialog (like Msgbox) then it is possible that the order of events will be changed."

I don't have any modal dialog open.

Dan
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
Hi, try this



B4X:
Sub AStreams_NewData (Buffer() As Byte)
       
   Dim Rx_Data As String
         
     Rx_Data = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   
       
If Rx_Data.Length = 14 Then ' length data (FIRST CONTROL)
   
   
   'SPLIT DATA  OF >>>>>>  00,19,02,00,00   
   
   If Rx_Data.CharAt(0) = "0" AND Rx_Data.CharAt(1) = "0" AND Rx_Data.CharAt(2) = "," AND Rx_Data.CharAt(3) = "1"Then' ecc.... (SECOND CONTROL)
   

     'YOUR FUNCTION

   
   
   Else
Log("Error data")

End If
End If
 
Upvote 0

yo3ggx

Active Member
Licensed User
Longtime User
Hi,

I think is a misunderstanding in my post.
By "00,19,02,00,00" I mean a byte array (with the lenght 5) with the respective byte values (in HEX), not a string.
buffer(0) = 0x00
buffer(1) = 0x19
buffer(2) = 0x02
buffer(3) = 0x00
buffer(4) = 0x00
The byte array can contain any values for each byte, just the length is known, so I have no way to check the validity of the received data.

Unfortunately I cannot use Prefixes as I don't have any control over the other end.

Thank you,
Dan
 
Upvote 0

yo3ggx

Active Member
Licensed User
Longtime User
Hi Erel,

Thank you for your answer. It seems that DoEvents is responsible for this. I will do some changes in the code and be back with the result.

Dan
 
Upvote 0

yo3ggx

Active Member
Licensed User
Longtime User
Hi Erel,

"DoEvents" was the root cause. Now the order is always correct.

Thanks a lot.
Dan
 
Upvote 0

Thuong

Member
Licensed User
Longtime User
for stream data, I think you should use format <header + data + footer > for transmitter
If not, bluetooth device should transmit data and delay
 
Upvote 0

yo3ggx

Active Member
Licensed User
Longtime User
Hi.
As stated in one of my previous email, I have no control over the other end, so the protocol cannot be modified.
Anyway, removing DoEVents solves my issue completely.
Thank you.
Dan
 
Upvote 0
Top