0
我寫了一個類可以從藍牙串行端口收到的文本,但是當我送「一」之後我發「KKKK」敬酒「akkk」它敬酒「KKKK」,爲什麼?它應該的成績beyound考慮我應該怎麼做,使之士「是」我可以清除緩衝區,如何清除緩衝區?
這裏怎麼是我的代碼:??
class RecieveThread extends Thread {
private final InputStream mmInStream;
@TargetApi(Build.VERSION_CODES.ECLAIR)
protected RecieveThread(BluetoothSocket socket) {
InputStream tmpIn = null;
try {
tmpIn = socket.getInputStream();
} catch (IOException ignored) { }
mmInStream = tmpIn;
}
public void run() {
Looper.prepare();
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
String str = new String(buffer);
handler.obtainMessage(READ, bytes, -1, str)
.sendToTarget();
} catch (Exception e) {
s=e.getMessage();
System.out.print(s);
h=false;
break;
}
}
Looper.loop();
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==READ) {
String str = (String)msg.obj;
Toast.makeText(MyService.this,msg.str+"",Toast.LENGTH_SHORT).show();
}
else if(h==false)
Toast.makeText(MyService.this,"Failed:"+s+".",Toast.LENGTH_SHORT).show();
}
};