ブルートゥースに問題があります。接続が確立され、モジュールにデータを送信できますが、受信すると常にバイト配列が分割され、モジュールからのメッセージは最初のバイトとメッセージの残りの部分に分割されるため、単一ではなくメッセージ 2 つ取得しています。私はBluetoothチャットの例を使用しました。
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[100];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(ActivityTrafficSignChooserUpperSign.MESSAGE_READ, bytes, bytes, buffer)
.sendToTarget();
//Log.d(TAG, bytes);
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
これは私の活動の一部です:
private final Handler mHandler = 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
readMessage = new String(readBuf, 0, msg.arg1);
Toast.makeText(getApplicationContext(), readMessage, Toast.LENGTH_SHORT).show();
break;
}}