2

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

KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33
  • 1
    *Do we need to change the way we are scanning* - How we can know that without seeing the relevant scanning code logic? What about the runtime permissions? Do you handle them properly? Have a look at [this answer](https://stackoverflow.com/a/72842249/12749998) to check whether you handle runtime permissions correctly. – Kozmotronik Jan 27 '23 at 06:12

1 Answers1

2

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

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
  • Haven't all these permission settings changed for Bluetooth in Android 12? – Emil Jan 25 '23 at 07:48
  • I'm not sure, I believe they are still required (especially the ACCESS_BACKGROUND_LOCATION) but perhaps there were other changes that I am not aware of. This is a comprehensive link from the Android Developer website that includes the permissions needed for Android 12 and higher: https://developer.android.com/guide/topics/connectivity/bluetooth/permissions – Youssif Saeed Jan 26 '23 at 07:35