2

我对此感到非常困惑,并且在过去的三天里我一直在尝试调试。希望有人能够告诉我我做错了什么。

我正在实现一个 BlockingQueue (FIFO) 缓冲区来接收通过蓝牙从我的 PC 流式传输的信息。我正在使用 RealTerm 通过超级终端链接发送预先记录的心电图信号。

我通过添加值然后删除它们来启动应用程序时测试了缓冲区,它似乎可以正常工作。

当我从蓝牙连接接收数据时尝试存储在缓冲区中时,问题就出现了。我不知道我添加的速度是否比 BlockingQueue 可以应付的速度快,但是当我停止数据传输并检查我的缓冲区时,整个缓冲区包含添加的最后一个值。缓冲区的大小是正确的,但内容不正确。

这是我的缓冲区:

public class IncomingBuffer {

private static final String TAG = "IncomingBuffer";

private BlockingQueue<byte[]> inBuffer;

public IncomingBuffer() {
    inBuffer = new LinkedBlockingQueue<byte[]>();
    Log.i(TAG, "Initialized");
}

public int getSize() {
    int size;
    size = inBuffer.size();
    return size;
}

// Inserts the specified element into this queue, if possible. Returns True
// if successful.
public boolean insert(byte[] element) {
    Log.i(TAG, "Inserting " + element[0]);

    boolean success = inBuffer.offer(element);
    return success;

}

// Retrieves and removes the head of this queue, or null if this queue is
// empty.
public byte[] retrieve() {
    Log.i(TAG, "Retrieving");
    return inBuffer.remove();

}

// Retrieves, but does not remove, the head of this queue, returning null if
// this queue is empty.
public byte[] peek() {

    Log.i(TAG, "Peeking");
    return inBuffer.peek();
}
}

我的 BluetoothCommunication 类接收信息并将其发送到缓冲区的部分如下:

   public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        ringBuffer = new IncomingBuffer();

        byte[] buffer = new byte[1024];
        Log.i(TAG, "Declared buffer byte");

        int bytes;

        byte[] retrieve;
        int size;

        Log.i(TAG, "Declared int bytes");
        //Setting up desired data format 8
        write(helloworld);
        Log.i(TAG, "Call write(initialize)");

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                Log.i(TAG, "Trying to get message");
                // Read from the InputStream
                bytes = mmInStream.read(buffer);

                //THIS IS WHERE THE BYTE ARRAY IS ADDED TO  THE IncomingBuffer
                RingBuffer.insert(buffer);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();

                Log.i(TAG, "Sent to target" +ringBuffer.getSize());
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                // Start the service over to restart listening mode
                BluetoothCommService.this.start();
                break;
            }
        }
    }

所以我的问题的一个例子是:

通过蓝牙连接发送值(从 1 到 20 的 8 位值)。在 IncomingBuffer 类的 insert 方法中,日志消息确认发送了正确的值。当从缓冲区中检索值时,它包含 20 个字节数组,这些数组都包含最后插入的数字 (20)。

关于为什么缓冲区会在其他情况下工作但在蓝牙通信期间不起作用的任何线索?

4

1 回答 1

1

我弄清楚我的问题是什么。

当我使用变量buffer读取mmInStream然后将其传递给 时ringBuffer,每次通过 while 循环时,我都会传递相同的字节数组变量。据我所知,它只是分配了一个特定的内存位置来计算字节数组,这就是为什么ringBuffer最后我的所有元素都是从mmInStream.

我所做的改变是创建一个单独的变量,我将“缓冲区”字节数组克隆到其中。在将“缓冲区”传递给“RingBuffer”之前,我执行以下操作:

byte[] newBuf;
newBuf = buffer.clone();
ringBuffer.store(newBuf);

这可以解决我的问题。

于 2012-01-02T20:17:10.200 回答