@@ -121,18 +121,19 @@ export class HciAdapter extends Adapter {
121121 // For BLE <= 4.2:
122122 // - Disable advertising while we're connected.
123123 // - Don't connect if we have a connection in master mode
124+ let advertisingWasDisabled = false ;
124125 if ( this . hci . hciVersion < 8 ) {
125126 if ( [ ...this . connectedDevices . values ( ) ] . some ( ( d ) => d . isMaster ) ) {
126127 throw new Error ( `Connecting in master & slave role concurrently is only supported in BLE 5+` ) ;
127128 }
128129
129130 if ( this . advertising ) {
130- this . wasAdvertising = true ;
131131 try {
132132 await this . stopAdvertising ( ) ;
133+ this . wasAdvertising = true ;
134+ advertisingWasDisabled = true ;
133135 } catch ( err ) {
134136 this . emit ( 'error' , `Could not disable advertising before connecting: ${ err } ` ) ;
135- this . wasAdvertising = false ;
136137 }
137138 }
138139 }
@@ -150,7 +151,7 @@ export class HciAdapter extends Adapter {
150151 peripheral . onDisconnect ( ) ;
151152
152153 // Re-enable advertising since we didn't establish a connection
153- if ( this . wasAdvertising ) {
154+ if ( advertisingWasDisabled ) {
154155 await this . startAdvertising ( this . deviceName , this . advertisedServiceUUIDs ) ;
155156 this . wasAdvertising = false ;
156157 }
@@ -199,7 +200,11 @@ export class HciAdapter extends Adapter {
199200 return ;
200201 }
201202
202- await this . gap . stopAdvertising ( ) ;
203+ try {
204+ await this . gap . stopAdvertising ( ) ;
205+ } catch {
206+ // NO-OP: Errors here probably mean we already stopped advertising
207+ }
203208
204209 this . advertising = false ;
205210 }
0 commit comments