如何使用GattServer编程地清除蓝牙缓存

25

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

重启手机、关闭并重新开启蓝牙,卸载并重新安装应用程序都无法解决问题。唯一的解决方案是清除 Android 应用程序管理器中 Bluetooth Share 应用程序的缓存。

这篇帖子 如何在 Android 上通过编程方式强制执行蓝牙低功耗服务发现而不使用缓存 解决了类似的问题,但由于我们没有使用 BluetoothGatt 进行连接,因此它不是一个合适的解决方案。重构整个继承代码也不是一个好方法。

我想问您是否有一种使用 BluetoothGattServer 编程方式清除缓存的方法。


4
找到答案了吗?我也遇到了类似的问题。 - IgorGanapolsky
@IgorGanapolsky看起来不在线。你找到任何解决办法了吗?我有类似的问题。 - Alican Uzun
在Android 10发布后,我们也遇到了同样的问题。 - Sanjay Kakadiya
@SanjayKakadiya,你找到解决方案了吗? - Keselme
@Keselme 是的,我们已经解决了BLE连接问题。我们在连接之前扫描BLE设备。 - Sanjay Kakadiya
1个回答

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

注意:我不推荐这种方式。

2
Google已对非SDK接口添加了限制。请查看此处 - Sanjay Kakadiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接