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:
- Go to Settings > Apps > Your app > Advanced > Battery > Battery optimization
- Change view to All apps
- Search for your app
- Choose Not optimized