Android 12: BLE Scan does not find any devices
I'm trying to upgrade a Bluetooth Low Energy app (connects to a custom physical device) to Android 12. I've set up everything as in the documentation, but it doesn't work.
Permissions:
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BILLING" />
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
Code:
private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.d(TAG, "Scan result!");
}
@Override
public void onScanFailed(int errorCode) {
Log.w(TAG, "Scan failed: " + errorCode);
}
};
public void start() {
bluetoothLeScanner.startScan(scanCallback);
}
I also ask for the permissions using the ActivityResultContracts.RequestMultiplePermissions()
contract. When I read through the logs I can see, that the permissions are set.
Now when I call the start()
-Method, I never get the onScanResult(...)
-Method called. When I switch to a device with Android 11 on it, it works without any problems. When I start the BLE Scanner app (from play store), it finds the device.
UPDATE: When I set back the targetSdk to 30, everything works well (with old permissions, etc. for sure).
Does anyone have the same problem or even fixed it? Would be grateful for any advices.
Thanks!
Solution 1:
I found the solution. 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. Now everything works fine again.
Solution 2:
I found that Andriod 12 still requires Location services enabled, android.permission.ACCESS_FINE_LOCATION
and additionally android.permission.BLUETOOTH_SCAN
without android:usesPermissionFlags="neverForLocation"
flag in AndroidManifest, and explicit granting of these permissions by user in runtime to search for beacon devices.
Other device types can be found without Location services and ACCESS_FINE_LOCATION
if in AndroidManifest you add the flag android:usesPermissionFlags="neverForLocation"
to android.permission.BLUETOOTH_SCAN
permission.