8

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 Answer 1

12

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.

9
  • 1
    This does not work on Android 9+ developer.android.com/distribute/best-practices/develop/…
    – Alix
    Apr 12, 2019 at 13:46
  • Thanks for this info. I'll check this out ... You still have caching problem on Android 9? I not yet tested this version.
    – Domske
    Apr 12, 2019 at 14:45
  • 2
    After some investigation we concluded that our problem was the BLE peripheral that was causing the cache issues.According to the BLE spec a device that does not have a static set of GATT services should declare a service called Service Changed: bluetooth.com/specifications/gatt/…
    – Alix
    Apr 15, 2019 at 8:18
  • The BLE specs also declares that unbound devices must not be cached. But Android does it anyway. That's a bug. But anyway. I don't know when the cache bug will be fixed. BLE on Android is still a bit experiemental.
    – Domske
    Apr 15, 2019 at 8:32
  • 1
    I investigated this now and concluded that this works on Android 9 and 10. Apparantly refresh() is "unrestricted greylisted" (annotation @UnsupportedAppUsage) which means it should work
    – Alix
    Dec 3, 2019 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.