Skip to content

Commit efdd310

Browse files
authored
Handle no devices found (#272)
1 parent d7683ea commit efdd310

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/switchbot-ble.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ export class SwitchBotBLE extends EventEmitter {
171171
this.log('error', `discover stopScanningAsync error: ${JSON.stringify(e.message ?? e)}`)
172172
}
173173
}
174-
return Object.values(peripherals)
174+
const devices = Object.values(peripherals)
175+
if (devices.length === 0) {
176+
this.log('warn', 'No devices found during discovery.')
177+
}
178+
return devices
175179
}
176180

177181
return new Promise<SwitchbotDevice[]>((resolve, reject) => {
@@ -192,7 +196,14 @@ export class SwitchBotBLE extends EventEmitter {
192196

193197
this.noble.startScanningAsync(PRIMARY_SERVICE_UUID_LIST, false)
194198
.then(() => {
195-
timer = setTimeout(async () => resolve(await finishDiscovery()), p.duration)
199+
timer = setTimeout(async () => {
200+
const result = await finishDiscovery()
201+
if (result.length === 0) {
202+
reject(new Error('No devices found during discovery.'))
203+
} else {
204+
resolve(result)
205+
}
206+
}, p.duration)
196207
})
197208
.catch(reject)
198209
})

0 commit comments

Comments
 (0)