数据处理导致Android上的蓝牙数据完整性不佳

时间:2011-04-26 00:02:35

标签: java android bluetooth inputstream

我正在处理我通过蓝牙接收的一些二进制数据,但是,当大量数据立即进入时,我的数据流中丢失或无效字节。我希望有人可以帮助我,因为我需要一些帮助来获得更一致的回复。只是简单地“丢掉”坏数据我就没事了。以下是相关方法,它们在“已连接”线程内运行:

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int num_bytes = 0;
        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                mmInStream.read(buffer, 0, 11);
                processBytes(buffer);

            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

    private void processBytes(byte[] bytes) throws IOException {
        if (bytes[0] != delimiter) {
            int c;
            do {
                c = mmInStream.read();
            } while (c != delimiter && c != -1);
            if (c == -1) {
                return;
            }
        }
        /*Some processing happens here to generate the variables cellId and
                      voltage and cellId2 and voltage2
                    */
        // Send the obtained bytes to the UI Activity
        mHandler.obtainMessage(BluetoothInterface.DATA_READ,
                new DataPoint(cellId, voltage)).sendToTarget();
        // Send the obtained bytes to the UI Activity
        mHandler.obtainMessage(BluetoothInterface.DATA_READ,
                new DataPoint(cellId2, voltage2)).sendToTarget();

    }

1 个答案:

答案 0 :(得分:1)

我以前遇到过这个问题。我不太确定我是否通过在调用processBytes之前将流读入字符串对象来解决它。