如何在等待连接时关闭BluetoothServerSocket?

时间:2010-08-27 18:41:34

标签: android bluetooth

如何强制BluetoothServerSocket停止等待以accept()开始的传入连接?

当我正在呼叫stop()时,我得到的是“ERROR / BluetoothSppPort(2211):无效的端口状态断开连接”在LogCat中。

我需要停止控制线程,但在套接字等待连接时不能销毁它。

我知道accept(int timeout),但这不是我需要的。

有任何解决方案或解决方法吗?

1 个答案:

答案 0 :(得分:0)

while (socket == null) {
        try {
            socket = mmServerSocket.accept(50);
        } catch (IOException e) {
            if( UserReqestToExit )//if the user reqest to exit ,use sync
                return;
        }
        // If a connection was accepted
        if (socket != null) {
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
相关问题