获取Client套接字时,BluetoothServerSocket.accept()将不会在接受线程中返回

时间:2012-07-19 15:15:53

标签: android bluetooth serversocket

所以我有这个Android蓝牙项目,我遇到了一个非常恼人的问题; 让我来描述一下背景:

要连接的两部手机使用Android开发者网站(here)上蓝牙文档中描述的确切方法获取蓝牙套接字; 所以,我使用以下线程进行连接:

private class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        // Use a temporary object that is later assigned to mmServerSocket,
        // because mmServerSocket is final
        BluetoothServerSocket tmp = null;
        try {
            // MY_UUID is the app's UUID string, also used by the client code
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) { }
        mmServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                break;
            }
            // If a connection was accepted
            if (socket != null) {
                // Do work to manage the connection (in a separate thread)
                manageConnectedSocket(socket);
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) { }
    }
}

这是acceptthread和

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        mBluetoothAdapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }

        // Do work to manage the connection (in a separate thread)
        manageConnectedSocket(mmSocket);
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

这是ConnectThread;(您可能会注意到它们确实是从上层链接复制的);

问题是,当尝试连接时,connectthread实际上返回一个套接字(调用managesocket()),但接受线程仍然在socket = mmServerSocket.accept(),好像什么也没发生; 但是在AcceptThread中发生了一些事情,因为当我从另一个设备(具有ConnectThread的设备)发起连接时,正在更新接受设备的logcat; 有时连接实际上是正确创建的,但只有在一些强制关闭后,禁用 - >启用蓝牙等等。

这里是logcat中一个有趣的行(由.accept()调用生成,我嘲笑):

07-19 18:04:47.484:D / BLZ20_WRAPPER(3143):btlif_signal_event:### event BTLIF_BTS_RFC_CON_IND不匹配###

那可能是什么问题?

0 个答案:

没有答案