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

a 夏天 提交于 2019-12-04 01:37:43

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);
    }

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.

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