diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 55719bd..779f7ac 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -132,31 +132,29 @@ export class PoweredUP extends EventEmitter { let buf: Buffer = Buffer.alloc(0); device.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, (data: Buffer) => { buf = Buffer.concat([buf, data]); - const len = buf[0]; - if (len >= buf.length) { + while (buf[0] <= buf.length) { + const len = buf[0]; const message = buf.slice(0, len); buf = buf.slice(len); if (message[2] === 0x01 && message[3] === 0x0b) { - process.nextTick(() => { - switch (message[5]) { - case Consts.BLEManufacturerData.REMOTE_CONTROL_ID: - resolve(Consts.HubType.REMOTE_CONTROL); - break; - case Consts.BLEManufacturerData.MOVE_HUB_ID: - resolve(Consts.HubType.MOVE_HUB); - break; - case Consts.BLEManufacturerData.HUB_ID: - resolve(Consts.HubType.HUB); - break; - case Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID: - resolve(Consts.HubType.DUPLO_TRAIN_BASE); - break; - case Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB: - resolve(Consts.HubType.TECHNIC_MEDIUM_HUB); - break; - } - debug("Hub type determined"); - }); + switch (message[5]) { + case Consts.BLEManufacturerData.REMOTE_CONTROL_ID: + resolve(Consts.HubType.REMOTE_CONTROL); + break; + case Consts.BLEManufacturerData.MOVE_HUB_ID: + resolve(Consts.HubType.MOVE_HUB); + break; + case Consts.BLEManufacturerData.HUB_ID: + resolve(Consts.HubType.HUB); + break; + case Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID: + resolve(Consts.HubType.DUPLO_TRAIN_BASE); + break; + case Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB: + resolve(Consts.HubType.TECHNIC_MEDIUM_HUB); + break; + } + debug("Hub type determined"); } else { debug("Stashed in mailbox (LPF2_ALL)", message); device.addToCharacteristicMailbox(Consts.BLECharacteristic.LPF2_ALL, message); diff --git a/src/webbleabstraction.ts b/src/webbleabstraction.ts index 337c86a..8244210 100644 --- a/src/webbleabstraction.ts +++ b/src/webbleabstraction.ts @@ -99,11 +99,14 @@ export class WebBLEDevice extends EventEmitter implements IBLEAbstraction { return callback(buf); }; this._characteristics[uuid].addEventListener("characteristicvaluechanged", this._listeners[uuid]); - for (const data of this._mailbox) { + + const mailbox = Array.from(this._mailbox); + this._mailbox = []; + for (const data of mailbox) { debug("Replayed from mailbox (LPF2_ALL)", data); callback(data); } - this._mailbox = []; + this._characteristics[uuid].startNotifications(); }