Skip to content

Commit

Permalink
fix: attempt to use extended w custom noble binding
Browse files Browse the repository at this point in the history
  • Loading branch information
datagutt committed Dec 20, 2024
1 parent dea20e8 commit 3135462
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/connect-to-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
7 changes: 7 additions & 0 deletions src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 5 additions & 5 deletions src/device.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/scanner.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,17 +27,15 @@ export class DjiDiscoveredDevice {
export class DjiDeviceScanner extends EventEmitter {
static shared = new DjiDeviceScanner();
discoveredDevices: DjiDiscoveredDevice[] = [];
private noble?: typeof Noble;
private noble?: typeof noble;

constructor() {
super();
}

async startScanningForDevices(): Promise<void> {
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));
}
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 3135462

Please sign in to comment.