【问题标题】:Android app unexpectedly quitted when I closed the bluetooth serversocket or bluetooth socket当我关闭蓝牙服务器套接字或蓝牙套接字时,Android 应用程序意外退出
【发布时间】:2011-05-30 18:47:34
【问题描述】:

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

这是我的代码片段。

公共无效运行(){

试试{

bluetoothServerSocket = 蓝牙适配器。 getDefaultAdapter().listenUsingRfcommWithServiceRecord(LISTEN_NAME, LISTEN_UUID);

/* 此处为传入连接阻塞的线程 */

bluetoothSocket = bluetoothServerSocket.accept();

...

} catch(Exception e) {} 终于 {

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

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

}

}

在活动中。

public void onStop () {

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

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

super.onStop();

}

当我单击后退按钮时。活动关闭,但大约一秒钟后,应用程序退出,没有任何警告。

我的安卓操作系统版本是 2.2。 HTC Desire 设备。

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2017-06-05
      • 1970-01-01
      • 2011-03-16
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多