-
Notifications
You must be signed in to change notification settings - Fork 19
Description
Just got a pair of GoDice and am attempting to connect it to p5.js via P5LIVE to use the dice for generative live-coding = working right out of the box! Main issue is whenever I have to do a complete recompile (ie, rebuilding the whole page/sketch within an iframe) I lose the connection to the dice and have to call openConnectionDialog()
again. I see the code within the library for an attempt to reconnect – but I'm guessing that's only relevant if the devices have been connected and it got interrupted (distance or battery). I'm especially interested to keep persistence across website refreshes, so upon researching, found the following:
Unfortunately persistent bluetooth is still hidden behind a Chromium experimental flag (since 2017+!), see discussion, where both of the following flags have to be enabled:
- enable-experimental-web-platform-features
- enable-web-bluetooth-new-permissions-backend
Then one just has to poll the browser for any connected devices via navigator.bluetooth.getDevices()
and pass any found (and filtered) – into a new GoDice() object.
If interested, I could make a pull-request with the following snippets that got it working:
Added to godice.js
:
/**
* Attempt reconnect to a found and paired device.
*/
reconnectDevice(device){
this.GlobalDeviceId = device.id.toString();
this.bluetoothDevice = device;
var _self = this
this.bluetoothDevice.addEventListener('gattserverdisconnected', function() {
_self.onDiceDisconnected(_self.GlobalDeviceId, _self)
})
this.connectDeviceAndCacheCharacteristics();
}
I then call the following function within client browser on refresh:
function reconnectDice(){
return navigator.bluetooth.getDevices()
.then(async devices => {
for(let d of devices){
d.watchAdvertisements() // helps, but needs a cycle of tries (2x works)
if(d.name.includes('GoDice')){
const newDice = new GoDice();
newDice.reconnectDevice(d)
}
}
})
}
Er it was working fine – then now while testing, I did get the following error at some point, NetworkError: Bluetooth Device is no longer in range.
. Perhaps that's from a timeout or ______.. Curious how one should catch that and let the person know they need to manually re-connect them again. Er, just found this discussion about the exact problem. Still need to test running a watchAdvertisements()
on found devices.
Does this functionality already exist and I oversaw it – or would this be useful? As suggested or in another way?