Android 12: BLE Scan does not find any devices
Asked Answered
K

3

10

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!

Kellie answered 4/1, 2022 at 12:12 Comment(8)
BLUETOOTH_ADVERTISE, BLUETOOTH_CONNECT, and BLUETOOTH_SCAN are runtime permissions, therefore the app explicitly must request user approval. I can't see this in your snippet. For more Information: developer.android.com/guide/topics/connectivity/bluetooth/…Admonish
@Admonish Thanks for explaining this. But I already do that. I'll update my post.Kellie
I assume your target sdk is 31 (Android 12)?Glimp
Hi @MichaelKotzjan my Target SDK was 31, I changed it for testing purposes to 32Kellie
Maybe you can find some answers on this thread : #67723450Wilburn
Hey @Wilburn thank you but I already found that thread. I'm also searching a long time for a solution and begin to think this is a phone-specific bug with Android 12 (have a pixel 3 and a pixel 6 both with Android 11 and Android 12).Kellie
@LukasWerner the accepted answer of this thread mention a bug with Android 12 beta. Maybe this bug is not fully fixed on pixels ?Wilburn
@Wilburn yes I know but Android 12 isn't in beta anymore and the thread also says that it's fixed already. Additionally, what I wonder about is that the ble scanner app worksKellie
K
14

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.

Kellie answered 11/1, 2022 at 7:26 Comment(7)
That was strange. I certainly do not need that on my Pixel 3 with Android 12.Nickolasnickolaus
I had others test that and even on a newer Samsung with android 12 ble devices could not be found with the Bluetooth permissions only. The BLE Scanner App queries those as wellKellie
For me, the BLE scanner on Android 12 (Samsung S10) finds some devices but filters others, even with location permissions. Haven't resolved this issue yet.Samarium
@Samarium From official documentation, this might be helpful. "If you include neverForLocation in your android:usesPermissionFlags, some BLE beacons are filtered from the scan results."Matti
So Android documentation is wrong... Is it sufficient to declare them into the manifest or also to ask them runtime?Wexford
That's also what I experienced on my Zenfone 8. Possibly this is manufacturer related?Kimon
even after giving fine location permission you have to give coarse location. that is silly. thanks your solution worksRansack
S
12

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.

Samarium answered 20/1, 2022 at 9:32 Comment(1)
Yes, beacons still need ACCESS_FINE_LOCATION. Since beacons reveal location. The neverForLocation attribute in essence says: "I'm only using this to connect to devices that don't have any known specific location." Granted, that's not well-documented, but that's how I understood it.Kimon
P
0
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
android:name="android.permission.BLUETOOTH_CONNECT"

Add these two permission in manifest and make to ask permission from user

Ptomaine answered 5/8, 2022 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.