私はこれに本当に困惑しており、過去3日間デバッグしようとしました。うまくいけば、誰かが私が間違っていることを教えてくれるでしょう。
Bluetooth 経由で 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;
}
}
}
したがって、私の問題の例は次のようになります。
Bluetooth 接続を介して値を送信します (1 から 20 までの 8 ビット値)。IncomingBuffer クラスの挿入メソッドでは、適切な値が送信されたことをログ メッセージが確認します。値がバッファから取得されると、最後に挿入された数値 (20) をすべて含む 20 個のバイト配列が含まれます。
バッファが他の状況では機能するが、Bluetooth 通信中は機能しない理由についての手がかりはありますか?