From 92e43042a77e72682d7bbb981955c76acfc6b2dc Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Sun, 17 Nov 2024 19:10:01 +0100 Subject: [PATCH] Handle no devices found --- 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) })