3,119
社区成员
发帖
与我相关
我的任务
分享
private Handler handler = new Handler(){
public void handlerMessage(Message msg){
Toast.makeText(DayMainActivity.this, String.valueOf(msg.obj), Toast.LENGTH_LONG).show();
super.handleMessage(msg);
}
};
private class AcceptThread extends Thread{
private BluetoothServerSocket serversocket;
private BluetoothSocket socket;
private InputStream is;
private OutputStream os;
public AcceptThread(){
try {
serversocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);
} catch (Exception e) {
// TODO: handle exception
}
}
public void run(){
// 截获客户端的蓝牙消息
try {
//显示进程。
Toast.makeText(DayMainActivity.this, "线程执行", 1000).show();
socket = serversocket.accept();// 如果阻塞了,就会一直停留在这里
is = socket.getInputStream();
os = socket.getOutputStream();
while(true){//不断从客户端读取数据
byte[] buffer = new byte[128];
// 读取
int count = is.read(buffer);
// 如果读取到了,我们就发送刚
Message msg = new Message();
msg.obj = new String(buffer,0,count,"utf-8");
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}