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

时间:2014-10-15 07:05:36

标签: android android-bluetooth bufferedinputstream

我读了很多方法,现在我的脑袋里有不同的方法,我不能做我想做的事情。从arduino我发送固定的8字节消息。由于波特率,硬件问题或我得到的消息以随机大小分隔(例如:1st msg = 1byte,2nd msg = 7 bytes)。要解决这个问题,必须将缓冲区中的字节添加到带有序列的新数组中。为了测试消息是否正确,我将检查CRC32。我想做测试程序获取8字节消息,计算CRC并将其发送回arduino以检查它是否正确。在这种情况下,我将获得Rx / Tx检查。

如果缓冲区大小为buffer[256],如何获取我收到的字节数,将它们添加到byte[] c = new byte[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 个答案:

答案 0 :(得分:0)

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)
}