diff --git a/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java b/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java index f502c3c8..de82c1c2 100644 --- a/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java +++ b/src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java @@ -648,6 +648,11 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState Peripheral.this.dispatchToCommandCallback("onConnectionStateChange", () -> Peripheral.this.commandCallback.onConnectionStateChange(gatt, status, newState)); } + // Close it, or the client stays registered with Android and leaks. + if (newState == BluetoothGatt.STATE_DISCONNECTED && Peripheral.this.gatt == gatt) { + gatt.close(); + Peripheral.this.gatt = null; + } suppressDisconnectNotification = Peripheral.this.suppressDisconnectNotification; // A failed connect closes the gatt and clears `connected` even if the state was CONNECTED. nowConnected = Peripheral.this.connected;