How to Programmatically Clear Bluetooth Cache using GattServer
Asked Answered
K

1

25

I'm slightly familiar with BLE and I am facing some problem with an inherited code. So the app works like that:

  1. With BLE enabled the app scans for devices
  2. The app displays the devices found
  3. The user selects the device to pair with
  4. The app pairs with the device

The problem I'm facing is that after pairing several times (it varies) the phone is not able to discover devices, hence blocking the user to pair.

I'm using GattServer to connect with the client device, and I'm reseting the services as below:

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

Restarting the phone, turning bluetooth off and then back on, and uninstalling and installing the app won't fix the problem. The only solution is to clear the cache from the Bluetooth Share app on the android apps manager.

This post How to programmatically force bluetooth low energy service discovery on Android without using cache adresses to a similar problem but since we are not using BluetoothGatt to connect it's no a suitable solution. Neither will be to refactor the whole inherited code.

I'm asking you if there is a way to clear the cache programmatically using BluetoothGattServer.

Koran answered 19/1, 2017 at 21:13 Comment(5)
Did find an answer to this? I am facing a similar issue.Edo
@Edo looks like he is not online. did you find any solution to this? I have similar problem.Dualistic
We are also facing the same problem after the release of Android 10.Malvasia
@SanjayKakadiya did you find a solution for your case?Crowell
@Crowell Yes, we fixed the BLE connection problem. We scan the BLE device before connect.Malvasia
F
2

One solution - solve this issue using reflection.

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

Note : I did not recommending this way

Faiyum answered 1/2, 2019 at 7:47 Comment(1)
Google had added restrictions on non-SDK interfaces. Please have a look hereMalvasia

© 2022 - 2024 — McMap. All rights reserved.