【问题标题】:Implementing BlockingQueue Buffer used in Bluetooth Communication for Android实现用于 Android 蓝牙通信的 BlockingQueue 缓冲区
【发布时间】:2012-01-01 14:14:15
【问题描述】:

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

我正在实现一个 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)。

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

【问题讨论】:

    标签: android bluetooth buffer blockingqueue


    【解决方案1】:

    我知道我的问题是什么。

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

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

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

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2015-05-30
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      相关资源
      最近更新 更多