From dfdc7b4927bbab8ee51aef4fc29603a8d3aad29b Mon Sep 17 00:00:00 2001 From: icanos Date: Tue, 14 Jan 2020 18:08:02 +0000 Subject: [PATCH] added catch for write error unknown peripheral --- plejd/ble.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/plejd/ble.js b/plejd/ble.js index 5f101b1..31cff5b 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -315,12 +315,22 @@ class PlejdService extends EventEmitter { return false; } - const encryptedData = this._encryptDecrypt(this.cryptoKey, this.deviceAddress, data); - this.characteristics.data.write(encryptedData, false); + try { + const encryptedData = this._encryptDecrypt(this.cryptoKey, this.deviceAddress, data); + this.characteristics.data.write(encryptedData, false); - let writeData; - while ((writeData = this.writeQueue.shift()) !== undefined) { - this.characteristics.data.write(this._encryptDecrypt(this.cryptoKey, this.deviceAddress, writeData), false); + let writeData; + while ((writeData = this.writeQueue.shift()) !== undefined) { + this.characteristics.data.write(this._encryptDecrypt(this.cryptoKey, this.deviceAddress, writeData), false); + } + } + catch (error) { + console.log('error: writing to plejd: ' + error); + console.log('will reconnect and try again.'); + this.writeQueue.push(data); + + this.disconnect(); + this.connect(); } }