如何刷新服务/清除缓存? [英] How to refresh services / clear cache?

查看:134
本文介绍了如何刷新服务/清除缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些android设备上(无论操作系统或蓝牙版本如何),连接BLE设备后都会出现问题.服务/特征不是最新的.这通常在外围设备更改其服务时发生.(当应用未连接时)

On some android devices (regardless of the OS or Bluetooth version) there is a problem after BLE device connect. The services / characteristics are not up to date. This usually happens when the peripheral changes his services. (while the app was not connected)

在这种状态下,无法使用该设备.要验证此问题,您可以发现所有特征,并看到从android设备的缓存中加载了过时的特征(不再存在).

In this state, it is not possible to use the device. To verify this issue you can discover all characteristics and you see that there are outdated (no more existing) characteristics loaded from cache of the android device.

当前解决方案(非编程方式)在android的os系统设置中重置蓝牙启用状态.(关闭并打开蓝牙状态)

Current solution (not programmatically) Reset the bluetooth enable state in the os system settings of android. (turn off and on the bluetooth state)

nRF Connect 应用程序中还有一个名为"刷新服务"的功能.

Also there is a feature in the nRF Connect app called Refresh services

(忽略解析已知特征",这不是问题)该项目(nRF Connect)不是开源的.我不知道如何刷新服务"/清除缓存"以避免在连接时加载错误的服务/特征.

(Ignore "Parse known characteristics" This is not the problem) This project (nRF Connect) is not open source. I don't know how to "Refresh Services" / "Clear Cache" to avoid to load wrong services / characteristics on connect.

如何在android(java)中实现此功能?

How to implement this in android (java)?

背景:我正在将ionic与本机ble插件一起使用.我可以直接在插件中实现本机代码.

Background: I'm using ionic with the native ble plugin. I could implement native code directly in the plugin.

推荐答案

通常,Android不应缓存未绑定的设备.但是它会忽略规则.要刷新缓存,请使用反射调用隐藏方法.

Usually Android should not cache not-bonded devices. BUT it ignores the rule. To refresh the cache, call a hidden methode using reflections.

import java.lang.reflect.Method;

使用一种方法执行此操作:

Do this in a method:

try {
  // BluetoothGatt gatt
  final Method refresh = gatt.getClass().getMethod("refresh");
  if (refresh != null) {
    refresh.invoke(gatt);
  }
} catch (Exception e) {
  // Log it
}

用法示例:

如果您发现特征高速缓存存在问题.

If you see a poblem with the characteristic cache.

  1. 调用该方法清除缓存.(等待几秒钟).
  2. 重新连接(断开连接->连接).

现在应该修复.

注意:刷新方法没有完整的回调.

NOTE: The refresh method has no complete callback.

这篇关于如何刷新服务/清除缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆