title: How to Programmatically Clear Bluetooth Cache using GattServer
body: <p>I'm slightly familiar with BLE and I am facing some problem with an inherited code. So the app works like that:</p>
  
  <ol>
  <li>With BLE enabled the app scans for devices</li>
  <li>The app displays the devices found</li>
  <li>The user selects the device to pair with</li>
  <li>The app pairs with the device</li>
  </ol>
  
  <p>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.</p>
  
  <p>I'm using GattServer to connect with the client device, and I'm reseting the services as below:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>This post <a href="https://stackoverflow.com/questions/22596951/how-to-programmatically-force-bluetooth-low-energy-service-discovery-on-android">How to programmatically force bluetooth low energy service discovery on Android without using cache</a> 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.</p>
  
  <p>I'm asking you if there is a way to clear the cache programmatically using BluetoothGattServer.</p>