他のデバイスからのデータ(バイト)を受信するためにBluetoothを使用するアプリケーションがあります。すべてうまくいっていますが、バイトをすべて一緒に受信することには少し問題があります。バイトを受け取った後、私はそれらをテストするためだけにトーストに示します。他のデバイスが10バイトを一緒に送信すると(たとえば "ABCDEFGHIJ")、プログラムは最初のバイト "A"のみをトーストに表示し、2番目の繰り返しに進み、 BCDEFGHIJ "をトーストに追加します。ここに私のコードは次のとおりです。MainActivityでBluetooth経由ですべてのバイトを一緒に読み取る方法は?
byte[] buffer = new byte[1024]; // Read 1K character at a time.
int bytes = 0; // Number of bytes.
while(true)
{
try
{
// Read from the InputStream.
bytes = bInStream.read(buffer);
// Send the obtained bytes to the MainActivity.
mainActivityHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
catch(IOException e)
{
connectionLost();
break;
}
}
、私が持っている:
// The Handler that gets information back from the BluetoothManager.
private final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer.
String readMessage = new String(readBuf, 0, msg.arg1);
Toast.makeText(MainActivity.this, readMessage, Toast.LENGTH_SHORT).show();
break;
// ...
}
}
};
は、どのように私は一緒にすべてのバイトを受け取ることができます!
は、私はあなたがあなたのメッセージを送っているかを確認することができ役に立てば幸い? bOutStream.write( "ABCDEFGHIJ" .getBytes())のようなことをやっているのですか、一度に1つずつ文字を書き出していますか? – broody
bluetoothモジュールがarduinoに接続されているため、arduinoソフトウェアのシリアルモニタを使用してメッセージを送信しています。 –