2

私はこれに本当に困惑しており、過去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 通信中は機能しない理由についての手がかりはありますか?

4

1 に答える 1

1

私の問題が何であるかを理解しました。

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

それを変更するために行ったのは、「バッファ」バイト配列のクローンを作成する別の変数を作成することです。「buffer」を「RingBuffer」に渡す前に、次のことを行います。

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

これで私の問題は解決します。

于 2012-01-02T20:17:10.200 に答える