Android InputStream dropping first two bytes (modified BluetoothChat)

后端 未结 5 937
长情又很酷
长情又很酷 2020-12-05 12:47

I\'ve used code from BluetoothChat example to send and receive byte data from a Bluetooth Scale. The scale receives the command from the device, then sends back a byte array

5条回答
  •  一个人的身影
    2020-12-05 13:06

    As said before the data is not guaranteed, bluetooth is an open stream of data like UDP.

    I suppose some understanding of rs232 is needed to know when bytes will be packaged together, or sent individually.

    with a microcontroller project, imagine something like:

    if (data_ready) echo read_byte();
    

    with PIC microchips, the resulting data is something like:

    0 0 0 0 0 0 0 h 0 0 0 0 e 0 0 0 0 0 0 l 0 0 0 0 0 0 l 0 0 0 0 0 0 o ...
    

    With the Android to microcontroller project I'm working on at the moment I'm doing something like:

    do {instream.read()} while (!DELIMETER)
    

    I've found that you need to be efficient with your code when reading a bluetooth data stream. Using inputstream methods other than read(), I would almost always miss the first byte.

    I've just started with bluetooth smart, which I guess is quite different since data is only sent when it's available.

提交回复
热议问题