Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrictions when scanning in background on Android 10?

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
like image 693
Alix Avatar asked Dec 04 '19 16:12

Alix


People also ask

What is restrict background activity?

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

Why did Android OS limit the power given to background services?

Services running in the background can consume device resources, potentially resulting in a worse user experience. To mitigate this problem, the system applies a number of limitations on services. The system distinguishes between foreground and background apps.

What is background activity in Android?

Definition of background work. An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.


1 Answers

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

like image 198
kfh Avatar answered Oct 01 '22 17:10

kfh