KK_07k11A0585
KK_07k11A0585

Reputation: 2403

Android BLE SCAN getting stopped in 30 secs in Foreground Service after sending the app to background

We have BLE scan implemented in a ForegroundService our app, when we target the app from targetSDKVersion 31 (Android 12) and try to scan when app is in background, the scan is getting stopped exactly after 30 secs ..

Note:- We have used the permissions

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

<uses-permission android:name="android.permission.REQUEST_COMPANION_START_FOREGROUND_SERVICES_FROM_BACKGROUND"/>

<permission android:name="android.permission.START_ACTIVITIES_FROM_BACKGROUND"
    android:protectionLevel="signature|privileged|vendorPrivileged|oem|verifier" />

Do we need to change the way we are scanning or do we add any permissions or settings ???

Please help

Upvotes: 2

Views: 768

Answers (1)

Youssif Saeed
Youssif Saeed

Reputation: 13285

If you want to scan in the background, you need to add the ACCESS_BACKGROUND_LOCATION to your permissions (both in the manifest file and at runtime). There are a few other restrictions when it comes to scanning in the background; the articles below do a good job covering them and how to temporarily overcome them:-

Upvotes: 2

Related Questions