@@ -29,16 +29,21 @@ export class BluetoothManager implements IBluetoothManager {
2929 async connect (
3030 registryItem : IDeviceTypeRegistryItem
3131 ) : Promise < BluetoothManager . Device | undefined > {
32- const native = await this . requestDevice ( registryItem ) ;
33- if ( native ) {
34- const device = await registryItem . factory ( native ) ;
35- if ( device && device . isConnected ) {
36- this . _addDeviceToList ( device ) ;
37- device . disconnected . connect ( async ( ) => {
38- this . _removeDeviceFromList ( device ) ;
39- } ) ;
40- return device ;
32+ try {
33+ const native = await this . requestDevice ( registryItem ) ;
34+ if ( native ) {
35+ const device = await registryItem . factory ( native ) ;
36+ if ( device && device . isConnected ) {
37+ this . _addDeviceToList ( device ) ;
38+ device . disconnected . connect ( async ( ) => {
39+ this . _removeDeviceFromList ( device ) ;
40+ } ) ;
41+ return device ;
42+ }
4143 }
44+ } catch ( error ) {
45+ console . error ( 'Connection failed:' , error ) ;
46+ throw error ;
4247 }
4348 }
4449
@@ -54,7 +59,7 @@ export class BluetoothManager implements IBluetoothManager {
5459 this . _deviceList . push ( device ) ;
5560 this . _identifierRegistry . push ( identifier ) ;
5661 } else {
57- console . warn ( 'The device is already in the registry of identifiers' ) ;
62+ console . log ( 'The device is already in the registry of identifiers' ) ;
5863 }
5964 // Emit the signal when the list changes
6065 this . deviceListChanged . emit ( this . _deviceList ) ;
@@ -72,23 +77,24 @@ export class BluetoothManager implements IBluetoothManager {
7277 device . dispose ( ) ;
7378 }
7479
75- removeAllDevices ( ) {
76- this . _deviceList . forEach ( ( device , index ) => {
80+ removeAllDevices ( deviceList : Array < BluetoothManager . Device > ) {
81+ const devicesToRemove = [ ...deviceList ] ;
82+ for ( const device of devicesToRemove ) {
7783 this . _removeDeviceFromList ( device ) ;
78- this . deviceListChanged . emit ( this . _deviceList ) ;
79- } ) ;
84+ }
85+ this . deviceListChanged . emit ( deviceList ) ;
8086 }
8187
8288 async checkWebBluetoothSupport ( ) : Promise < boolean > {
83- const isWebBluetoothSupported : boolean = navigator . bluetooth ? true : false ;
84- if ( isWebBluetoothSupported === false ) {
89+ if ( ! ( 'bluetooth' in navigator ) ) {
8590 showDialog ( {
8691 title : 'Error' ,
87- body : 'Web Bluetooth is not supported on your browser. It works on Chrome and Edge (Firefox and Explorer are not supported). \n Please also check that the Web Bluetooth flag is properly set to enabled in the Chrome flags ( chrome://flags/) .' ,
92+ body : 'Web Bluetooth is not supported in your browser. It works on Chrome and Edge. Make sure the Web Bluetooth flag is enabled in chrome://flags/.' ,
8893 buttons : [ Dialog . okButton ( { label : 'Close' } ) ]
8994 } ) ;
95+ return false ;
9096 }
91- return isWebBluetoothSupported ;
97+ return true ;
9298 }
9399
94100 async requestDevice (
@@ -185,7 +191,11 @@ export namespace BluetoothManager {
185191
186192 async disconnect ( ) : Promise < void > {
187193 if ( this . native ) {
188- this . native . gatt ?. disconnect ( ) ;
194+ try {
195+ this . native . gatt ?. disconnect ( ) ;
196+ } catch ( error ) {
197+ console . error ( 'Failed to disconnect:' , error ) ;
198+ }
189199 this . isConnected = false ;
190200 }
191201 }
@@ -264,7 +274,7 @@ export namespace BluetoothManager {
264274 * Interface for the bluetooth manager.
265275 */
266276export interface IBluetoothManager {
267- removeAllDevices ( Devices : Array < BluetoothManager . Device > ) : void ;
277+ removeAllDevices ( deviceList : Array < BluetoothManager . Device > ) : void ;
268278 connect ( registryItem : IDeviceTypeRegistryItem ) : any ;
269279 disconnect ( device : BluetoothManager . Device ) : void ;
270280 deviceListChanged : Signal < BluetoothManager , Array < BluetoothManager . Device > > ;
0 commit comments