Skip to content

Commit b76659d

Browse files
Improve BluetoothManager class with copilot comments.
1 parent bde9ff1 commit b76659d

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

src/bluetooth/BluetoothManager.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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,28 +77,24 @@ export class BluetoothManager implements IBluetoothManager {
7277
device.dispose();
7378
}
7479

75-
removeAllDevices() {
76-
this._deviceList.forEach(device => {
77-
const index = this._deviceList.indexOf(device);
78-
if (index > -1) {
79-
this._deviceList.splice(index, 1);
80-
this._identifierRegistry.splice(index, 1);
81-
}
80+
removeAllDevices(deviceList: Array<BluetoothManager.Device>) {
81+
const devicesToRemove = [...deviceList];
82+
for (const device of devicesToRemove) {
8283
this._removeDeviceFromList(device);
83-
});
84-
this.deviceListChanged.emit(this._deviceList);
84+
}
85+
this.deviceListChanged.emit(deviceList);
8586
}
8687

8788
async checkWebBluetoothSupport(): Promise<boolean> {
88-
const isWebBluetoothSupported: boolean = navigator.bluetooth ? true : false;
89-
if (isWebBluetoothSupported === false) {
89+
if (!('bluetooth' in navigator)) {
9090
showDialog({
9191
title: 'Error',
92-
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/.',
9393
buttons: [Dialog.okButton({ label: 'Close' })]
9494
});
95+
return false;
9596
}
96-
return isWebBluetoothSupported;
97+
return true;
9798
}
9899

99100
async requestDevice(
@@ -190,7 +191,11 @@ export namespace BluetoothManager {
190191

191192
async disconnect(): Promise<void> {
192193
if (this.native) {
193-
this.native.gatt?.disconnect();
194+
try {
195+
this.native.gatt?.disconnect();
196+
} catch (error) {
197+
console.error('Failed to disconnect:', error);
198+
}
194199
this.isConnected = false;
195200
}
196201
}
@@ -269,7 +274,7 @@ export namespace BluetoothManager {
269274
* Interface for the bluetooth manager.
270275
*/
271276
export interface IBluetoothManager {
272-
removeAllDevices(Devices: Array<BluetoothManager.Device>): void;
277+
removeAllDevices(deviceList: Array<BluetoothManager.Device>): void;
273278
connect(registryItem: IDeviceTypeRegistryItem): any;
274279
disconnect(device: BluetoothManager.Device): void;
275280
deviceListChanged: Signal<BluetoothManager, Array<BluetoothManager.Device>>;

0 commit comments

Comments
 (0)