out.write(arrayOfByte1);//发送数据到蓝牙串口
SystemClock.sleep(2000);//暂停2秒
//接收蓝牙串口返回的数据
new Thread(){
public void run(){
byte[] b = new byte[1024]; // buffer store for the stream
while (true){
try {
while((bytes =mmInStream.read(b))!=-1){
ty.gsm =ty.gsm+ new String(b,0,bytes);
}
} catch (IOException e){
break;}
}
}
}.start(); //如何让上面的线程执行结束后才运行下面主进程的代码
shuju(ty.gsm);
if(ty.nx.equals("")&&ty.ey.equals("")&&ty.zg.equals("")){
if(System.currentTimeMillis()-time>10000){toast("通讯出错");ty.socket=null; return;}
SystemClock.sleep(2000);//暂停2秒
//接收蓝牙串口返回的数据
new Thread(){
public void run(){
byte[] b = new byte[1024]; // buffer store for the stream
while (true){
try {
while((bytes =mmInStream.read(b))!=-1){
ty.gsm =ty.gsm+ new String(b,0,bytes);
}
} catch (IOException e){
break;}
}
}
}.start(); //如何让上面的线程执行结束后才运行下面主进程的代码
shuju(ty.gsm);
if(ty.nx.equals("")&&ty.ey.equals("")&&ty.zg.equals("")){
if(System.currentTimeMillis()-time>10000){toast("通讯出错");ty.socket=null; return;}
Handler h = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case 0:
shuju(ty.gsm);
if(ty.nx.equals("")&&ty.ey.equals("")&&ty.zg.equals("")){
if(System.currentTimeMillis()-time>10000){toast("通讯出错");ty.socket=null; return;}
break;
default:
break;
}
}
};然后在新线程里调用h.sendemptymessage(0);
new Thread(){
public void run(){
out.write(arrayOfByte1);//发送数据到蓝牙串口
SystemClock.sleep(2000);//暂停2秒
byte[] b = new byte[1024]; // buffer store for the stream
while (true){
try {
while((bytes =mmInStream.read(b))!=-1){
ty.gsm =ty.gsm+ new String(b,0,bytes);
}
Messafe msg = Message.obtain();
msg.object = ty.gsm;
msg.what = 0;
handler.sendMessage(msg);
} catch (IOException e){
break;}
}}
}.start();Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case 0:
String gsm = (String)msg.object;
shuju(gsm);
if(ty.nx.equals("")&&ty.ey.equals("")&&ty.zg.equals("")){
if(System.currentTimeMillis()-time>10000){toast("通讯出错");ty.socket=null; return;}
break;
default:
break;
}
}
};