TextView
シリアルからデータを表示する必要がありBluetoothます。しかし、アプリケーションをシリアルデバイスに接続すると、接続されましたが、突然強制的に閉じられました。logcat には何も表示されないため、何が問題なのかわかりません。
inputstream
これは、接続中にアプリケーションがリッスンするときのコードです。
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
//mEmulatorView.write(buffer, bytes);
mTextView.append(new String(buffer));
// Send the obtained bytes to the UI Activity
//mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
String a = buffer.toString();
mTextView.setText(a);
a = "";
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
そしてTextView mTextView = (TextView) findViewById(R.id.dataTerm);
レイアウト上:
<TextView
android:id="@+id/dataTerm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
それで、誰かが何がうまくいかなかったのか知っていますか?どんな答えもとても役に立ちます、ありがとう..
最終的に動作するコード
私の場合は FinalSetting という名前のメイン ファイルで:
Activity メソッドで、次のように宣言します。//Layout View private static TextView mTextView;
メソッドで次
onCreate(Bundle savedInstanceState)
を宣言しtextview
ます。mTextView = (TextView) findViewById(R.id.dataTerm);
メソッド
Handler
について:case MESSAGE_READ: byte[] readBuf = (byte[]) msg.obj; //mEmulatorView.write(readBuf, msg.arg1); // construct a string from the valid bytes in the buffer String readMessage = new String(readBuf, 0, msg.arg1); //mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage); mTextView.setText(readMessage); break;
BluetoothService.java
ファイルについて:メソッドに直行し
ましょう//This thread runs during a connection with a remote device. //It handles all incoming and outgoing transmissions. 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"); byte[] buffer = new byte[1024]; //final byte[] buffer = new byte[1024]; 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(FinalSetting.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); } catch (IOException e) { Log.e(TAG, "disconnected", e); connectionLost(); break; } } } /** * Write to the connected OutStream. * @param buffer The bytes to write */ public void write(byte[] buffer) { try { mmOutStream.write(buffer); // Share the sent message back to the UI Activity mHandler.obtainMessage(FinalSetting.MESSAGE_WRITE, buffer.length, -1, buffer).sendToTarget(); } catch (IOException e) { Log.e(TAG, "Exception during write", e); } } public void cancel() { try { mmSocket.close(); } catch (IOException e) { Log.e(TAG, "close() of connect socket failed", e); } } }
シリアルBluetoothデバイスに接続すると、データが に表示されますTextView
。この助けを願っています:D