From efdd310a913553244ab4fbffce775b30eab67a30 Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Mon, 18 Nov 2024 04:54:38 +0100 Subject: [PATCH] Handle no devices found (#272) --- src/switchbot-ble.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/switchbot-ble.ts b/src/switchbot-ble.ts index 625317cb..fe7d4958 100644 --- a/src/switchbot-ble.ts +++ b/src/switchbot-ble.ts @@ -171,7 +171,11 @@ export class SwitchBotBLE extends EventEmitter { this.log('error', `discover stopScanningAsync error: ${JSON.stringify(e.message ?? e)}`) } } - return Object.values(peripherals) + const devices = Object.values(peripherals) + if (devices.length === 0) { + this.log('warn', 'No devices found during discovery.') + } + return devices } return new Promise((resolve, reject) => { @@ -192,7 +196,14 @@ export class SwitchBotBLE extends EventEmitter { this.noble.startScanningAsync(PRIMARY_SERVICE_UUID_LIST, false) .then(() => { - timer = setTimeout(async () => resolve(await finishDiscovery()), p.duration) + timer = setTimeout(async () => { + const result = await finishDiscovery() + if (result.length === 0) { + reject(new Error('No devices found during discovery.')) + } else { + resolve(result) + } + }, p.duration) }) .catch(reject) })