diff --git a/README.md b/README.md index e2d44bd..0a0a6bd 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ For Issues please consider to directly provide debug loggins (see above). ### __WORK IN PROGRESS__ * (dnicolson/Apollon77) Change noble to @stoprocent fork +* (Apollon77) prevent crash when no BLE device isc connected ### 0.10.1 (2023-11-23) * (Apollon77) Remove duplicate entries in characteristic list diff --git a/src/transport/ble/ble-discovery.ts b/src/transport/ble/ble-discovery.ts index 6f9a17a..073dfea 100644 --- a/src/transport/ble/ble-discovery.ts +++ b/src/transport/ble/ble-discovery.ts @@ -7,12 +7,16 @@ import { Peripheral } from '@stoprocent/noble'; import GattClient from './gatt-client'; import Debug from 'debug'; -let noble: typeof import('@stoprocent/noble'); -noble = require('@stoprocent/noble'); -if (typeof noble.on !== 'function') { - // The following commit broke the default exported instance of noble: - // https://github.com/abandonware/noble/commit/b67eea246f719947fc45b1b52b856e61637a8a8e - noble = (noble as any)({ extended: false }); +let noble: typeof import('@stoprocent/noble') | null = null; +try { + noble = require('@stoprocent/noble'); + if (typeof noble?.on !== 'function') { + // The following commit broke the default exported instance of noble: + // https://github.com/abandonware/noble/commit/b67eea246f719947fc45b1b52b856e61637a8a8e + noble = (noble as any)({ extended: false }); + } +} catch (error) { + console.error('Cannot start noble. Make sure it is installed and BLE device is connected.'); } const debug = Debug('hap-controller:gatt-client'); @@ -168,6 +172,9 @@ export default class BLEDiscovery extends EventEmitter { * updated in the advertisement. */ start(allowDuplicates = false): void { + if (!noble) { + throw new Error('BLE not enabled'); + } this.scanEnabled = true; this.allowDuplicates = allowDuplicates; @@ -207,6 +214,9 @@ export default class BLEDiscovery extends EventEmitter { * Stop an ongoing discovery process. */ stop(): void { + if (!noble) { + throw new Error('BLE not enabled'); + } this.scanEnabled = false; noble.stopScanning(); noble.removeListener('stateChange', this.handleStateChange);