Problems reading bytes from bluetooth using Android
===

I Have a Bluettoth device which is connected to the phone using Bluetooth and sends packets over it.
The packets have 21 byte length and the following format:
0x00 | timestamp (2 bytes) | data 1 (6 bytes) | data 2 (6 bytes) | data 3 (6 bytes)

Each "data" is composed by 3 values. Data 1 and Data 2 are unsigned shorts little-endian and Data 3 is signed short little-endian.

The problem I have is, Android does not read in-range values but if I write a Python program wich does "the same", it works perfect.

The Java Code is the next:

    char [] b = new char[21];
    reader.read(b);
    for(int i = 0; i < sensors; i+= 6){
    if(i != sensors-6){
    data += Integer.toString((int)((b[4+i]&0xFF)<<8) + (int)(b[3+i]&0xFF));//x axis
    data += ",";
    data += Integer.toString((int)((b[6+i]&0xFF)<<8) + (int)(b[5+i]&0xFF));//y axis
    data += ",";
    data += Integer.toString((int)((b[8+i]&0xFF)<<8) + (int)(b[7+i]&0xFF));//z axis
    } else{
    data += Integer.toString(~((int)((b[4+i])<<8) + (int)(b[3+i])));//x axis
    data += ",";
    data += Integer.toString((int)((b[6+i])<<8) + (int)(b[5+i]));//y axis
    data += ",";
    data += Integer.toString((int)((b[8+i])<<8) + (int)(b[7+i]));//z axis
    }
    }
The Python Code is this:

    # read incoming data   
       ddata = ""
       numbytes = 0
       framesize = 21  #i.e. Packet type (1), TimeStamp (2), 3xAccel (3x2), 3xGyro (3x2), 3xMag (3x2)
       try:
          while True:
             while numbytes < framesize:
                ddata += ser.read(framesize)
                numbytes = len(ddata)
    
             data = ddata[0:framesize]
             ddata = ddata[framesize:]
             numbytes = len(ddata)
             
             packettype = struct.unpack('B', data[0:1])
             (timestamp, accelx, accely, accelz, gyrox, gyroy, gyroz, magx, magy, magz) = struct.unpack('HHHHHHHhhh', data[1:framesize])