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;}

解决方案 »

  1.   

    Thread.join() 或者 Handler
      

  2.   


    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);
      

  3.   

    你可以在while里查询thread的状态的。
      

  4.   

    这样好不会 比较耗资源?感觉楼上几位朋友说的用handler也行啊
      

  5.   

    我综合一下上面几位//接收蓝牙串口返回的数据
    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;
    }
    }
    };
      

  6.   

    Thread.join() 这个应该是正解