21

Most of times it works great but sometimes i'm having this error while trying to discover BLE devices:

02-12 18:00:41.952  16178-16339/com.icrealtime.allie W/BleRpcConnectionFactory﹕ Starting discovery
02-12 18:00:41.955  16178-16339/com.icrealtime.allie D/BluetoothAdapter﹕ STATE_ON
02-12 18:00:41.957  24342-18813/? D/BtGatt.GattService﹕ registerClient() - UUID=c4a4c56d-1d10-4615-9c8d-44971bc3d6e6
02-12 18:00:41.957  24342-24384/? E/bt_btif﹕ Register with GATT stack failed.
02-12 18:00:41.957  24342-24384/? E/bt_btif﹕ Register with GATT stack failed.
02-12 18:00:41.957  24342-24370/? D/BtGatt.GattService﹕ onClientRegistered() - UUID=c4a4c56d-1d10-4615-9c8d-44971bc3d6e6, clientIf=0
02-12 18:00:41.958  16178-16190/com.icrealtime.allie D/BluetoothLeScanner﹕ onClientRegistered() - status=133 clientIf=0
02-12 18:00:41.967  16178-16178/com.icrealtime.allie E/BleRpcConnectionFactory﹕ BLE SCAN FAILED: 2

error code 2 stands for https://developer.android.com/reference/android/bluetooth/le/ScanCallback.html#SCAN_FAILED_APPLICATION_REGISTRATION_FAILED

It seems to be internal android issue, but it can be affected by my code doing something wrong. What can be the reason and how to walkaround it?

PS. Nexus 9, Android 6.0.1

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • Possible duplicate of [Solution for BLE scan's SCAN\_FAILED\_APPLICATION\_REGISTRATION\_FAILED?](https://stackoverflow.com/questions/27516399/solution-for-ble-scans-scan-failed-application-registration-failed) – tir38 Apr 19 '19 at 03:46

5 Answers5

2

I had the same issue and this worked for me. Might look like a silly fix but worked lol.

After adding the required Bluetooth and Location permission requirements in your manifest file...

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

You must turn on the FINE LOCATION or COARSE LOCATION permissions for the app. You could do this manually from the app settings on the device or add this bit of code to your onCreate() method.

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'll also need to define int REQUEST_LOCATION_ENABLE_CODE as 1.

Ahimsa Das
  • 111
  • 7
0

A possible workaround it might be to disable/enable the blueetooth programmatically. 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;
Dekra
  • 554
  • 5
  • 15
  • Unfortunately this doesn't seem to fix the issue. I'm seeing the same issue on both Android 6.0.1 and 7.1. My app was doing a continuous scan for long periods of time; this happened only twice in about a week. Toggling Bluetooth adapter off and on from the Settings app didn't fix the issue either, but what's interesting is that the Settings app can still perform a Bluetooth scan for surrounding peripherals, but all 3rd party apps that do scanning would get this error code of `SCAN_FAILED_APPLICATION_REGISTRATION_FAILED`. – Chee-Yi Aug 09 '17 at 04:08
  • 2
    Feel free to mark questions as duplicates instead of posting the same answer multiple places. – tir38 Mar 13 '18 at 01:47
0

Ideally, please look into the phone's location services support (Android 6+ BLE services require it be on for proper functionality). We've seen this issue on other occasions with Cordova plugins. Turn on location services (or check that these are turned on).

vsecades
  • 129
  • 2
  • 7
0

you need to add following permission in the manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-feature android:name="android.hardware.location.network"/>

If not the coarse location will not be able to access network and so the LeScan break up

0

Make sure you close the GattConnection if you disconnect and/or reconnect. Just disposing of the object doesn’t release underlying resources and you will soon reach a limit as there, to my understanding, are no more than 32 possible simultaneous clients across all apps.