Skip to content

Commit

Permalink
Handle no devices found (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolson authored Nov 18, 2024
1 parent d7683ea commit efdd310
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/switchbot-ble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwitchbotDevice[]>((resolve, reject) => {
Expand All @@ -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)
})
Expand Down

0 comments on commit efdd310

Please sign in to comment.