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

发布于 2025-01-04 04:14:55 字数 3000 浏览 4 评论 0原文

我正在开发一个基于 BluetoothChat 示例 的 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,安卓 2.3.5。设备已配对,但我没有收到数据,因为连接在“.accept()”方法中被阻止。它只是继续等待。

socket = mmServerSocket.accept(); //...and waiting

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

I am developing an Android bluetooth application based on the BluetoothChat exemple. i am starting a bluetooth server and listening for a device(not a phone) to connect to mine app on an insecure rfcomm connection.

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);
        }
    }
}

I am using a HTC Desire S, android 2.3.5. The device gets paired, but i don't receive data, because the connection gets blocked in the '.accept()' method. It just keeps on waiting.

socket = mmServerSocket.accept();
//...and waiting

  • Why does it still wait, if the device is paired?
  • How can i establish the connection, because i also tried reflection, and still no result
  • Is there a problem with HTC's Bluetooth stack? Has anyone established the connection maybe using another android phone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2025-01-11 04:14:55

我认为您的代码有问题,不应该是 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

I think there is something wrong with your code, shouldn't it be socket = tmp.accept(); here is what I have to make a socket server connection:

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
夏九 2025-01-11 04:14:55

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

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

我想您最好的机会是向特殊设备的制造商/销售商询问详细规格和/或用于配置/测试它的软件/驱动程序。

Chances are, this has to do with your other device (this is happening to me). Your Android already does its job which is listing for incoming connections. There are many reason why your special device won't initiate connections properly to your Android phone:

  • The device mysteriously switch to other Bluetooth profile e.g. HDP instead of SPP
  • The device somehow remembers a different Android phone in its memory (it was last connected to or something like that) and keeps trying to connect to that phone but not the one you are using right now.

I suppose your best chance is to query the manufacturer/ seller of the special device for detailed specifications and/or software/ driver to configure/ test it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文