Android停止查找BLE设备:onClientRegistered()-status = 133 clientIf = 0 [英] Android stops finding BLE devices: onClientRegistered() - status=133 clientIf=0

查看:1024
本文介绍了Android停止查找BLE设备:onClientRegistered()-status = 133 clientIf = 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可以同时查找和配置BLE设备的应用程序。我正在使用标准的Android BLE API,但最近遇到了一些奇怪的问题。



当我打开应用程序时,BLE扫描正常。我正在使用以下方式进行扫描:

  mBluetoothAdapter.startLeScan(mLeScanCallback); //对于Kitkat及以下

  mBluetoothAdapter.getBluetoothLeScanner()。startScan(mScanCallback); //对于棒棒糖及以上

在Logcat中,我收到以下消息(我想这对于此问题):

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

在我的应用程序中,我还可以从BLE设备中读取某些特征(例如电池状态)。我连接到设备以使用以下单独的片段读取此特征:

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

已正确读取特征。在 onCharacteristicRead 回调中,我还断开并关闭Gatt:

  mBluetoothGatt。断开(); 
mBluetoothGatt.close();

每次我打开一个片段读取特征(无论它是否是同一设备) ) clientIf 值增加。我可以在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

一切正常,直到 clientIf 值等于10。BLE扫描停止查找任何设备,我无法连接到任何设备以读取任何特征等。LogCat无限显示以下消息:

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

修复该问题的唯一方法是终止该应用程序,然后重新启动或重启蓝牙手动关闭和开启。我仅在某些设备上遇到过此问题,例如Xperia Z1(Android KitKat)和Galaxy S4(Android Lollipop)。我无法在运行Android Marshmallow的Xperia Z3 Compact上重现此问题...



我能对此做些什么?我已经尝试了多种解决方案,例如-从UI线程调用所有BLE方法并关闭/断开GATT,但没有任何帮助。我该如何解决?

解决方案

我会回答我自己的问题,因为它可能会帮助遇到相同问题的人。 / p>

首先,在 mClientIf 值达到之后,我无法解决蓝牙崩溃的问题10 。但是,我发现了一种解决方法,对我的情况有所帮助。



即使我在第一个 Fragment 中停止了信标搜索并开始在另一个中读取特征,但BLE API显然没有不要立即停止搜索,在打开下一个 Fragment 之后,系统正在创建另一个客户端。



这就是为什么我需要在离开第一个 Fragment 之后并开始阅读其中的特征之前要等待一段时间另一个。



此方法在新 片段 onCreateView 中调用$ c>(使用 postDelayed 帮助我解决了问题):

 私有无效getBatteryCharacteristic(){
new Handler(Looper.getMainLooper())。postDelayed(new Runnable(){
@Override
public void run(){
mBeaconBatteryStateReader = new BeaconBatteryStateReader(
BeaconDetailsActivity.this,
mBeacon.getMacAddress());
mBeaconBatteryStateReader.readBatteryState(BeaconDetailsActivity.this);
}
},100);
}


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?

解决方案

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

这篇关于Android停止查找BLE设备:onClientRegistered()-status = 133 clientIf = 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆