2012-01-01 38 views
2

我真的难住这一点,我试图调试过去的三天。希望有人能够告诉我我做错了什么。在Android的蓝牙通信中实现BlockingQueue缓冲区

我正在实施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类的插入方法中,日志消息确认发送了适当的值。从缓冲区中检索值时,它包含20个字节数组,其中都包含插入的最后一个数字(20)。

任何线索为什么缓冲区可以在其他情况下工作,但不是在蓝牙通信期间工作?

回答

1

我想出了我的问题是什么。

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

我做了什么改变是做一个单独的变量,我克隆'缓冲区'字节数组。在我将'缓冲区'传递给'RingBuffer'之前,我执行以下操作:

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

这会照顾我的问题。