Skip to content

Commit 588d673

Browse files
authored
Update GoPro_Web_RC.html
Added unpaired connection detection with notice
1 parent a5bc0f0 commit 588d673

1 file changed

Lines changed: 69 additions & 35 deletions

File tree

GoPro_Web_RC.html

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
<!--
55
Author: sepp89117
6-
Version: 1.2
7-
Date: 2022-12-13
6+
Version: 1.3
7+
Date: 2023-05-28
88
-->
99

1010
<head>
@@ -270,7 +270,7 @@
270270
</tbody>
271271
</table>
272272
</fieldset>
273-
<fieldset class="foldable">
273+
<fieldset id="errorLog" class="foldable">
274274
<legend style="text-align: left;" onclick="this.parentNode.classList.toggle('expanded');"><b>Error Log</b>
275275
</legend>
276276
<table id="error_log_table" class="styled-table">
@@ -467,6 +467,10 @@
467467
* Pairing / Connecting
468468
*/
469469
function onClickPair() {
470+
if (connectingDev != null) {
471+
showInfo("A connect process with '" + connectingDev.name + "' is already in progress!", 3000);
472+
return;
473+
}
470474
let filters = [];
471475
let options = {};
472476

@@ -479,13 +483,19 @@
479483

480484
navigator.bluetooth.requestDevice(options)
481485
.then(device => {
486+
if (device.name === null) {
487+
showInfo("Device name is 'null'! Is the camera paired with your device via Bluetooth?", 6000);
488+
logError("[navigator.bluetooth.requestDevice] Device name is 'null'");
489+
resetConnectingDev();
490+
return;
491+
}
482492
console.log("Connecting to '" + device.name + "'.");
483493
showInfo("Connecting to '" + device.name + "'. Make sure the camera is powered on and wait...", 0);
484494

485495
// Check if device is always conencted
486496
for (var i = 0; i < connectedDevs.length; i++) {
487497
if (connectedDevs[i].server.device.id == device.id && connectedDevs[i].server.connected) {
488-
showInfo(device.name + "' is always connected!", 3000);
498+
showInfo("'" + device.name + "' is always connected!", 3000);
489499
return; // device is always connected
490500
}
491501
}
@@ -495,16 +505,14 @@
495505
window.setTimeout(function () {
496506
if (connectingDev != null) {
497507
showInfo("Connecting '" + connectingDev.name + "' timeout! Please retry!", 5000);
498-
if (connectingDev != null)
499-
connectingDev.gatt.disconnect();
500-
connectingDev = null;
508+
resetConnectingDev();
501509
}
502510
}, 60000);
503511
return device.gatt.connect();
504512
})
505513
.then(server => {
506514
if (server == null) {
507-
logError("[gatt.connect] server is 'null'");
515+
logError("[gatt.connect] Server is 'null'");
508516
return;
509517
}
510518
connectingDev = null;
@@ -533,7 +541,7 @@
533541
})
534542
.then(service => {
535543
if (service == null) {
536-
logError("[getPrimaryServices] service is 'null'");
544+
logError("[getPrimaryServices] Service is 'null'");
537545
return;
538546
}
539547

@@ -573,12 +581,9 @@
573581
}
574582
})
575583
.catch(error => {
576-
if (connectingDev != null)
577-
connectingDev.gatt.disconnect();
578-
connectingDev = null;
579-
console.log(new Date().toLocaleString() + ' [onClickPair] ERROR! ' + error);
580584
showInfo("[onClickPair] " + error, 0);
581585
logError("[onClickPair] " + error);
586+
resetConnectingDev();
582587
});
583588
}
584589

@@ -590,7 +595,7 @@
590595
.then(characteristic => {
591596
bufferExecNext();
592597
if (characteristic == null) {
593-
logError("[getCharacteristic] characteristic is 'null'");
598+
logError("[getCharacteristic] Characteristic is 'null'");
594599
return;
595600
}
596601
console.log('Got Characteristic');
@@ -606,35 +611,21 @@
606611
return;
607612
case commandRespUUID:
608613
dev.commandRespCharacteristic = characteristic;
609-
// Start Notification
610-
dev.commandRespCharacteristic.startNotifications().then(_ => {
611-
console.log('> Characteristic Notifications started');
612-
dev.commandRespCharacteristic.addEventListener('characteristicvaluechanged', handleRespNotifications);
613-
});
614+
startNotifications(dev.commandRespCharacteristic);
614615
return;
615616
case settingsUUID:
616617
dev.settingsCharacteristic = characteristic;
617618
return;
618619
case settingsRespUUID:
619620
dev.settingsRespCharacteristic = characteristic;
620-
621-
// Start Notification
622-
dev.settingsRespCharacteristic.startNotifications().then(_ => {
623-
console.log('> Characteristic Notifications started');
624-
dev.settingsRespCharacteristic.addEventListener('characteristicvaluechanged', handleRespNotifications);
625-
});
621+
startNotifications(dev.settingsRespCharacteristic);
626622
return;
627623
case queryUUID:
628624
dev.queryCharacteristic = characteristic;
629625
return;
630626
case queryRespUUID:
631627
dev.queryRespCharacteristic = characteristic;
632-
633-
// Start Notification
634-
dev.queryRespCharacteristic.startNotifications().then(_ => {
635-
console.log('> Characteristic Notifications started');
636-
dev.queryRespCharacteristic.addEventListener('characteristicvaluechanged', handleRespNotifications);
637-
});
628+
startNotifications(dev.queryRespCharacteristic);
638629
return;
639630
case modelNoUUID:
640631
dev.modelNoCharacteristic = characteristic;
@@ -654,13 +645,55 @@
654645
logError("[getCharacteristic] " + error);
655646
if (connectingDev != null) {
656647
showInfo("Connecting '" + connectingDev.name + "' error: '" + error + "'. Please retry!", 5000);
657-
if (connectingDev != null)
658-
connectingDev.gatt.disconnect();
659-
connectingDev = null;
648+
resetConnectingDev();
660649
}
661650
});
662651
}
663652

653+
function startNotifications(characteristic) {
654+
characteristic.startNotifications()
655+
.then(_ => {
656+
console.log('> Characteristic Notifications started');
657+
characteristic.addEventListener('characteristicvaluechanged', handleRespNotifications);
658+
})
659+
.catch(error => {
660+
if (error.message.includes("Authentication failed")) {
661+
removeConnectedDev(characteristic.service.device);
662+
resetConnectingDev();
663+
664+
showInfo("Authentication failed! Perform Bluetooth pairing first! See Error Log for details!");
665+
logError("[startNotifications] " + error);
666+
logError("Authentication failed!<br>Please first perform a Bluetooth pairing with the camera via your device Bluetooth manager!");
667+
document.getElementById("errorLog").classList.add('expanded');
668+
}
669+
});
670+
}
671+
672+
function removeConnectedDev(dev) {
673+
if (dev === null)
674+
return;
675+
676+
for (var i = 0; i < connectedDevs.length; i++) {
677+
if (connectedDevs[i].server.device.id == dev.gatt.device.id) {
678+
connectedDevs[i].server.device.removeEventListener('gattserverdisconnected', onDisconnected);
679+
connectedDevs[i].server.device.gatt.disconnect();
680+
connectedDevs.splice(i, 1);
681+
dev = null;
682+
updateList();
683+
break;
684+
}
685+
}
686+
}
687+
688+
function resetConnectingDev() {
689+
if (connectingDev === null)
690+
return;
691+
692+
connectingDev.gatt.disconnect();
693+
updateList();
694+
connectingDev = null;
695+
}
696+
664697
/*
665698
* Non-blocking asynchronous execution of the BLE communication functions
666699
*/
@@ -691,7 +724,7 @@
691724
var characteristic = args[0];
692725

693726
if (characteristic == null) {
694-
logError("[readValue] characteristic is 'null'");
727+
logError("[readValue] Characteristic is 'null'");
695728
bufferExecNext();
696729
return;
697730
}
@@ -921,6 +954,7 @@
921954
for (var i = 0; i < connectedDevs.length; i++) {
922955
if (connectedDevs[i].server.device.id == sender_id) {
923956
showInfo("Cam '" + connectedDevs[i].server.device.name + "' disconnected.", 5000);
957+
connectedDevs[i].server.device.removeEventListener('gattserverdisconnected', onDisconnected);
924958
connectedDevs.splice(i, 1);
925959
updateList();
926960
break;

0 commit comments

Comments
 (0)