How to refresh services / clear cache?

蓝咒 提交于 2021-02-16 09:15:08

问题


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)

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.

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

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

(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.

How to implement this in android (java)?

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


回答1:


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
}

Usuage example:

If you see a poblem with the characteristic cache.

  1. Call the method to clear the cache. (Wait a few seconds).
  2. Reconnect (Disconnect -> Connect).

Should be fixed now.

NOTE: The refresh method has no complete callback.



来源:https://stackoverflow.com/questions/50739085/how-to-refresh-services-clear-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!