Android Question Bluetooth serial

Discussion in 'Android Questions' started by yo3ggx, Jun 5, 2015.

  1. yo3ggx

    yo3ggx Active Member Licensed 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:

    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,
  2. yo3ggx

    yo3ggx Active Member Licensed User

    I forgot to add something.
    According with the nites from this thread:
    "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.

  3. XorAndOr

    XorAndOr Active Member Licensed User

    Hi, try this

    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)


    Log("Error data")

    End If
    End If
  4. yo3ggx

    yo3ggx Active Member Licensed User


    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,
  5. Erel

    Erel Administrator Staff Member Licensed User

    The message order can only be incorrect if you are showing a modal dialog or calling DoEvents somewhere.
    Peter Simpson likes this.
  6. yo3ggx

    yo3ggx Active Member Licensed 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.

  7. yo3ggx

    yo3ggx Active Member Licensed User

    Hi Erel,

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

    Thanks a lot.
  8. Thuong

    Thuong Member Licensed User

    for stream data, I think you should use format <header + data + footer > for transmitter
    If not, bluetooth device should transmit data and delay
  9. yo3ggx

    yo3ggx Active Member Licensed User

    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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice