Android InputStream dropping first two bytes (modified BluetoothChat)

后端 未结 5 925
长情又很酷
长情又很酷 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:17

    I think the issue you are seeing with e.g. the 198 turning into -58 is because Java uses signed bytes, so anything over 127 is a negative number. So 198 binary is seen as -(256 - 198) = -58.
    The solution is to convert it to an int using some code like the following:

    private int UByte(byte b){
            if(b<0) // if negative
                return (int)( (b&0x7F) + 128 );
            else
                return (int)b;
        }
    

提交回复
热议问题