@@ -163,6 +163,7 @@ class PbleConnectionManager implements ConnectionManager {
163163 int _intent = 0 ;
164164 Future <void >? _disposeFuture;
165165 final Set <Future <void >> _retirements = < Future <void >> {};
166+ final Map <String , Future <void >> _openings = < String , Future <void >> {};
166167
167168 @override
168169 Connection get connection => _facade;
@@ -241,17 +242,26 @@ class PbleConnectionManager implements ConnectionManager {
241242 // Do not open another board until those exact retirements complete.
242243 await Future .wait (_retirements.toList ());
243244 if (! _isCurrent (intent)) return ;
244- final Connection board = await _connectionFactory (id);
245- if (! _isCurrent (intent)) {
246- await _retire (board);
247- return ;
245+ if (_openings[id] case final Future <void > previous) {
246+ // Native wrappers with the same ID share one physical link. Finish
247+ // the stale acquisition AND its teardown before opening that ID again.
248+ // Its factory error belongs to its caller; a failed physical close is
249+ // independently retained in _retirements and must still block us.
250+ try {
251+ await previous;
252+ } catch (_) {}
253+ await Future .wait (_retirements.toList ());
254+ if (! _isCurrent (intent)) return ;
255+ }
256+ final Future <void > opening = Future <void >.microtask (
257+ () => _openAndPublish (id, intent),
258+ );
259+ _openings[id] = opening;
260+ try {
261+ await opening;
262+ } finally {
263+ if (identical (_openings[id], opening)) _openings.remove (id);
248264 }
249- _liveBoard = board;
250- _facade.attach (board);
251- // Derive the phase from the freshly attached board's live state (the
252- // listener also fires on the sync-up, but re-derive to cover a board that
253- // attaches already-ready with no state change).
254- _phase.value = _phaseForConn (_facade.state.value);
255265 } catch (error) {
256266 if (_isCurrent (intent)) {
257267 _lastError = error;
@@ -261,6 +271,18 @@ class PbleConnectionManager implements ConnectionManager {
261271 }
262272 }
263273
274+ Future <void > _openAndPublish (String id, int intent) async {
275+ if (! _isCurrent (intent)) return ;
276+ final Connection board = await _connectionFactory (id);
277+ if (! _isCurrent (intent)) {
278+ await _retire (board);
279+ return ;
280+ }
281+ _liveBoard = board;
282+ _facade.attach (board);
283+ _phase.value = _phaseForConn (_facade.state.value);
284+ }
285+
264286 @override
265287 Future <void > disconnect () async {
266288 _ensureActive ();
@@ -311,7 +333,10 @@ class PbleConnectionManager implements ConnectionManager {
311333 _facade.state.removeListener (_onFacadeState);
312334 _detachAndRetire ();
313335 _selected = null ;
314- await attempt (() => Future .wait (_retirements.toList ()));
336+ await Future .wait (< Future <void >> [
337+ attempt (_scanner.stopScan),
338+ attempt (() => Future .wait (_retirements.toList ())),
339+ ]);
315340 await attempt (_scanResults.close);
316341 await attempt (_facade.dispose);
317342 await attempt (_phase.dispose);
0 commit comments