我正在开发一个应用程序,其中我可以找到并配置BLE设备。 我正在使用标准的Android BLE API,但最近遇到了一些奇怪的问题。
当我打开我的应用程序时,BLE扫描正常工作。 我正在使用以下方式进行扫描:
mBluetoothAdapter.startLeScan(mLeScanCallback); // for Kitkat and below
和
mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); // for Lollipop and above
在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.disconnect();
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,但都没有帮助。我该怎么办才能解决这个问题?