From c36b5be83828e6c70627a422f18e8ea787871073 Mon Sep 17 00:00:00 2001 From: JovannMC Date: Wed, 24 Jul 2024 15:58:17 +0300 Subject: [PATCH] Fix bluetooth isDeviceAvailable() --- src/HaritoraX.ts | 2 +- src/mode/bluetooth.ts | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/HaritoraX.ts b/src/HaritoraX.ts index 40961a1..b51f861 100644 --- a/src/HaritoraX.ts +++ b/src/HaritoraX.ts @@ -773,7 +773,7 @@ export default class HaritoraX extends EventEmitter { async getAvailableDevices(): Promise { let availableDevices: string[] = []; - let com = new COM("wireless"); + let com = new COM("wireless"); // variable doesn't matter, just need to initialize it to get the available devices let bluetooth = new Bluetooth(); if (await com.isDeviceAvailable()) { diff --git a/src/mode/bluetooth.ts b/src/mode/bluetooth.ts index da0394f..de92baa 100644 --- a/src/mode/bluetooth.ts +++ b/src/mode/bluetooth.ts @@ -73,18 +73,20 @@ export default class Bluetooth extends EventEmitter { } }); - if (noble._state === "poweredOn" && !found) { - noble.startScanning([], true); - - setTimeout(() => { - if (!found) { - noble.stopScanning(); - resolve(false); - } - }, 3000); - } else if (noble._state !== "poweredOn") { - resolve(false); - } + noble.on("stateChange", (state) => { + if (state === "poweredOn" && !found) { + noble.startScanning([], true); + + setTimeout(() => { + if (!found) { + noble.stopScanning(); + resolve(false); + } + }, 3000); + } else if (noble._state !== "poweredOn") { + resolve(false); + } + }); }); }