0

私は現在、Bluetooth接続を介して着信データのバッファとして使用するクラスを実装しようとしています:

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 = inBuffer.size();
    byte[] check=new byte[1];
    String total=" ";
    if(size>20){
        while(inBuffer.size()>1){
            check=inBuffer.remove();
            total=total+ " " +check[0];
        }
        Log.i(TAG, "All the values inside are "+total);
    }
    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();
}   
}

このバッファに問題があります。バイト配列を追加するたびに、バッファ内のすべてのバイト配列が追加したものと同じになります。

コードの残りの部分で (独自のクラスにせずに) 同じタイプのブロッキング キューを使用してみましたが、正常に動作します。問題は、このクラスを使用するときのようです。

クラスを宣言する方法は次のとおりです。

private IncomingBuffer ringBuffer;
ringBuffer = new IncomingBuffer();

私が間違いを犯している場所を誰かが見ることができますか?

4

1 に答える 1

1

毎回同じ ものを追加している可能性はありますか?byte[]

多分:

public boolean insert ( byte[] element ) {
  Log.i(TAG, "Inserting "+element[0]);
  // Take a copy of the element.
  byte[] b = new byte[element.length];
  System.arraycopy( element, 0, b, 0, element.length );
  boolean success = inBuffer.offer( b );
  return success;
}

あなたの問題を解決するでしょう。

于 2011-12-31T14:15:41.693 に答える