HubbellCorp/SweetBlue

RxBleManager not returning scan results on Android 12 without location permissions

Opened this issue · 5 comments

When targeting Android 12 or higher, only BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions should be needed to scan/connect to BLE devices.

However, I noticed that without requesting ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION runtime permissions, RxBleManager does not find any BLE devices.

Could this be related to anything in the SweetBLUE library?

Did you strongly assert that you are not using location? If you don't you will not get results.

Did you strongly assert that you are not using location? If you don't you will not get results.

You mean in the manifest? I do:

    <uses-permission
        android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />

But worth noting, in the merged manifest (because I have a modular codebase that may define other permissions in other modules) it also includes:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

However, at runtime these are not always needed, as is the case I am reporting here (so I don't need to ask for them at runtime). Maybe this could be the cause. 🤔

In that case that would be unexpected Android behavior. Similar to how I recently discovered Android manifest merging doesn't at all work in a logical way (it does work as documented 🤷‍♂️ ).

Possibly wrong documentation according to this StackOverflow answer:

Contrary to the statements in the official documentation you still need the android.permission.ACCESS_FINE_LOCATION and android.permission.ACCESS_COARSE_LOCATION permissions to be set in the Manifest and request them from the mobile user.

Or, different device manufacturers deviating from the standard. From the comments:

I certainly do not need that on my Pixel 3 with Android 12.
... even on a newer Samsung with android 12 ble devices could not be found with the Bluetooth permissions only ...

Also possibly related issue on Google's issuetracker:

When an app that scans for Bluetooth peripherals using BLUETOOTH_SCAN permission is installed on an Android 12 device into only a non-default user managed profile (into a Work Profile), a permissions error is encountered instead of returning scan results.

When the same build of the same app is installed in only the default user profile on the same device (regardless of whether that default profile is managed) the scan returns results as expected.

Yeah, this wouldn't surprise me. When we did testing, we didn't have the location permissions in the manifest at all, though I only tested on a Pixel and a Samsung . It did seem to be pretty finicky though.

SweetBlue does add the location permissions to the manifest, so you can try to remove the nodes:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove" />

I tried removing both fine and coarse permissions in the main app manifest as suggested. No difference (still no scan results).

I also noticed you specify <uses-permission-sdk-23> for fine and coarse location. I tried removing those as well; no difference.

Then I tried removing android:usesPermissionFlags="neverForLocation": also no difference.

In summary: the only thing that does work on my Zenfone 8 is if I also ask for coarse and fine location permissions, similar to what others on StackOverflow reported for other phones.