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:
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