相关编码如下所示:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what==READ) {
String str = (String)msg.obj;
textView1.setText(str);
}
super.handleMessage(msg);
}
};
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
try {
tmpIn = socket.getInputStream();
}catch (IOException e) { }
mmInStream = tmpIn;
}
public void run() {
byte[] buffer = new byte[5];
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
String str = new String(buffer);
temp = byteToInt(buffer); //Convert byte to int
handler.obtainMessage(READ, bytes, -1, str).sendToTarget();
}catch (Exception e) {
System.out.print("read error");
break;
}
}
}
}
答案 0 :(得分:1)