@@ -14,7 +14,7 @@ import { DEFAULT_DISCOVERY_DURATION, PRIMARY_SERVICE_UUID_LIST } from './setting
1414 * SwitchBotBLE class to interact with SwitchBot devices.
1515 */
1616export 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