|
3 | 3 |
|
4 | 4 | <!-- |
5 | 5 | Author: sepp89117 |
6 | | - Version: 1.2 |
7 | | - Date: 2022-12-13 |
| 6 | + Version: 1.3 |
| 7 | + Date: 2023-05-28 |
8 | 8 | --> |
9 | 9 |
|
10 | 10 | <head> |
|
270 | 270 | </tbody> |
271 | 271 | </table> |
272 | 272 | </fieldset> |
273 | | - <fieldset class="foldable"> |
| 273 | + <fieldset id="errorLog" class="foldable"> |
274 | 274 | <legend style="text-align: left;" onclick="this.parentNode.classList.toggle('expanded');"><b>Error Log</b> |
275 | 275 | </legend> |
276 | 276 | <table id="error_log_table" class="styled-table"> |
|
467 | 467 | * Pairing / Connecting |
468 | 468 | */ |
469 | 469 | function onClickPair() { |
| 470 | + if (connectingDev != null) { |
| 471 | + showInfo("A connect process with '" + connectingDev.name + "' is already in progress!", 3000); |
| 472 | + return; |
| 473 | + } |
470 | 474 | let filters = []; |
471 | 475 | let options = {}; |
472 | 476 |
|
|
479 | 483 |
|
480 | 484 | navigator.bluetooth.requestDevice(options) |
481 | 485 | .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 | + } |
482 | 492 | console.log("Connecting to '" + device.name + "'."); |
483 | 493 | showInfo("Connecting to '" + device.name + "'. Make sure the camera is powered on and wait...", 0); |
484 | 494 |
|
485 | 495 | // Check if device is always conencted |
486 | 496 | for (var i = 0; i < connectedDevs.length; i++) { |
487 | 497 | 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); |
489 | 499 | return; // device is always connected |
490 | 500 | } |
491 | 501 | } |
|
495 | 505 | window.setTimeout(function () { |
496 | 506 | if (connectingDev != null) { |
497 | 507 | showInfo("Connecting '" + connectingDev.name + "' timeout! Please retry!", 5000); |
498 | | - if (connectingDev != null) |
499 | | - connectingDev.gatt.disconnect(); |
500 | | - connectingDev = null; |
| 508 | + resetConnectingDev(); |
501 | 509 | } |
502 | 510 | }, 60000); |
503 | 511 | return device.gatt.connect(); |
504 | 512 | }) |
505 | 513 | .then(server => { |
506 | 514 | if (server == null) { |
507 | | - logError("[gatt.connect] server is 'null'"); |
| 515 | + logError("[gatt.connect] Server is 'null'"); |
508 | 516 | return; |
509 | 517 | } |
510 | 518 | connectingDev = null; |
|
533 | 541 | }) |
534 | 542 | .then(service => { |
535 | 543 | if (service == null) { |
536 | | - logError("[getPrimaryServices] service is 'null'"); |
| 544 | + logError("[getPrimaryServices] Service is 'null'"); |
537 | 545 | return; |
538 | 546 | } |
539 | 547 |
|
|
573 | 581 | } |
574 | 582 | }) |
575 | 583 | .catch(error => { |
576 | | - if (connectingDev != null) |
577 | | - connectingDev.gatt.disconnect(); |
578 | | - connectingDev = null; |
579 | | - console.log(new Date().toLocaleString() + ' [onClickPair] ERROR! ' + error); |
580 | 584 | showInfo("[onClickPair] " + error, 0); |
581 | 585 | logError("[onClickPair] " + error); |
| 586 | + resetConnectingDev(); |
582 | 587 | }); |
583 | 588 | } |
584 | 589 |
|
|
590 | 595 | .then(characteristic => { |
591 | 596 | bufferExecNext(); |
592 | 597 | if (characteristic == null) { |
593 | | - logError("[getCharacteristic] characteristic is 'null'"); |
| 598 | + logError("[getCharacteristic] Characteristic is 'null'"); |
594 | 599 | return; |
595 | 600 | } |
596 | 601 | console.log('Got Characteristic'); |
|
606 | 611 | return; |
607 | 612 | case commandRespUUID: |
608 | 613 | 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); |
614 | 615 | return; |
615 | 616 | case settingsUUID: |
616 | 617 | dev.settingsCharacteristic = characteristic; |
617 | 618 | return; |
618 | 619 | case settingsRespUUID: |
619 | 620 | 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); |
626 | 622 | return; |
627 | 623 | case queryUUID: |
628 | 624 | dev.queryCharacteristic = characteristic; |
629 | 625 | return; |
630 | 626 | case queryRespUUID: |
631 | 627 | 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); |
638 | 629 | return; |
639 | 630 | case modelNoUUID: |
640 | 631 | dev.modelNoCharacteristic = characteristic; |
|
654 | 645 | logError("[getCharacteristic] " + error); |
655 | 646 | if (connectingDev != null) { |
656 | 647 | showInfo("Connecting '" + connectingDev.name + "' error: '" + error + "'. Please retry!", 5000); |
657 | | - if (connectingDev != null) |
658 | | - connectingDev.gatt.disconnect(); |
659 | | - connectingDev = null; |
| 648 | + resetConnectingDev(); |
660 | 649 | } |
661 | 650 | }); |
662 | 651 | } |
663 | 652 |
|
| 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 | + |
664 | 697 | /* |
665 | 698 | * Non-blocking asynchronous execution of the BLE communication functions |
666 | 699 | */ |
|
691 | 724 | var characteristic = args[0]; |
692 | 725 |
|
693 | 726 | if (characteristic == null) { |
694 | | - logError("[readValue] characteristic is 'null'"); |
| 727 | + logError("[readValue] Characteristic is 'null'"); |
695 | 728 | bufferExecNext(); |
696 | 729 | return; |
697 | 730 | } |
|
921 | 954 | for (var i = 0; i < connectedDevs.length; i++) { |
922 | 955 | if (connectedDevs[i].server.device.id == sender_id) { |
923 | 956 | showInfo("Cam '" + connectedDevs[i].server.device.name + "' disconnected.", 5000); |
| 957 | + connectedDevs[i].server.device.removeEventListener('gattserverdisconnected', onDisconnected); |
924 | 958 | connectedDevs.splice(i, 1); |
925 | 959 | updateList(); |
926 | 960 | break; |
|
0 commit comments