Skip to content

Commit 83bea71

Browse files
committed
Check for powered on state in initialize
1 parent faf97bf commit 83bea71

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/switchbot-ble.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ describe('switchBotBLE', () => {
77
it('should create an instance without parameters', () => {
88
const switchbot = new SwitchBotBLE()
99
expect(switchbot).toBeInstanceOf(SwitchBotBLE)
10-
expect(switchbot.ready).toBeInstanceOf(Promise)
10+
expect(switchbot.nobleInitialized).toBeInstanceOf(Promise)
1111
})
1212

1313
it('should create an instance with parameters', () => {
1414
const params = { duration: 5000 }
1515
const switchbot = new SwitchBotBLE(params)
1616
expect(switchbot).toBeInstanceOf(SwitchBotBLE)
17-
expect(switchbot.ready).toBeInstanceOf(Promise)
17+
expect(switchbot.nobleInitialized).toBeInstanceOf(Promise)
1818
})
1919
})
2020

src/switchbot-ble.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { DEFAULT_DISCOVERY_DURATION, PRIMARY_SERVICE_UUID_LIST } from './setting
1414
* SwitchBotBLE class to interact with SwitchBot devices.
1515
*/
1616
export class SwitchBotBLE extends EventEmitter {
17-
public ready: Promise<void>
17+
public nobleInitialized: Promise<void>
1818
public noble: any
1919
ondiscover?: ondiscover
2020
onadvertisement?: onadvertisement
@@ -26,7 +26,7 @@ export class SwitchBotBLE extends EventEmitter {
2626
*/
2727
constructor(params?: Params) {
2828
super()
29-
this.ready = this.initialize(params)
29+
this.nobleInitialized = this.initialize(params)
3030
}
3131

3232
/**
@@ -56,6 +56,13 @@ export class SwitchBotBLE extends EventEmitter {
5656
} else {
5757
this.noble = (await import('@stoprocent/noble')).default
5858
}
59+
60+
try {
61+
await this.noble.waitForPoweredOnAsync()
62+
this.log(LogLevel.DEBUG, 'Noble powered on')
63+
} catch (e: any) {
64+
this.log(LogLevel.ERROR, `Failed waiting for powered on: ${JSON.stringify(e.message ?? e)}`)
65+
}
5966
} catch (e: any) {
6067
this.log(LogLevel.ERROR, `Failed to import noble: ${JSON.stringify(e.message ?? e)}`)
6168
}
@@ -90,8 +97,6 @@ export class SwitchBotBLE extends EventEmitter {
9097
quick: { required: false, type: 'boolean' },
9198
})
9299

93-
await this.noble.waitForPoweredOnAsync()
94-
95100
if (!this.noble) {
96101
throw new Error('Noble BLE library failed to initialize properly')
97102
}
@@ -264,14 +269,12 @@ export class SwitchBotBLE extends EventEmitter {
264269
* @returns {Promise<void>} - Resolves when scanning starts successfully.
265270
*/
266271
public async startScan(params: Params = {}): Promise<void> {
267-
await this.ready
272+
await this.nobleInitialized
268273
await this.validate(params, {
269274
model: { required: false, type: 'string', enum: Object.values(SwitchBotBLEModel) },
270275
id: { required: false, type: 'string', min: 12, max: 17 },
271276
})
272277

273-
await this.noble.waitForPoweredOnAsync()
274-
275278
if (!this.noble) {
276279
throw new Error('noble object failed to initialize')
277280
}

0 commit comments

Comments
 (0)