Beacon scanning not working with Android 12 Bluetooth permissions
See original GitHub issueHey David! Long time no see, I hope you’re doing well. 🙏
In the process of upgrading my application to Android 12, I was updating the permission declarations to the new Bluetooth permissions introduced in that OS version. Unfortunately, I’m unable to find my nearby beacon when using AltBeacon to range for it. Replacing the AltBeacon usage with some vanilla Android code (using BluetoothLeScanner
directly), I did find it, however.
In the PR for Android 12, it was noted that scanning didn’t work unless ACCESS_FINE_LOCATION
was also granted at runtime. Being a replacement for this old way, the new Bluetooth permissions should be able to work without that extra declaration though, so I went back into AltBeacon itself to see what I could find. Eventually, I landed inside this conditional inside CycledLeScanner
, which I identified as the culprit. The permission check in this method does not take the new permissions into account. I believe that checkLocationPermission
could be updated to include an SDK check and choose FINE/COARSE
or the new BLUETOOTH_SCAN
permission depending on the result. No problem from my end to file a PR for this change if it sounds good to you as well.
Expected behavior
Devices running Android 12 should be able to range for beacons without also requiring the old-style permissions.
Actual behavior
A declaration of ACCESS_FINE_LOCATION
is required in order to make beacon ranging work, even on Android 12.
Steps to reproduce this behavior
Launch the reference application on an Android 12 device and try to find a nearby beacon with it.
Mobile device model and OS version
Pixel Pro 6, Android 12
Android Beacon Library version
2.19
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (8 by maintainers)
Top GitHub Comments
My understanding is that it is not possible to detect popular bluetooth beacon formats (iBeacon, Eddystone) on when only approximate location permission is granted on Android 12+ because the operating system filters out those bluetooth detections. The operating system will only pass through those detections to third party apps if precise location permission is granted.
Hi, Marcel.
Thank you for pointing out that line. At a minimum I think it should be changed to put a warning in the log in an else condition.
However, I am not sure it should be removed. In my tests of an app targeting Android 12 (See here: #1043), it is necessary to have both BLUETOOTH_SCAN and ACCESS_FINE_LOCATION in order to detect common beacon formats like iBeacon, AltBeacon and Eddystone. Android’s documentation says that the manifest must include one of these two:
or
And says:
See: https://developer.android.com/guide/topics/connectivity/bluetooth/permissions?hl=it_ch&skip_cache=false
Can you explain exactly what you have in your manifest and what kind of beacons you are scanning for? It sounds to me like what you describe is not supposed to work.