简体   繁体   English

Android蓝牙广播接收器未接收

[英]Android Bluetooth Broadcast Receiver not receiving

We have a hardware that will try to connect to the android phone, so opening a server socket on the android phone and listening will work. 我们拥有会尝试连接到android手机的硬件,因此在android手机上打开服务器套接字并进行监听即可。

The problem I'm having now is we have a broadcast receiver listening for ACL_CONNECTED and ACL_DISCONNECTED. 我现在遇到的问题是,我们有一个广播接收器正在侦听ACL_CONNECTED和ACL_DISCONNECTED。

The broadcast receiver is not receiving those event, neither is the server socket accepting connection from the hardware it just stay at the accept() method. 广播接收器未接收到这些事件,服务器套接字也不接受来自硬件的连接,它只是停留在accept()方法上。

To make it work I had to make a connection from the Android Phone to the hardware first then from that point I'm getting all the ACL_CONNECTED/ACL_DISCONNECTED events and my server socket is accepting connection from the hardware. 要使其正常工作,我必须先建立从Android Phone到硬件的连接,然后从那时开始,我得到所有ACL_CONNECTED / ACL_DISCONNECTED事件,并且我的服务器套接字正在接受来自硬件的连接。

Is this a normal behavior? 这是正常现象吗? That the Android has to first make a connection once to the hardware before it can receive the ACL_CONNECTED/ACL_DISCONNECTED events? Android必须先与硬件建立一次连接,然后才能接收ACL_CONNECTED / ACL_DISCONNECTED事件? Once I did that, I will always receive the events, until I do like a factory reset on the device then it's the same issue again. 完成此操作后,我将始终收到事件,直到我确实希望设备恢复出厂设置,然后又是同样的问题。

Thanks 谢谢

It is hard to figure out what exactly is wrong without any code provided. 没有提供任何代码,很难弄清楚到底是什么问题。 However I am going to fathom a guess. 但是,我要猜测一下。 The most common reason for connections to fail is because the appropriate UUID is not provided. 连接失败的最常见原因是未提供适当的UUID

In most cases the most generic UUID for common devices such as printers,keyboards, headsets etc is 00001101-0000-1000-8000-00805F9B34FB. 在大多数情况下,常见设备(例如打印机,键盘,耳机等)的最通用UUID是00001101-0000-1000-8000-00805F9B34FB。 When you ask your phone to connect to such a device and you have used this UUID most of the time it will connect. 当您要求手机连接到这样的设备并且您在大多数时间都使用了该UUID时,它将连接。 However when you tell your hardware device to connect to your phone(initialize connection), if it does not have the appropriate UUID for your phone the connection cannot be established. 但是,当您告诉硬件设备连接到手机(初始化连接)时,如果它没有适合您手机的UUID,则无法建立连接。

So in summary: Check your UUID definitions. 因此,总而言之:检查您的UUID定义。

UPDATE UPDATE

Does your android side code look this? 您的android辅助代码看起来像这样吗?

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

If this is the case then it is a client not a server. 如果是这种情况,则它是客户端而不是服务器。 And your hardware is listening for a client to connect to it rather then the other way around. 而且您的硬件正在侦听客户端连接,而不是相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM