Solution for BLE scan's SCAN_FAILED_APPLICATION_REGISTRATION_FAILED?

对着背影说爱祢 提交于 2019-11-28 10:57:13

When you got the error

SCAN_FAILED_APPLICATION_REGISTRATION_FAILED

You should disable the BluetoothAdapter

BluetoothAdapter.getDefaultAdapter().disable();

Disabling BluetoothAdapter, the event STATE_TURNING_OFF is fired. Once this event is fired, try to reconnect to the BluetoothAdapter:

case BluetoothAdapter.STATE_OFF:
  Log.d(TAG, "bluetooth adapter turned off");
  handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Log.d(TAG, "bluetooth adapter try to enable");
        BluetoothAdapter.getDefaultAdapter().enable();
    }}, 500);
  break;

It turns out that Bluetooth LE requires the following Android application permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!--BLE scanning is commonly used to determine a user's location with Bluetooth LE beacons. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- if your app targets API level 21 or higher. -->
<uses-feature android:name="android.hardware.location.gps" />

<!--app is available to BLE-capable devices only. -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

Besides on main activity:

// onResume()
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
        android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
} else {
    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
            REQUEST_LOCATION_ENABLE_CODE);
}

You should perform operations only success initialization of BT adapter. To be sure that it is ready create intent filter:

val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)

and broadcast receiver(you will perform action only if adapter is ready):

val broadcastReceiver = object: BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent?) {
                val action = intent?.action
                if (action != null && action == BluetoothAdapter.ACTION_STATE_CHANGED) {
                    val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
                    when (state) {
                        BluetoothAdapter.STATE_ON -> {
                            if (bluetoothAdapter.isEnabled) {
                               //perform your task here
                            }
                        }
                        BluetoothAdapter.STATE_OFF -> {}
                    }
                }
            }
        }

then register receiver:

registerReceiver(broadcastReceiver, filter)

and relaunch adapter(this part can be replaces with check):

bluetoothAdapter.disable()
bluetoothAdapter.enable()

DONE!

I had this happen to me today. While manually disabling and then enabling BT in the Android Settings did not fix this, I was able to get it working after only disabling it manually and then have the app that is affected by the issue enable BT.

The app then pops up an Android System message "An app is requesting permission to turn on BT" (I have a German UI, so it may be worded differently), and when I then press allow, the app finally has proper access to BT and this error no longer shows.

Sounds like a bug or something.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!