Skip to content

Commit

Permalink
[Feature]: Deactivate auto connect on disconnected peripherals
Browse files Browse the repository at this point in the history
  • Loading branch information
Marika-Fukazawa authored and chipweinberger committed Mar 19, 2024
1 parent 4b6e8e3 commit 97f9350
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class FlutterBluePlusPlugin implements
private final Map<String, BluetoothGatt> mCurrentlyConnectingDevices = new ConcurrentHashMap<>();
private final Map<String, BluetoothDevice> mBondingDevices = new ConcurrentHashMap<>();
private final Map<String, Integer> mMtu = new ConcurrentHashMap<>();
private final Map<String, Boolean> mAutoConnected = new ConcurrentHashMap<>();
private final Map<String, BluetoothGatt> mAutoConnected = new ConcurrentHashMap<>();
private final Map<String, String> mWriteChr = new ConcurrentHashMap<>();
private final Map<String, String> mWriteDesc = new ConcurrentHashMap<>();
private final Map<String, String> mAdvSeen = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -703,7 +703,7 @@ public void onMethodCall(@NonNull MethodCall call,

// remember autoconnect
if (autoConnect) {
mAutoConnected.put(remoteId, autoConnect);
mAutoConnected.put(remoteId, gatt);
} else {
mAutoConnected.remove(remoteId);
}
Expand All @@ -728,6 +728,20 @@ public void onMethodCall(@NonNull MethodCall call,
if (gatt == null) {
gatt = mConnectedDevices.get(remoteId);;
}
if (gatt == null) {
gatt = mAutoConnected.get(remoteId);
log(LogLevel.DEBUG, "already disconnected, but autoconnect deactivation is required");

// calling disconnect explicitly turns off autoconnect.
// this allows gatt resources to be reclaimed
mAutoConnected.remove(remoteId);

// cleanup
gatt.close();

result.success(false); // no work to do
return;
}
if (gatt == null) {
log(LogLevel.DEBUG, "already disconnected");
result.success(false); // no work to do
Expand Down

0 comments on commit 97f9350

Please sign in to comment.