From 31354623131b9e585d322d42cf0f654023553b4b Mon Sep 17 00:00:00 2001 From: Thomas Lekanger Date: Fri, 20 Dec 2024 02:02:06 +0100 Subject: [PATCH] fix: attempt to use extended w custom noble binding --- examples/connect-to-device.js | 2 +- src/custom.d.ts | 7 +++++++ src/device.ts | 10 +++++----- src/scanner.ts | 12 +++++++----- 4 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 src/custom.d.ts diff --git a/examples/connect-to-device.js b/examples/connect-to-device.js index 257f4d0..c15f06d 100644 --- a/examples/connect-to-device.js +++ b/examples/connect-to-device.js @@ -16,7 +16,7 @@ if (process.argv.length < 6) { } const deviceId = process.argv[2]; -const deviceModel = process.argv[3]; +const deviceModel = DjiDeviceModel.osmoAction4; // process.argv[3]; const wiFiName = process.argv[4]; const wiFiPassword = process.argv[5]; const rtmpUrl = process.argv[6]; diff --git a/src/custom.d.ts b/src/custom.d.ts new file mode 100644 index 0000000..d6ab93f --- /dev/null +++ b/src/custom.d.ts @@ -0,0 +1,7 @@ +// custom.d.ts or in your TypeScript file +declare module '@stoprocent/noble/with-custom-binding' { + import * as noble from '@stoprocent/noble'; + + // Optionally, extend or modify types here + export default noble; +} diff --git a/src/device.ts b/src/device.ts index ea1f422..1057462 100644 --- a/src/device.ts +++ b/src/device.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ -import noble, { Peripheral, Characteristic, Service } from '@stoprocent/noble'; - +import { Peripheral, Characteristic, Service } from '@stoprocent/noble'; +import noble from '@stoprocent/noble/with-custom-binding.js'; import { DjiDeviceModel, DjiDeviceModelName, @@ -111,9 +111,7 @@ export class DjiDevice { this.reset(); this.startStartStreamingTimer(); this.setState(DjiDeviceState.discovering); - this.noble = await import('@stoprocent/noble').then( - (module) => module.default, - ); + this.noble = noble({ extended: true }); this.noble.on('stateChange', this.onStateChange.bind(this)); this.noble.on('discover', this.onDiscover.bind(this)); } @@ -206,6 +204,8 @@ export class DjiDevice { private onStateChange(state: string): void { if (state === 'poweredOn') { + console.log('Powered on'); + this.noble.reset(); this.noble?.startScanningAsync([], false); } } diff --git a/src/scanner.ts b/src/scanner.ts index 23c064c..c45632e 100644 --- a/src/scanner.ts +++ b/src/scanner.ts @@ -1,4 +1,6 @@ -import Noble, { Peripheral } from '@stoprocent/noble'; +import { Peripheral } from '@stoprocent/noble'; +import noble from '@stoprocent/noble/with-custom-binding.js'; + import { EventEmitter } from 'events'; import { djiModelFromManufacturerData, @@ -25,7 +27,7 @@ export class DjiDiscoveredDevice { export class DjiDeviceScanner extends EventEmitter { static shared = new DjiDeviceScanner(); discoveredDevices: DjiDiscoveredDevice[] = []; - private noble?: typeof Noble; + private noble?: typeof noble; constructor() { super(); @@ -33,9 +35,7 @@ export class DjiDeviceScanner extends EventEmitter { async startScanningForDevices(): Promise { this.discoveredDevices = []; - this.noble = await import('@stoprocent/noble').then( - (module) => module.default, - ); + this.noble = noble({ extended: true }); this.noble.on('stateChange', this.onStateChange.bind(this)); this.noble.on('discover', this.onDiscover.bind(this)); } @@ -51,6 +51,8 @@ export class DjiDeviceScanner extends EventEmitter { private onStateChange(state: string): void { if (state === 'poweredOn') { + console.log('Powered on'); + this.noble.reset(); this.noble?.startScanningAsync([], false); } }