Skip to content

Commit

Permalink
fix(hci): wait for stop scanning before connecting
Browse files Browse the repository at this point in the history
due to the issues on the older dongles when connecting to peripheral stop scan is called in HCI binding
  • Loading branch information
stoprocent committed Dec 30, 2024
1 parent 7e67413 commit fde991e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
31 changes: 20 additions & 11 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Signaling = require('./signaling');

const NobleBindings = function (options) {
this._state = null;
this._isScanning = false;

this._addresses = {};
this._addresseTypes = {};
Expand Down Expand Up @@ -53,21 +54,27 @@ NobleBindings.prototype.stopScanning = function () {

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
let addressType = this._addresseTypes[peripheralUuid] || 'random';

// 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 = typeof(parameters) === 'object' && parameters.addressType ? parameters.addressType : 'random';
address = peripheralUuid.match(/.{1,2}/g).join(':');
addressType = parameters && parameters.addressType ? parameters.addressType : 'random';
}

// Manage connection attempts
if (!this._pendingConnectionUuid) {
this._pendingConnectionUuid = peripheralUuid;
this._hci.createLeConn(address, addressType, parameters);
const createConnection = () => {
if (!this._pendingConnectionUuid) {
this._pendingConnectionUuid = peripheralUuid;
this._hci.createLeConn(address, addressType, parameters);
} else {
this._connectionQueue.push({ id: peripheralUuid, params: parameters });
}
};

if (this._isScanning) {
this.once('scanStop', createConnection);
this.stopScanning();
} else {
// If there is already a pending connection, queue this one
this._connectionQueue.push({ id: peripheralUuid, params: parameters });
createConnection();
}
};

Expand Down Expand Up @@ -158,7 +165,7 @@ NobleBindings.prototype.onStateChange = function (state) {
);
console.log(' Try to run with environment variable:');
console.log(' [sudo] NOBLE_HCI_DEVICE_ID=x node ...');
}
}

this.emit('stateChange', state);
};
Expand All @@ -172,10 +179,12 @@ NobleBindings.prototype.onScanParametersSet = function () {
};

NobleBindings.prototype.onScanStart = function (filterDuplicates) {
this._isScanning = true;
this.emit('scanStart', filterDuplicates);
};

NobleBindings.prototype.onScanStop = function () {
this._isScanning = false;
this.emit('scanStop');
};

Expand Down
8 changes: 1 addition & 7 deletions lib/hci-socket/gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,7 @@ Gap.prototype.parseServices = function (
serviceUuids: [],
solicitationServiceUuids: []
};

if (leMetaEventType !== LE_META_EVENT_TYPE_SCAN_RESPONSE) {
// reset service data every non-scan response event
advertisement.serviceUuids = [];
advertisement.serviceSolicitationUuids = [];
}


while (i + 1 < eir.length) {
const length = eir.readUInt8(i);

Expand Down

0 comments on commit fde991e

Please sign in to comment.