javaandroidbluetoothbroadcastreceiverbluetooth-device-discovery

Android bluetoothadapter not emitting ACTION_DISCOVERY_FINISHED


I have a class with a broadcast receiver setup. The receiver listens for the following actions:

ACTION_FOUND
ACTION_DISCOVERY_STARTED
ACTION_DISCOVERY_FINISHED

The found and started actions get emitted as expected, however, the documentation for the BluetoothAdapter specifies that a discovery scan should take ~12 seconds and is then followed by a ACTION_DISCOVERY_FINISHED. This does not seem to be the case, since I can only manually trigger an emit of the finished action by making a call to cancelDiscovery(). If I leave it running long enough, sometimes the event is emitted (but this can take upwards of 1-2 minutes). Sometimes, that doesn't even happen. If I proceed to force the scan to end, by disabling Bluetooth, the action is emitted as expected.

Is the scan supposed to take this long, and am I supposed to manually control the scanning process, i.e. ending it myself after a 12 second countdown? Or am I missing something in the docs. The broadcastreceiver is configured correctly, as evidenced by the arrival of all the actions.


Solution

  • After upgrading the Nexus 5 to android version 6.0.1 as well, I seem to be getting a little more consistent results with the ACTION_DISCOVERY_FINISHEDbroadcast. It also came to light that my other android phone (Xperia Z3) wasn't finding any devices as per the changes in https://developer.android.com/about/versions/marshmallow/android-6.0-changes.htm , which means that it now also requires the ACCESS_COARSE_LOCATION permission, which you need to ask for during runtime. If this is not done, the broadcast will not be received by your application. I hope someone else finds this useful.