'How to solve SCAN_FAILED_APPLICATION_REGISTRATION_FAILED
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:
- I open the app and I start the foreground service
- I close the application (the foreground service is still running)
- The foreground service scan and connect to my device and then I stop scanning
- I turn off my BLE device and the foreground service start to scan again
- I turn on my ble device and the foreground service detect it and connect to it.
- Repeat step 4 and suddenly the error
SCAN_FAILED_APPLICATION_REGISTRATION_FAILEDshow.
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?
Solution 1:[1]
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;
Solution 2:[2]
Your dbTables variable is not defined. Compiler didn't catch this earlier because you used the non-null assertion operator (bang operator: !).
Instead of using this assertion you should adjust behaviour of your program, for example:
// check first if variable is defined
if (dbTables && dbTables.length) {}
Same but shorter:
if (dbTables?.length) {}
Your case is perfect example of why overusing non-null assertion operator can be dangerous for your code base. We should use it only when for some reason we have better knowledge of types than the compiler, which is rather rare.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? |
| Solution 2 | tymzap |
