Search code examples
javaandroiddataformat

Identify and separate the data from Datagrampacke's Data Packet using java


Hi Currently I am working on Android that communicates with Healthcare device through bluetooth. The Healthcare device can send data packets like this format enter image description here

Now I want to know, how can I identify the LSB , Acces code, Header , Msb and Payload separately. and how can I retrieve the data from this packets. Really I am new for this kind of data packets development. I have googled, but I got theoretical solutions only. Also I want to know, whether I can use Datagrampacket or someother 3rd party API. Kindly someone suggest me some ideas and tutorial for this. Thanks in advance.


Solution

  • Try to use the following method:

    (2475 bits? it maybe should be 2472 or 2480, or if header is 54 bits, here should be 2474 bits) // read bytes

    public byte[] readBytes(InputStream inputStream, int length)
            throws IOException {
        byte[] data = new byte[length];
        int len = inputStream.read(data);
        if (len != length) {
            throw new IOException("Read the end of stream.");
        }
        return data;
    }
    
    
    //Get Header data
    byte[] headerData = readBytes(inputStream, 9);
    
    // I think header data need to parse again, its structure should look like the following format:
    // | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
    // |  Version  | Type  | other values  |
    // You can parse them to use headerData
    
    
    // #######################################
    // write bytes
    public class ByteWriter {
        private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    
        public void writeBytes(byte[] data) {
            try {
                outputStream.write(data);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public byte[] getBytes() {
            return outputStream.toByteArray();
        }
    }