Android 从InputStream获取字节到固定长度消息

Android 从InputStream获取字节到固定长度消息,android,android-bluetooth,bufferedinputstream,Android,Android Bluetooth,Bufferedinputstream,我读了很多方法,现在我的头脑里充斥着不同的方法,我不能做我想做的事。我从arduino发送固定的8字节信息。由于波特率、硬件问题或其他原因,我得到的消息以随机大小分开(例如:第一条消息=1字节,第二条消息=7字节)。要解决此问题,必须使用序列将缓冲区中的字节添加到新数组中。为了测试消息是否正确,我将与CRC32进行检查。我想做一个测试程序,得到8字节的消息,计算CRC,然后把它发送回arduino,检查它是否正确。在这种情况下,我将进行Rx/Tx检查 如果缓冲区大小为buffer[256],如何

我读了很多方法,现在我的头脑里充斥着不同的方法,我不能做我想做的事。我从arduino发送固定的8字节信息。由于波特率、硬件问题或其他原因,我得到的消息以随机大小分开(例如:第一条消息=1字节,第二条消息=7字节)。要解决此问题,必须使用序列将缓冲区中的字节添加到新数组中。为了测试消息是否正确,我将与CRC32进行检查。我想做一个测试程序,得到8字节的消息,计算CRC,然后把它发送回arduino,检查它是否正确。在这种情况下,我将进行Rx/Tx检查

如果缓冲区大小为
buffer[256]
,如何获取我接收到的字节数,将它们添加到
byte[]c=新字节[8]
,然后计算CRC32

buffer.length
总是给出256

private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    int destination = 0;

    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[256];  // buffer store for the stream

        /* Should i add to c array??? */
        byte[] c      = new byte[8];    // Fixed length message array

        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream

                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                // Here i should add buffer array to my array
                //System.arraycopy(buffer, 0, c, destination, buffer.length);
                //destination = ??? // number of bytes added to c and then do some if c is full calculate crc
                CRC32 checksum = new CRC32();
                checksum.update(c, 0, c.length);
                long checksumValue = checksum.getValue();
                System.out.println("CRC: " + checksumValue);
                write(""+checksumValue);
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(String message) {
        Log.d(TAG, "Sent: " + message + "...");
        byte[] msgBuffer = message.getBytes();
        try {
            mmOutStream.write(msgBuffer);
        } catch (IOException e) {
            Log.d(TAG, "Error data send: " + e.getMessage() + "...");     
          }
    }
}
1) buffer.length始终为您提供256,因为您在下一行中分配256:

byte[] buffer = new byte[256];
2)
mmInStream.read(buffer)
返回读取的字节数,如果已到达流的末尾,则返回-1

3) 假设您从流中读取了8个字节。这意味着您的缓冲区将有您刚刚读取的8个字节,其余248个字节为0(因为缓冲区是256)。因此,根据您的数据协议,您必须考虑到这一点。大多数情况下,您将有一个终止字节(或多个)模式。这会让你知道你得到了一个完整的信息

假设您的终止模式是0字节,那么它看起来像这样:

if (nrBytes <= BUFFER_SIZE) { // if buffer is enough
    for (int i = 0; i < nrBytes; i++) {
        //append byte to your byte[] c array 
        // or whatever data structure is suppose to hold each message
        if (0 == buffer[i]) {
              // termination pattern = complete message! 
              // CRC check
              // Do work with your message!
        }
    }
    buffer = new byte[BUFFER_SIZE]; //reset buffer (resets all to 0's)
} 
if(nrBytes)