fragon fragon - 1 year ago 342
Android Question

Android stops finding BLE devices: onClientRegistered() - status=133 clientIf=0

I am developing an app in which I can both find and configure BLE devices. I am using standard Android BLE API, but recently I've encountered some strange problems.

When I turn on my app the BLE scan works OK. I am scanning using:

mBluetoothAdapter.startLeScan(mLeScanCallback); // for Kitkat and below


mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); // for Lollipop and above

In the Logcat I am getting following messages (I guess this is important for this issue):

D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5

In my app I can also read certain characteristics from my BLE devices (eg. battery state). I connect to a device to read this characteristic in a separate fragment using:

mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);

The characteristics are read correctly. In the
callback I also disconnect and close Gatt:


Each time I open a fragment to read a characteristic (no matter whether it is the same device or not)
value increases. I can see in the LogCat:

D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10

Everything works fine until the
value equals 10. BLE scan stops finding any devices, I can't connect to any of my devices to read any characteristics etc. And the LogCat infinitely displays these messages:

D/BluetoothGatt: unregisterApp() - mClientIf=0
D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0

The only way to fix it is to kill the app and relaunch it or restart Bluetooth by turning it off and on manually. I've encountered this issue only on certain devices such as Xperia Z1 (Android KitKat) and Galaxy S4 (Android Lollipop). I couldn't reproduce this issue on Xperia Z3 Compact running Android Marshmallow...

Is there anything I can do about it? I've already tried multiple "solutions" such as - calling all BLE methods from the UI thread and closing/disconnecting GATT, but nothing helps. How can I fix it?

Answer Source

I will answer my own question, because propably it will help someone with the same problem.

First of all I couldn't fix the problem with Bluetooth crashing after mClientIf value reached 10. However I found a workaround which helped in my case.

Even though I was stopping beacon search in the first Fragment and starting characteristic read in another, BLE API apparently didn't stop the search immediately and after opening the next Fragment the system was creating another "client".

This is why I need to wait some time after leaving the first Fragment and before starting to read the characteristic in another.

This method is called in onCreateView of the "new" Fragment (using postDelayed helped me to fix the problem):

private void getBatteryCharacteristic() {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            public void run() {
                mBeaconBatteryStateReader = new BeaconBatteryStateReader(
        }, 100);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download