即使设备已配对,BluetoothServerSocket.accept()也不会返回

时间:2012-02-10 13:45:09

标签: android bluetooth connection rfcomm

我正在开发基于BluetoothChat exemple的Android蓝牙应用程序。我正在启动蓝牙服务器,并在不安全的rfcomm连接上侦听设备(而非手机)以连接到我的应用程序。

private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread(boolean secure) {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE);
        } catch (Exception e) {
            Log.e(TAG, ".AcceptThread # listen() failed", e);
        } 
        mmServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn");

                socket = mmServerSocket.accept(); //FIXME: it blocks here

                Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
            } catch (Exception e) {
                MMLog.e(TAG, ".run # accept() failed: "+e);
                connectionFailed();
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // starting the thread where i will receive input
                        // streams from the other device
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }

    }

    public void cancel() {
        try {
            if(mmServerSocket != null) {
                mmServerSocket.close();
            }
        } catch (IOException e) {
            Log.e(TAG, ".cancel # Could not close server socket: ", e);
        }
    }
}

我使用的是HTC Desire S,android 2.3.5。设备已配对,但我没有收到数据,因为连接在'.accept()'方法中被阻止。它只是在等待。

socket = mmServerSocket.accept(); //...等待

  • 如果设备已配对,为什么还要等待?
  • 我如何建立连接,因为我也尝试过反射,但仍然没有结果
  • HTC的蓝牙堆栈有问题吗?有没有人建立连接可能使用另一个Android手机?

2 个答案:

答案 0 :(得分:0)

我认为你的代码有问题,不应该是socket = tmp.accept();这里是我必须建立套接字服务器的连接:

BluetoothServerSocket serverSocket = null;
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
try {
    serverSocket = bta.listenUsingInsecureRfcommWithServiceRecord("BluetoothChatInsecure", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) { e.printStackTrace(); }

while(!Thread.interrupted()) {
    try {
        socket = serverSocket.accept();
        if (socket != null) {
            Log.d("CONNECTED", "Connected bluetooth");

        /// do your stuff

答案 1 :(得分:0)

很可能,这与你的其他设备有关(这发生在我身上)。您的Android已经完成了其列出的传入连接的工作。您的特殊设备无法正确启动与Android手机的连接的原因有很多:

  • 设备神秘地切换到其他蓝牙配置文件,例如HDP而不是SPP
  • 该设备以某种方式记住其内存中的不同Android手机(它最后连接到或类似的东西)并继续尝试连接到该手机但不是您现在正在使用的手机。

我认为您最好的机会是询问特殊设备的制造商/销售商以获取详细规格和/或软件/驱动程序以进行配置/测试。