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

旧街凉风 提交于 2019-12-05 15:52:52

问题


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

and

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 onCharacteristicRead callback I also disconnect and close Gatt:

mBluetoothGatt.disconnect();
mBluetoothGatt.close();

Each time I open a fragment to read a characteristic (no matter whether it is the same device or not) clientIf 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 clientIf 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?


回答1:


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() {
            @Override
            public void run() {
                mBeaconBatteryStateReader = new BeaconBatteryStateReader(
                        BeaconDetailsActivity.this,
                        mBeacon.getMacAddress());
                mBeaconBatteryStateReader.readBatteryState(BeaconDetailsActivity.this);
            }
        }, 100);
    }



回答2:


Closing the gatt object is the correct way to release the resources. If it still doesn't work there is a bug in Android on that particular phone.

To minimize the chance to reach the limit, you could make sure you never have more than one gatt object per device.



来源:https://stackoverflow.com/questions/39646253/android-stops-finding-ble-devices-onclientregistered-status-133-clientif-0

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