Skip to content

Commit

Permalink
fix(hci): address type can be now provided in connect function
Browse files Browse the repository at this point in the history
  • Loading branch information
stoprocent committed Dec 29, 2024
1 parent f390337 commit 756390f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
/**
* @deprecated
Expand All @@ -26,8 +34,8 @@ export declare function startScanning(serviceUUIDs?: string[], allowDuplicates?:
export declare function startScanningAsync(serviceUUIDs?: string[], allowDuplicates?: boolean): Promise<void>;
export declare function stopScanning(callback?: () => void): void;
export declare function stopScanningAsync(): Promise<void>;
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<Peripheral>;
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<Peripheral>;
export declare function cancelConnect(peripheralUuid: string, options?: object): void;
export declare function reset(): void;
export declare function stop(): void;
Expand Down
4 changes: 2 additions & 2 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 756390f

Please sign in to comment.