From bbcade6bb35ad3d28eb3050b38739d6df327aa5e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 29 Jan 2019 19:02:11 -0800 Subject: [PATCH] Wait for second advertisement packet before reading hub name --- src/hub.ts | 7 +++++-- src/poweredup.ts | 17 ++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/hub.ts b/src/hub.ts index 908ea3b..0cc9113 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -31,7 +31,7 @@ export class Hub extends EventEmitter { protected _ports: {[port: string]: Port} = {}; protected _characteristics: {[uuid: string]: Characteristic} = {}; - protected _name: string; + protected _name: string = ""; protected _firmwareInfo: IFirmwareInfo = { major: 0, minor: 0, bugFix: 0, build: 0 }; protected _batteryLevel: number = 100; protected _voltage: number = 0; @@ -46,7 +46,10 @@ export class Hub extends EventEmitter { this.autoSubscribe = !!autoSubscribe; this._peripheral = peripheral; this._uuid = peripheral.uuid; - this._name = peripheral.advertisement.localName; + // NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it + setTimeout(() => { + this._name = peripheral.advertisement.localName; + }, 200); } diff --git a/src/poweredup.ts b/src/poweredup.ts index 8d38d45..10640e7 100644 --- a/src/poweredup.ts +++ b/src/poweredup.ts @@ -154,13 +154,16 @@ export class PoweredUP extends EventEmitter { } }); - debug(`Hub ${hub.uuid} discovered`); - /** - * Emits when a Powered UP Hub device is found. - * @event PoweredUP#discover - * @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainBase} hub - */ - this.emit("discover", hub); + // NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it + setTimeout(() => { + debug(`Hub ${hub.uuid} discovered`); + /** + * Emits when a Powered UP Hub device is found. + * @event PoweredUP#discover + * @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainBase} hub + */ + this.emit("discover", hub); + }, 500); }