【问题标题】:How to Programmatically Clear Bluetooth Cache using GattServer如何使用 GattServer 以编程方式清除蓝牙缓存
【发布时间】:2017-06-04 17:50:44
【问题描述】:

我对 BLE 有点熟悉,但我在继承代码方面遇到了一些问题。所以应用程序是这样工作的:

  1. 启用 BLE 后,应用会扫描设备
  2. 应用会显示找到的设备
  3. 用户选择要配对的设备
  4. 应用与设备配对

我面临的问题是多次配对后(它会有所不同),手机无法发现设备,从而阻止用户配对。

我正在使用 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();
}

重启手机、关闭蓝牙再打开、卸载再安装应用程序都无法解决问题。唯一的解决办法是从安卓应用管理器上的蓝牙共享应用清除缓存。

这篇帖子How to programmatically force bluetooth low energy service discovery on Android without using cache 解决了一个类似的问题,但由于我们没有使用 BluetoothGatt 进行连接,所以它不是一个合适的解决方案。两者都不会重构整个继承的代码。

我在问你是否有办法使用 BluetoothGattServer 以编程方式清除缓存。

【问题讨论】:

  • 找到答案了吗?我面临着类似的问题。
  • @IgorGanapolsky 看起来他不在线。你找到解决办法了吗?我也有类似的问题。
  • Android 10 发布后我们也面临同样的问题。
  • @SanjayKakadiya 你找到解决方案了吗?
  • @Keselme 是的,我们修复了 BLE 连接问题。我们在连接之前扫描 BLE 设备。

标签: android bluetooth bluetooth-lowenergy clear-cache bluetooth-peripheral


【解决方案1】:

一个解决方案 - 使用反射解决这个问题。

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

注意:我不推荐这种方式

【讨论】:

  • Google 增加了对非 SDK 接口的限制。请看here
猜你喜欢
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
相关资源
最近更新 更多