Skip to content

Commit

Permalink
make bluetooth consistent with com
Browse files Browse the repository at this point in the history
  • Loading branch information
JovannMC committed Jul 25, 2024
1 parent 63c3a45 commit 325bdde
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,19 +785,27 @@ export default class HaritoraX extends EventEmitter {
const devices = await com.getAvailableDevices();
log(`Got COM devices: ${devices}`);
// for each device, add the device name to the available devices
devices.forEach((device) => {
devices.forEach((device: string) => {
if (device === "HaritoraX 1.0" || device === "HaritoraX 1.1" || device === "HaritoraX 1.1b") {
availableDevices.push("HaritoraX Wired");
} else {
availableDevices.push(device);
}
});
}
log("Checking if Bluetooth & HaritoraX Wireless are available")
log("Checking if any Bluetooth devices is available")
if (await bluetooth.isDeviceAvailable()) {
log("Bluetooth & HaritoraX Wireless devices available")
log("Bluetooth available")
availableDevices.push("Bluetooth");
availableDevices.push("HaritoraX Wireless");

const devices = await bluetooth.getAvailableDevices();
log(`Got Bluetooth devices: ${devices}`);

if (devices) {
devices.forEach((device: string) => {
availableDevices.push(device);
});
}
}

com.removeAllListeners();
Expand Down
36 changes: 32 additions & 4 deletions src/mode/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,37 @@ export default class Bluetooth extends EventEmitter {

async isDeviceAvailable() {
return new Promise<Boolean>((resolve) => {
if (noble._state === "poweredOn") {
resolve(true);
} else {
const timeout = setTimeout(() => {
resolve(false);
}, 3000);

noble.on("stateChange", (state) => {
clearTimeout(timeout);
if (state === "poweredOn") {
resolve(true);
} else {
resolve(false);
}
});
}
});
}

async getAvailableDevices() {
return new Promise<string[]>((resolve) => {
let found = false;
let availableDevices = [];

noble.on("discover", (peripheral) => {
if (peripheral.advertisement.localName && peripheral.advertisement.localName.startsWith("HaritoraXW-")) {
availableDevices.push("HaritoraX Wireless");
found = true;
noble.removeAllListeners();
noble.stopScanning();
resolve(true);
resolve(availableDevices);
}
});

Expand All @@ -79,17 +103,21 @@ export default class Bluetooth extends EventEmitter {
setTimeout(() => {
if (!found) {
noble.stopScanning();
resolve(false);
noble.removeAllListeners();
resolve(null);
}
}, 3000);
} else if (noble._state !== "poweredOn") {
resolve(false);
noble.removeAllListeners();
resolve(null);
}
});

// Fail-safe if noble never initializes properly (no "stateChange" event fired)
setTimeout(() => {
resolve(false);
noble.stopScanning();
noble.removeAllListeners();
resolve(null);
}, 3500);
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/mode/com.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class COM extends EventEmitter {

if (gxDevicesFound) availableDeviceNames.add("HaritoraX Wireless");


return Array.from(availableDeviceNames);
}

Expand Down Expand Up @@ -144,6 +145,7 @@ export default class COM extends EventEmitter {
if (!serialPort.isOpen) return;
try {
log(`Closing COM port: ${port}`);
serialPort.removeAllListeners();
serialPort.close();
delete activePorts[port];
} catch (err) {
Expand Down

0 comments on commit 325bdde

Please sign in to comment.