2012-01-01 36 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); 

這會照顧我的問題。