当我关闭蓝牙服务器插座或蓝牙插座时,Android应用程序意外退出

时间:2010-12-26 04:13:19

标签: android bluetooth

当bluetoothServersocket.close()在主线程中调用bluetoothServersocket.accept()时,android app在没有任何警告和错误的情况下退出被阻止了另一个线程中的传入连接。

这是我的代码片段。

  
    

public void run(){

         
      
        

尝试{

                 
          
            

bluetoothServerSocket = BluetoothAdapter。             getDefaultAdapter()。listenUsingRfcommWithServiceRecord(LISTEN_NAME,LISTEN_UUID);

                         

/ *此处阻止线程传入连接* /

                         

bluetoothSocket = bluetoothServerSocket.accept();

                         

...

          
        
                 

} catch(Exception e){} finally {

                 
          
            

尝试{bluetoothServerSocket.close();} catch(Exception ex){}

                         

尝试{bluetoothSocket.close();} catch(Exception ex){}

          
        
                 

}

      
    
         

}

  

在活动中。

  
    

public void onStop(){

         
      
        

尝试{thread.bluetoothSocket.close();} catch(Exception ex){}

                 

尝试{thread.bluetoothServerSocket.close();} catch(Exception ex){}

                 

super.onStop();

      
    
         

}

  

当我点击后退按钮时。该活动已经结束,但大约一秒后该应用程序退出而没有任何警告。

我的android os版本是2.2。 HTC Desire设备。

1 个答案:

答案 0 :(得分:1)

我们应该关闭这样的蓝牙连接:

  
    

尝试{synchronized(bluetoothServerSocket){bluetoothServerSocket.close();}} catch(Exception ex){}