From 756390f825048a209fe7632258c3775bb249e82b Mon Sep 17 00:00:00 2001 From: Marek Serafin Date: Sun, 29 Dec 2024 13:24:23 +0100 Subject: [PATCH] fix(hci): address type can be now provided in connect function --- index.d.ts | 12 ++++++++++-- lib/hci-socket/bindings.js | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0ec9f800..7428a014 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,6 +13,14 @@ import events = require("events"); +export interface ConnectOptions { + addressType?: 'public' | 'random'; + minInterval?: number; + maxInterval?: number; + latency?: number; + timeout?: number; +} + export declare function waitForPoweredOn(timeout?: number): Promise; /** * @deprecated @@ -26,8 +34,8 @@ export declare function startScanning(serviceUUIDs?: string[], allowDuplicates?: export declare function startScanningAsync(serviceUUIDs?: string[], allowDuplicates?: boolean): Promise; export declare function stopScanning(callback?: () => void): void; export declare function stopScanningAsync(): Promise; -export declare function connect(peripheralUuid: string, options?: object, callback?: (error: Error | undefined, peripheral: Peripheral) => void): void; -export declare function connectAsync(peripheralUuid: string, options?: object): Promise; +export declare function connect(peripheralUuid: string, options?: ConnectOptions, callback?: (error: Error | undefined, peripheral: Peripheral) => void): void; +export declare function connectAsync(peripheralUuid: string, options?: ConnectOptions): Promise; export declare function cancelConnect(peripheralUuid: string, options?: object): void; export declare function reset(): void; export declare function stop(): void; diff --git a/lib/hci-socket/bindings.js b/lib/hci-socket/bindings.js index 582e6690..05467735 100644 --- a/lib/hci-socket/bindings.js +++ b/lib/hci-socket/bindings.js @@ -51,14 +51,14 @@ NobleBindings.prototype.stopScanning = function () { this._gap.stopScanning(); }; -NobleBindings.prototype.connect = function (peripheralUuid, parameters) { +NobleBindings.prototype.connect = function (peripheralUuid, parameters = {}) { let address = this._addresses[peripheralUuid]; let addressType = this._addresseTypes[peripheralUuid] || 'random'; // Default to 'random' if type is not defined // If address is not available, generate it from the UUID using the transformation logic inline if (!address) { address = peripheralUuid.match(/.{1,2}/g).join(':'); // Converts UUID back to MAC address format - addressType = (parseInt(address.slice(0, 2).slice(0,2), 16) & 0x02) === 0 ? 'public' : 'random'; + addressType = 'addressType' in parameters ? parameters.addressType : 'random'; } // Manage connection attempts