2012-01-01 4 views
2

私は本当にこれに困惑しており、渡された3日間デバッグしようとしています。うまくいけば、誰かが私が間違っていることを教えてくれるだろう。Android用Bluetooth通信で使用されているBlockingQueueバッファの実装

私はブルートゥース経由で私のPCからストリーミングされている情報を受け取るためにBlockingQueue(FIFO)バッファを実装しています。私はRealTermを使用して、ハイパーターミナルリンク上にあらかじめ記録された心電図信号を送信しています。

私は値を追加して削除することでアプリケーションを起動するときにバッファをテストしましたが、それは正常に動作するようです。

問題は、Bluetooth接続からデータを受信して​​いるときにバッファに格納しようとすると発生します。 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; 
      } 
     } 
    } 

だから私の問題の一例ですbe:

値をBluetooth接続(8ビットの値は1から20)で送信します。 IncomingBufferクラスのinsertメソッドでは、ログメッセージによって適切な値が送信されたことが確認されます。値がバッファから取得されると、最後に挿入された数(20)を含む20バイトの配列が格納されます。

バッファが他の状況では動作するが、Bluetooth通信中には動作しない理由についての手掛かりはありますか?

答えて

1

私の問題は何かを理解しました。

変数bufferを使用してmmInStreamから読み取った後、ringBufferに渡すと、whileループを実行するたびに同じバイト配列変数を渡します。私が理解できることは、バイト配列が計算される特定のメモリ位置を単純に割り当てることで、最後にはringBufferのすべての要素が、mmInStreamから 'バッファ'に割り当てられた最後の値であることになります。

私が変更したのは、 'バッファ'バイト配列をクローンする別の変数です。 'Buffer'を 'RingBuffer'に渡す前に、次のようにします。

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

これは私の問題を処理します。

関連する問題