0

I am trying to perform BLE scans in a foreground service (Android 12) after some start scan end stop scan I receive the error SCAN_FAILED_APPLICATION_REGISTRATION_FAILED, I cannot figure out what is the reason of the error.

I do startScan and stopScan in sequence (the scan is a long-running scan)

The app target SDK 32 so I have the new Bluetooth permissions.

This is the complete scenario:

  1. I open the app and I start the foreground service
  2. I close the application (the foreground service is still running)
  3. The foreground service scan and connect to my device and then I stop scanning
  4. I turn off my BLE device and the foreground service start to scan again
  5. I turn on my ble device and the foreground service detect it and connect to it.
  6. Repeat step 4 and suddenly the error SCAN_FAILED_APPLICATION_REGISTRATION_FAILED show.

I also notice this error in the logcat App 'com.sample.xxx' is scanning too much clientif (I look into AOSP source code and seems that this string is specific of Xiaomi ROM)

https://github.com/appersiano/TestingBackgroundConnection

P.S. Tested on Xiaomi Mi11 Lite 5g

How can I solve?

appersiano
  • 2,670
  • 22
  • 42

1 Answers1

0

you should not scan more than 5 times in 30s.

GattService: This constant defines the time window an app can scan multiple times. Any single app can scan up to |NUM_SCAN_DURATIONS_KEPT| times during this window. Once they reach this limit, they must wait until their earliest recorded scan exits this window.

static final int NUM_SCAN_DURATIONS_KEPT = 5;
static final long EXCESSIVE_SCANNING_PERIOD_MS = 30 * 1000;
戴文锦
  • 1,985
  • 1
  • 7
  • 5