Restrictions when scanning in background on Android 10?
Asked Answered
L

1

8

There seems to be a lot of "hidden features" regarding Bluetooth scanning on Android. For starters there is a "30s limit" (Android 7.0 ble scan no result). Then you have to set a ScanFilter in background mode otherwise you get no results (can't find a reference for this one).

Most recently I discovered that I don't get any scan results when I enable scanning using the following scan mode with the screen turned off (after 30s or so) on Android 10 (I have observed this behavior on Google Pixel 3 and Google Pixel 4, it works fine on older Androids):

/**
 * Perform Bluetooth LE scan in balanced power mode. Scan results are returned at a rate that
 * provides a good trade-off between scan frequency and power consumption.
 */
public static final int SCAN_MODE_BALANCED = 1;

There is another scan mode (which I have not tried yet):

/**
 * Perform Bluetooth LE scan in low power mode. This is the default scan mode as it consumes the
 * least power. This mode is enforced if the scanning application is not in foreground.
*/
public static final int SCAN_MODE_LOW_POWER = 0;

Question: Should I interpret this comment "is enforced" as enabling scanning with something other than SCAN_MODE_LOW_POWER will give me no scan results? Can somebody confirm this?

I will investigate further on my own, but it takes time...

Note: I have a foreground service and I can see in the ADB logs that the scanner is enabled / disabled periodically. But I don't get any scan results...

Update: I've now made sure to use SCAN_MODE_LOW_POWER when in background mode but still I get no results. I have no idea what's going on.

Update 2: I tried running an older version of the app (not compiled for Android 10) and that worked fine

Update 3: I disabled battery optimizations for the app just in case. This did not help:

  1. Go to Settings > Apps > Your app > Advanced > Battery > Battery optimization
  2. Change view to All apps
  3. Search for your app
  4. Choose Not optimized
Lemuroid answered 4/12, 2019 at 16:16 Comment(0)
D
4

Android 10 requires new permissions in order to get BLE scans to return results. Unfortunately, the scan will simply not return results instead of logging a warning. The new permissions are ACCESS_FINE_LOCATION instead of ACCESS_COARSE_LOCATION required for sdk versions 23 through 28 and if you need to perform BLE scans while the app is in the background (even if you have a foreground service) you will need ACCESS_BACKGROUND_LOCATION. You must list these permissions in the manifest and prompt the user to grant them at runtime. There's a similar question here with a little more info in the answers: Android 10 not working with BLE Bluetooth scanning

Donata answered 27/2, 2020 at 20:52 Comment(3)
I can confirm that requesting the "ACCESS_BACKGROUND_LOCATION" permission solved it for meLemuroid
A Quote regarding ACCESS_BACKGROUND_LOCATION: "This is a hard restricted permission which cannot be held by an app until the installer on record whitelists the permission. For more details see PackageInstaller.SessionParams.setWhitelistedRestrictedPermissions(Set)." You can see this here: "developer.android.com/reference/android/…"Sphenic
Also, ACCESS_BACKGROUND_LOCATION doesn't identify itself in the IDE until your target SDK is 29 or higher. Thought I should note that.Sphenic

© 2022 - 2024 — McMap. All rights reserved.