diff --git a/godice.js b/godice.js index 3b4054f..150fdf5 100644 --- a/godice.js +++ b/godice.js @@ -284,16 +284,21 @@ class GoDice { /** * Attempts to reconnect to the device incase of disconnect */ - async attemptReconnect() { - if (this.bluetoothDevice) { - // This object's device exists - if (this.bluetoothDevice.gatt.connected) { - console.debug(this.GlobalDeviceId + "'s Bluetooth device is already connected") - } else { - await this.connectDeviceAndCacheCharacteristics() - } + async attemptReconnect(diceId, diceInstance) { + while (!this.bluetoothDevice.gatt.connected) { + console.log("Reconnecting to: ", diceId); + try { + await this.connectDeviceAndCacheCharacteristics() + } catch (error) { + console.error("Error while trying to reconnect: ", error) + } + if (!this.bluetoothDevice.gatt.connected) { + await new Promise(resolve => setTimeout(resolve, 1000)) + } } - } + console.log(diceId + " reconnected!") + } + /** * Turn On/Off RGB LEDs, will turn off if led1 and led2 are null diff --git a/main.js b/main.js index 1ef4931..de9de04 100644 --- a/main.js +++ b/main.js @@ -157,6 +157,18 @@ GoDice.prototype.onRollStart = (diceId) => { diceIndicatorEl.textContent = "Rollling...."; }; +GoDice.prototype.onDiceDisconnected = (diceId, diceInstance) => { + console.log("Roll Start: ", diceId); + + // get rolling indicator + const diceIndicatorEl = document.getElementById(diceId + "-die-status"); + + // show rolling + diceIndicatorEl.textContent = "disconnected"; + // Attempt to reconnect + diceInstance.attemptReconnect(diceId, diceInstance); + }; + GoDice.prototype.onStable = (diceId, value, xyzArray) => { console.log("Stable event: ", diceId, value);