简体   繁体   中英

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.

The problem I'm having now is we have a broadcast receiver listening for ACL_CONNECTED and 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.

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.

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? 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.

In most cases the most generic UUID for common devices such as printers,keyboards, headsets etc is 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. 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.

So in summary: Check your UUID definitions.

UPDATE

Does your android side code look this?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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