Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions godice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down