@@ -31,18 +31,19 @@ export class BluetoothManager implements IBluetoothManager {
3131 ) : Promise < BluetoothManager . Device | undefined > {
3232 try {
3333 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- }
34+ if ( ! native ) {
35+ console . warn ( 'No device selected' ) ;
36+ return undefined ;
4337 }
44- }
45- catch ( error ) {
38+ const device = await registryItem . factory ( native ) ;
39+ if ( device && device . isConnected ) {
40+ this . _addDeviceToList ( device ) ;
41+ device . disconnected . connect ( ( ) => {
42+ this . _removeDeviceFromList ( device ) ;
43+ } ) ;
44+ return device ;
45+ }
46+ } catch ( error ) {
4647 console . error ( 'Connection failed:' , error ) ;
4748 throw error ;
4849 }
@@ -78,12 +79,11 @@ export class BluetoothManager implements IBluetoothManager {
7879 device . dispose ( ) ;
7980 }
8081
81- removeAllDevices ( ) {
82- const devicesToRemove = [ ...this . _deviceList ] ;
83- for ( const device of devicesToRemove ) {
82+ removeAllDevices ( deviceList : Array < BluetoothManager . Device > ) {
83+ for ( const device of deviceList ) {
8484 this . _removeDeviceFromList ( device ) ;
8585 }
86- this . deviceListChanged . emit ( this . _deviceList ) ;
86+ this . deviceListChanged . emit ( deviceList ) ;
8787 }
8888
8989 async checkWebBluetoothSupport ( ) : Promise < boolean > {
@@ -191,10 +191,11 @@ export namespace BluetoothManager {
191191 }
192192
193193 async disconnect ( ) : Promise < void > {
194- if ( this . native ) {
194+ if ( this . native ?. gatt ) {
195195 try {
196- this . native . gatt ?. disconnect ( ) ;
197-
196+ if ( this . native . gatt . connected ) {
197+ this . native . gatt . disconnect ( ) ;
198+ }
198199 } catch ( error ) {
199200 console . error ( 'Failed to disconnect:' , error ) ;
200201 }
@@ -276,8 +277,10 @@ export namespace BluetoothManager {
276277 * Interface for the bluetooth manager.
277278 */
278279export interface IBluetoothManager {
279- removeAllDevices ( ) : void ;
280- connect ( registryItem : IDeviceTypeRegistryItem ) : any ;
280+ removeAllDevices ( deviceList : Array < BluetoothManager . Device > ) : void ;
281+ connect (
282+ registryItem : IDeviceTypeRegistryItem
283+ ) : Promise < BluetoothManager . Device | undefined > ;
281284 disconnect ( device : BluetoothManager . Device ) : void ;
282285 deviceListChanged : Signal < BluetoothManager , Array < BluetoothManager . Device > > ;
283286 get deviceList ( ) : Array < BluetoothManager . Device > ;
0 commit comments