我对 BLE 有点熟悉,并且我面临着继承代码的一些问题。所以该应用程序的工作方式如下:
- 启用 BLE 后,应用程序会扫描设备
- 该应用程序显示找到的设备
- 用户选择要配对的设备
- 该应用程序与设备配对
我面临的问题是,配对几次(情况有所不同)后,手机无法发现设备,从而阻止用户配对。
我正在使用 Gatt 服务器与客户端设备连接,并且我正在重置服务,如下所示:
public void resetBluetoothGattServer() {
Log.i(TAG," resetBluetoothGattServer: bluetoothGattServer: "+ bluetoothGattServer);
if (bluetoothGattServer != null) {
if(!bluetoothGattServer.getServices().isEmpty()){
Log.i(TAG," resetBluetoothGattServer: clearing services on bluetooth Gatt Server");
bluetoothGattServer.clearServices();
}
Log.i(TAG," resetBluetoothGattServer: closing bluetoothGattServer");
bluetoothGattServer.close();
}
bluetoothGattServer = openGattServer();
}
重新启动手机、关闭蓝牙然后重新打开以及卸载并安装应用程序都无法解决问题。唯一的解决方案是从 Android 应用程序管理器上的蓝牙共享应用程序清除缓存。
这个帖子如何在不使用缓存的情况下以编程方式强制 Android 上的蓝牙低功耗服务发现 https://stackoverflow.com/questions/22596951/how-to-programmatically-force-bluetooth-low-energy-service-discovery-on-android解决了类似的问题,但由于我们没有使用 BluetoothGatt 进行连接,因此它不是一个合适的解决方案。两者都不会重构整个继承的代码。
我问您是否有一种方法可以使用BluetoothGattServer 以编程方式清除缓存。
一种解决方案 - 使用反射解决此问题。
private void refreshDeviceCache(BluetoothGatt gatt) {
try {
Method localMethod = gatt.getClass().getMethod("refresh");
if(localMethod != null) {
localMethod.invoke(gatt);
}
} catch(Exception localException) {
Log.d("Exception", localException.toString());
}
}
注意:我不推荐这种方式
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)