我对BLE有一些了解,但面临一个继承代码的问题。应用程序的工作方式如下:
- 开启BLE后,应用程序会扫描设备
- 应用程序显示已找到的设备
- 用户选择要配对的设备
- 应用程序与设备配对
我遇到的问题是,在配对几次后(变化不确定),手机无法发现设备,因此阻止了用户进行配对。
我正在使用GattServer与客户端设备连接,并按以下方式重置服务:
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 应用程序管理器中 Bluetooth Share 应用程序的缓存。
这篇帖子 如何在 Android 上通过编程方式强制执行蓝牙低功耗服务发现而不使用缓存 解决了类似的问题,但由于我们没有使用 BluetoothGatt 进行连接,因此它不是一个合适的解决方案。重构整个继承代码也不是一个好方法。
我想问您是否有一种使用 BluetoothGattServer 编程方式清除缓存的方法。