Skip to content

Commit

Permalink
ble/sagas: avoid Linux 5 second disconnect when possible
Browse files Browse the repository at this point in the history
If the user did not request disconnection, then we know this is a true
disconnect event and not an early one so we don't need the delay hack
in that case.

Issue: pybricks/support#600
Issue: pybricks/support#1021
  • Loading branch information
dlech committed Apr 1, 2023
1 parent 73248fe commit 3baaff5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/ble/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2022 The Pybricks Authors
// Copyright (c) 2020-2023 The Pybricks Authors
//
// Manages connection to a Bluetooth Low Energy device running Pybricks firmware.

Expand Down Expand Up @@ -434,14 +434,21 @@ function* handleBleConnectPybricks(): Generator {
// wait for disconnection
yield* take(disconnectChannel);

// HACK: Disconnection event comes early on Linux, so scanning again
// can show that the previous connection is still "paired" and trying
// to select it results in infinite wait. To work around this, we need
// to wait long enough for BlueZ to actually disconnect the device.
// HACK: Disconnection event comes early on Linux when server.disconnect()
// is called, so scanning again can show that the previous connection is
// still "paired" and trying to select it results in an infinite wait.
// To work around this, we need to wait long enough for BlueZ to actually
// disconnect the device.
// https://github.com/pybricks/support/issues/600#issuecomment-1286606624
// istanbul ignore if
if (process.env.NODE_ENV !== 'test' && isLinux()) {
yield* delay(5000);
const wasDisconnectRequestedByUser = yield* select(
(s: RootState) => s.ble.connection === BleConnectionState.Disconnecting,
);

if (wasDisconnectRequestedByUser) {
yield* delay(5000);
}
}

yield* put(bleDidDisconnectPybricks());
Expand Down

0 comments on commit 3baaff5

Please sign in to comment.