Skip to content

Commit b998bb9

Browse files
Merge pull request ParticulaCode#7 from Geek5510/main
Added disconnecting event, support for reconnecting and made connecting errors catchable
2 parents 14cf179 + 66509de commit b998bb9

2 files changed

Lines changed: 66 additions & 11 deletions

File tree

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ Requests:
9393
getBatteryLevel()
9494
```
9595
96+
```javascript
97+
/**
98+
* Attempts to recconect to dice's bluetooth device, incase the device is already connected
99+
* If the reconnection was successful an onDiceConnected event will follow
100+
**/
101+
attemptReconnect()
102+
96103
```javascript
97104
/**
98105
* Sets the die type for the die value calculations, Use GoDice.diceTypes.X for die type.
@@ -102,7 +109,22 @@ Requests:
102109
*/
103110
setDieType(GoDice.diceTypes)
104111
```
105-
112+
113+
```javascript
114+
/**
115+
* In order to catch error on the requestDevice and attemptReconnect methods use an async function and await the die's methods
116+
* Note: awaiting inside of the function will block it's execution
117+
* Example:
118+
*/
119+
async function openConnectionDialog() {
120+
const newDice = new GoDice();
121+
try {
122+
await newDice.requestDevice();
123+
} catch {
124+
console.log("Error on connecting die")
125+
}
126+
}
127+
```
106128

107129
Responses:
108130
----------
@@ -122,8 +144,26 @@ GoDice.prototype.onDiceConnected = (diceId, diceInstance) => {
122144
// die unique identifier
123145
let dieClass = diceInstance;
124146
};
125-
```
147+
```
148+
149+
```javascript
150+
/**
151+
* To recognize when a die has disconneted, override the function "onDiceDisconnected" in the GoDice class, with the following parameter:
152+
* @param {string} diceID - the die unique identifier
153+
* @param {GoDice class} diceInstance - the die class instance
154+
*/
155+
156+
// example:
126157
158+
GoDice.prototype.onDiceDisconnected = (diceId, diceInstance) => {
159+
// die unique identifier
160+
let dieIdentifier = diceID;
161+
162+
// die unique identifier
163+
let dieClass = diceInstance;
164+
};
165+
```
166+
127167
```javascript
128168
/**
129169
* To recognize the battery level, override the function "onBatteryLevel" in the GoDice class, with the following parameter:

godice.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,23 @@ class GoDice {
237237
onTiltStable(){};
238238
onMoveStable(){};
239239
onDiceConnected(){};
240+
onDiceDisconnected(){};
240241

241242
/******* API functions *******/
242243

243244
/**
244245
* Request for the die battery, that should follow by corresponding "BatteryLevel" event (response).
245246
*/
246247
getBatteryLevel() {
247-
console.log(this)
248+
console.debug(this)
248249
this.sendMessage([this.messageIdentifiers.BATTERY_LEVEL]);
249250
}
250251

251252
/**
252253
* Request for the die color, that should follow by corresponding "DiceColor" event (response).
253254
*/
254255
getDiceColor() {
255-
console.log(this)
256+
console.debug(this)
256257
this.sendMessage([this.messageIdentifiers.DICE_COLOUR]);
257258
}
258259

@@ -269,13 +270,30 @@ class GoDice {
269270
filters: [{ namePrefix: 'GoDice_' }],
270271
optionalServices: ['6e400001-b5a3-f393-e0a9-e50e24dcca9e']
271272
})
272-
.then(device => {
273+
.then(async device => {
273274
this.GlobalDeviceId = device.id.toString();
274-
this.bluetoothDevice = device;
275-
this.bluetoothDevice.addEventListener('gattserverdisconnected', this.onDisconnected);
276-
this.connectDeviceAndCacheCharacteristics();
275+
this.bluetoothDevice = device;
276+
var _self = this
277+
this.bluetoothDevice.addEventListener('gattserverdisconnected', function() {
278+
_self.onDiceDisconnected(_self.GlobalDeviceId, _self)
279+
})
280+
await this.connectDeviceAndCacheCharacteristics();
277281
});
278282
}
283+
284+
/**
285+
* Attempts to reconnect to the device incase of disconnect
286+
*/
287+
async attemptReconnect() {
288+
if (this.bluetoothDevice) {
289+
// This object's device exists
290+
if (this.bluetoothDevice.gatt.connected) {
291+
console.debug(this.GlobalDeviceId + "'s Bluetooth device is already connected")
292+
} else {
293+
await this.connectDeviceAndCacheCharacteristics()
294+
}
295+
}
296+
}
279297

280298
/**
281299
* Turn On/Off RGB LEDs, will turn off if led1 and led2 are null
@@ -548,7 +566,4 @@ class GoDice {
548566
this.bluetoothDevice = null;
549567
}
550568

551-
onDisconnected(event) {
552-
console.debug('> Bluetooth Device disconnected:' + event);
553-
}
554569
}

0 commit comments

Comments
 (0)