0

我正在尝试在安卓手机/平板电脑(4.0.3)和蓝牙设备之间建立蓝牙通信,蓝牙设备是一个耳环阅读器(Destron Fearring DTR3E,如果你想知道,我不认为你做)。

我通过蓝牙设置将手机与阅读器配对(阅读器在标签上有配对密码),蓝牙当然是打开的,现在我正在尝试通过 BluetoothServerSocket 收听来自设备的读取。问题是接受调用永远不会返回,所以很明显我做错了什么。通信是使用 RFCOMM 完成的。

代码:

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

                String uuid  = "00001101-0000-1000-8000-00805F9B34FB";
                tmp = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("pdfParserServer", UUID.fromString(uuid));
            } catch (Exception e) {
            e.printStackTrace();
            }
            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)

                    try {
                        mmServerSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }

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

有什么我想念的吗?

谢谢!

4

1 回答 1

2

可能导致代码永远不会从接受返回的唯一原因是,您尝试连接的设备“Destron Fearring DTR3E”实际上有一个蓝牙服务器套接字而不是蓝牙客户端,因此,设备可能正在等待您实际连接到它,而不是创建一个蓝牙服务器套接字并等待它连接到您的 android 设备,您应该阅读设备上的规格并确保您实际上是必须打开连接的那个“Destron Fearring DTR3E”插座...

希望这可以帮助...

问候!

于 2013-08-19T17:37:05.863 回答