From a86967573c35fc9b5e2bc1786ab425db9e603207 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 15 Jul 2022 17:24:47 -0500 Subject: [PATCH] ble: add 1 second delay after connecting This might help with some people that have connection issues where the OS is still busy enumerating the device when we are trying to read characteristics. --- src/ble/sagas.ts | 7 +++++++ src/lwp3-bootloader/sagas-ble.ts | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ble/sagas.ts b/src/ble/sagas.ts index 5e6b258c..e6622b18 100644 --- a/src/ble/sagas.ts +++ b/src/ble/sagas.ts @@ -10,6 +10,7 @@ import { Task, buffers, eventChannel } from 'redux-saga'; import { call, cancel, + delay, fork, put, select, @@ -164,6 +165,12 @@ function* handleBleConnectPybricks(): Generator { defer.push(() => server.disconnect()); + // istanbul ignore if + if (process.env.NODE_ENV !== 'test') { + // give OS Bluetooth stack some time to settle + yield* delay(1000); + } + const deviceInfoService = yield* call(() => server.getPrimaryService(deviceInformationServiceUUID).catch((err) => { if ( diff --git a/src/lwp3-bootloader/sagas-ble.ts b/src/lwp3-bootloader/sagas-ble.ts index 57dadde0..b5fe3860 100644 --- a/src/lwp3-bootloader/sagas-ble.ts +++ b/src/lwp3-bootloader/sagas-ble.ts @@ -4,7 +4,15 @@ // Handles Bluetooth Low Energy connection to LEGO Wireless Protocol v3 Bootloader service. import { END, eventChannel } from 'redux-saga'; -import { call, cancel, put, spawn, takeEvery, takeMaybe } from 'typed-redux-saga/macro'; +import { + call, + cancel, + delay, + put, + spawn, + takeEvery, + takeMaybe, +} from 'typed-redux-saga/macro'; import { alertsShowAlert } from '../alerts/actions'; import { ensureError } from '../utils'; import { @@ -99,6 +107,12 @@ function* handleConnect(): Generator { return; } + // istanbul ignore if + if (process.env.NODE_ENV !== 'test') { + // give OS Bluetooth stack some time to settle + yield* delay(1000); + } + let service: BluetoothRemoteGATTService; try { service = yield* call([server, 'getPrimaryService'], lwp3BootloaderServiceUUID);