My Android app scans BLE devices, and from a certain point it start to fails with error code 2 (ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED). I'm using Nexus 9, 5.0.1 Lollipop.
This problem continued even after I relaunched the app, and when I restarted the Bluetooth service from Settings, I could finally get rid of the problem. But this problem is recurring, and I think I'm coding in a wrong way; BLE related APIs are new and there is few information.
Does anyone know a general solution for this error, preferably not requiring restart of the Bluetooth service? Even though this error code is documented in Android API reference, I don't know how to handle it properly.
When you got the error
You should disable the BluetoothAdapter
Disabling BluetoothAdapter, the event STATE_TURNING_OFF is fired. Once this event is fired, try to reconnect to the BluetoothAdapter:
You should perform operations only success initialization of BT adapter. To be sure that it is ready create intent filter:
and broadcast receiver(you will perform action only if adapter is ready):
then register receiver:
and relaunch adapter(this part can be replaces with check):
DONE!
It turns out that Bluetooth LE requires the following Android application permissions in AndroidManifest.xml:
Besides on main activity: