I'm slightly familiar with BLE and I am facing some problem with an inherited code. So the app works like that:
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.
Step 1: Open Settings and go to 'Apps'. Step 2: Next, go to the Bluetooth option and select it. Step 3: Tap on 'Storage' and 'Clear Cache'. Point to note: You need to restart your device and then start pairing it with other Bluetooth devices.
Note: Clearing Bluetooth cache will cause the Bluetooth configuration settings for all your Bluetooth devices to be lost. In a sense, you're resetting Bluetooth on your device and "starting over" fresh. Do this as a last resort and at your own risk. Choose the option to "Display system apps".
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 am not recommended this way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With