问题描述
我对 BLE 有点熟悉,但我在继承代码方面遇到了一些问题.所以这个应用程序是这样工作的:
I'm slightly familiar with BLE and I am facing some problem with an inherited code. So the app works like that:
- 启用 BLE 后,应用会扫描设备
- 该应用会显示找到的设备
- 用户选择要配对的设备
- 应用与设备配对
我面临的问题是,在多次配对(因人而异)后,手机无法发现设备,从而阻止用户进行配对.
The problem I'm facing is that after pairing several times (it varies) the phone is not able to discover devices, hence blocking the user to pair.
我正在使用 GattServer 连接客户端设备,我正在重置服务如下:
I'm using GattServer to connect with the client device, and I'm reseting the services as below:
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 应用管理器上的蓝牙共享应用清除缓存.
Restarting the phone, turning bluetooth off and then back on, and uninstalling and installing the app won't fix the problem. The only solution is to clear the cache from the Bluetooth Share app on the android apps manager.
这篇文章如何以编程方式强制蓝牙Android 上的低能耗服务发现不使用缓存 地址解决类似问题,但由于我们没有使用 BluetoothGatt 进行连接,因此它不是一个合适的解决方案.也不会重构整个继承的代码.
This post How to programmatically force bluetooth low energy service discovery on Android without using cache adresses to a similar problem but since we are not using BluetoothGatt to connect it's no a suitable solution. Neither will be to refactor the whole inherited code.
我在问您是否有办法使用 BluetoothGattServer 以编程方式清除缓存.
I'm asking you if there is a way to clear the cache programmatically using BluetoothGattServer.
推荐答案
一个解决方案 - 使用反射解决这个问题.
One solution - solve this issue using reflection.
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());
}
}
注意:我不推荐这种方式
这篇关于如何使用 GattServer 以编程方式清除蓝牙缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!