Search code examples
androidbluetooth-lowenergyupgrade

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

  • 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.