@ -1826,67 +1826,88 @@ public class FlutterBluePlusPlugin implements
}
}
// #794: this method mutates the connection maps and must be serialized
// against the GATT callbacks , which take mMethodCallMutex. A caller that
// #794: this method mutates the connection maps , which must be serialized
// against the GATT callbacks that take mMethodCallMutex. A caller that
// already holds the (non-reentrant) mutex passes callerHoldsMutex=true:
// flutterRestart runs inside onMethodCall, which holds it, and acquiring
// again there would deadlock Hot Restart.
//
// The critical section is kept short (pre-PR review, #794): under the lock
// we only snapshot the handles and clear the maps. The blocking
// gatt.disconnect()/close() IPC runs OUTSIDE the lock, so a teardown on the
// main thread (onDetachedFromEngine) can't stall behind a binder-thread
// callback holding the mutex while the Bluetooth stack works.
private void disconnectAllDevices ( String func , boolean callerHoldsMutex )
{
if ( ! callerHoldsMutex ) {
acquireMutex ( mMethodCallMutex ) ;
}
try {
log ( LogLevel . DEBUG , "disconnectAllDevices(" + func + ")" ) ;
// merge connected & autoconnected
Map < String , BluetoothGatt > allDevices = new HashMap < > ( ) ;
allDevices . putAll ( mConnectedDevices ) ;
allDevices . putAll ( mAutoConnected ) ;
// request disconnections
for ( BluetoothGatt gatt : allDevices . values ( ) ) {
if ( func = = "adapterTurnOff" ) {
// Note:
// - calling `disconnect` and `close` after the adapter
// is turned off is not necessary. It is implied.
// Calling them leads to a `DeadObjectException`.
// - But, we must make sure the disconnect callback is called.
// It's surprising but android does not invoke this callback itself.
// #794: call the locked core directly; the public callback
// would try to re-acquire the mutex this method holds.
log ( LogLevel . DEBUG , "disconnectAllDevices(" + func + ")" ) ;
if ( func = = "adapterTurnOff" ) {
// Adapter is off: no real IPC is possible (disconnect/close would
// throw DeadObjectException), but Android does NOT deliver the
// disconnect callbacks itself, so we synthesize them. That work
// mutates the maps and emits events, so it stays under the lock;
// it does no blocking IPC, so the section is bounded. Snapshot
// first because the locked core mutates the maps it iterates.
if ( ! callerHoldsMutex ) {
acquireMutex ( mMethodCallMutex ) ;
}
try {
Map < String , BluetoothGatt > allDevices = new HashMap < > ( ) ;
allDevices . putAll ( mConnectedDevices ) ;
allDevices . putAll ( mAutoConnected ) ;
for ( BluetoothGatt gatt : allDevices . values ( ) ) {
handleConnectionStateChangeLocked ( gatt , 0 , BluetoothProfile . STATE_DISCONNECTED ) ;
} else {
String remoteId = gatt . getDevice ( ) . getAddress ( ) ;
// disconnect
log ( LogLevel . DEBUG , "calling disconnect: " + remoteId ) ;
gatt . disconnect ( ) ;
// it is important to close after disconnection, otherwise we will
// quickly run out of bluetooth resources, preventing new connections
log ( LogLevel . DEBUG , "calling close: " + remoteId ) ;
gatt . close ( ) ;
}
clearAllConnectionMaps ( ) ;
} finally {
if ( ! callerHoldsMutex ) {
mMethodCallMutex . release ( ) ;
}
}
return ;
}
mConnectedDevices . clear ( ) ;
mCurrentlyConnectingDevices . clear ( ) ;
mBondingDevices . clear ( ) ;
mBondingPins . clear ( ) ;
mMtu . clear ( ) ;
mWriteChr . clear ( ) ;
mWriteDesc . clear ( ) ;
mAutoConnected . clear ( ) ;
// Other paths (onDetachedFromEngine, flutterRestart): snapshot the
// handles and clear the maps under the lock, then disconnect/close
// them outside it.
List < BluetoothGatt > toClose = new ArrayList < > ( ) ;
if ( ! callerHoldsMutex ) {
acquireMutex ( mMethodCallMutex ) ;
}
try {
toClose . addAll ( mConnectedDevices . values ( ) ) ;
toClose . addAll ( mAutoConnected . values ( ) ) ;
clearAllConnectionMaps ( ) ;
} finally {
if ( ! callerHoldsMutex ) {
mMethodCallMutex . release ( ) ;
}
}
// Blocking IPC, outside the critical section. Per-device try/catch so
// one DeadObjectException doesn't strand the remaining handles.
for ( BluetoothGatt gatt : toClose ) {
String remoteId = gatt . getDevice ( ) . getAddress ( ) ;
log ( LogLevel . DEBUG , "calling disconnect: " + remoteId ) ;
try { gatt . disconnect ( ) ; } catch ( Exception e ) { /* best effort */ }
// it is important to close after disconnection, otherwise we will
// quickly run out of bluetooth resources, preventing new connections
log ( LogLevel . DEBUG , "calling close: " + remoteId ) ;
try { gatt . close ( ) ; } catch ( Exception e ) { /* best effort */ }
}
}
// #794: caller MUST hold mMethodCallMutex.
private void clearAllConnectionMaps ( )
{
mConnectedDevices . clear ( ) ;
mCurrentlyConnectingDevices . clear ( ) ;
mBondingDevices . clear ( ) ;
mBondingPins . clear ( ) ;
mMtu . clear ( ) ;
mWriteChr . clear ( ) ;
mWriteDesc . clear ( ) ;
mAutoConnected . clear ( ) ;
}
int getAppearanceFromScanRecord ( ScanRecord adv ) {