私はアンドロイドのウェブサイトからBluetooth Chatサンプルアプリを使用しています。私は特定のフォーマット(コードの下で述べる)で長いメッセージを送信し、それを相手側で受信し、私の電話(HTC Desire)に表示したいと思います。私はこのメッセージをさらに解析し、いくつかのものを抽出し、それを私のアプリケーションに使用します。ブルートゥースのバグ/問題を実行するHTC DesireのSPPプロファイルがBluetoothチャットアプリに影響しますか?
上記のメッセージのすべての文字を受信できません(いくつかの文字はランダムに省略されています)。それは、BluetoothソケットのInputStream "read"メソッドの問題、またはBluetooth SPPプロファイルにHTC Desireやその他の理由でバグや問題があることが原因ですか?私はInputStreamをバイトバッファ、charバッファにしようとしてから、通常の新しいString、StringBuilder、StringBufferを使用してバッファの内容から文字列を作成します。しかし、どういうわけか私は電話で必要な形式でメッセージ全体を表示することができません。
ご協力いただきありがとうございます。
はお時間を
乾杯、 マドゥナンダン
/** * をいただき、ありがとうございます。このスレッドは、リモートデバイスとの接続時に実行されます。 *すべての着信と発信を処理します。 */
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
char[] buffer = new char[1024];
int bytes;
//Writer writer = new StringWriter();
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
Reader reader = new BufferedReader(
new InputStreamReader(mmInStream, "UTF-8"));
//int n;
while ((bytes = reader.read(buffer)) != -1)
//writer.write(buffer, 0, bytes);
//String str = writer.toString();
// 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;
}
}
}
case MESSAGE_READ:
char[] readBuf = (char[]) msg.obj;
// construct a string from the valid bytes in the buffer
//StringBuffer sb = new StringBuffer("");
String Incoming = new String (readBuf, 0, msg.arg1);
StringBuilder sb = new StringBuilder("");
String readMessage = sb.append(Incoming).toString();
//String readMessage = new String(sb.append(Incoming));
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
mTitle.setText(readMessage);
//String readMessage = new String(sb);
//Make sure there is a received message. Extract it's name and value
//int msgLen = readMessage.length();
//if(msgLen!= 0)
//Message format: <MSG><N>xxx<!N><V>yyy<!V><!MSG> (xxx-name of the //message, yyy-value of the message)
if (readMessage.matches("<MSG><N>.*<!N><V>.*<!V><!MSG>"))
extractData(readMessage);
//else mTitle.setText(R.string.floor_it);
sb.setLength(0);