SPPプロファイルを使用して、Bluetooth接続を使用してAndroidスマートフォンとAndroid以外のBluetoothモジュール間でデータを転送するAndroidアプリに取り組んでいます。Android Developer サイトの Bluetooth Chat Example を参照として使用しました。
2 つのデバイスを相互に接続し、スマートフォンから bluetooth モジュールに単純な文字列を送信することに成功しました。しかし、モジュールから返されたデータの読み取りでエラーが発生しました。Bluetooth チャットの例とまったく同じ次のコードを使用して、InputStream からデータを読み取りました。
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String str = new String(buffer);
Log.i(TAG, "mmInStream - " + str);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
Bluetooth モジュールが電話に単純な文字列を送信すると、その文字列が正しく受信されません。ランダムな方法でいくつかの部分に分割されます。たとえば、「1234567890abcdef1234567890abcdef0123456789」を電話に 3 回送信すると、Eclipse の Logcat はこれらをログに記録します。
mmInstream - 12345678910abcdef��������(continuing null)
mmInstream - 1��������(continuing null)
mmInstream - 2345678910abcdef0123456789��������(continuing null)
初めて。2 回目と 3 回目のデータ送信では、異なる断片で受信されます。
mmInstream - 1234567891�������(continuing null)
mmInstream - 0abcdef012�������(continuing null)
mmInstream - 3456789���������(continuing null)
mmInstream - 1234567891����������������(continuing null)
mmInstream - 0abcdef0123456789������������(continuing null)
なぜこれが起こるのか、この問題を解決する方法はわかりません。このように任意の方法でデータを受信すると、処理に必要なデータを取得できません。ワンピースでどうやって手に入れるの?
どんな助けでも大歓迎です。
どうもありがとう。