I need to show data to a TextView
from a serial Bluetooth.
But when i connect my application with the serial device, it did connected but suddenly went forced closed. The logcat shows nothing so I don t know what s wrong.
这是在申请聆听<条码>输入<>/代码>时使用的代码。
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;
}
}
}
and TextView mTextView = (TextView) findViewById(R.id.dataTerm);
and on the layout:
<TextView
android:id="@+id/dataTerm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
So does anyone know what went wrong? Any answers are so helpful, thanks..
www.un.org/spanish/ecosoc 最后运作的法典
On the main file, in my case named FinalSetting:
On the Activity method, declare://Layout View private static TextView mTextView;
On the
onCreate(Bundle savedInstanceState)
method declare thetextview
: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;
On the
BluetoothService.java
file:
Let s just straight to the method//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); } } }
After you connect to the serial Bluetooth device, the data will show up on the TextView
.
Hope this help :D