From fde991e70de6357db7c9aaa651058d178740a38a Mon Sep 17 00:00:00 2001 From: Marek Serafin Date: Mon, 30 Dec 2024 19:06:20 +0100 Subject: [PATCH] fix(hci): wait for stop scanning before connecting due to the issues on the older dongles when connecting to peripheral stop scan is called in HCI binding --- lib/hci-socket/bindings.js | 31 ++++++++++++++++++++----------- lib/hci-socket/gap.js | 8 +------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/hci-socket/bindings.js b/lib/hci-socket/bindings.js index cd134641..c6de2a64 100644 --- a/lib/hci-socket/bindings.js +++ b/lib/hci-socket/bindings.js @@ -9,6 +9,7 @@ const Signaling = require('./signaling'); const NobleBindings = function (options) { this._state = null; + this._isScanning = false; this._addresses = {}; this._addresseTypes = {}; @@ -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(); } }; @@ -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); }; @@ -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'); }; diff --git a/lib/hci-socket/gap.js b/lib/hci-socket/gap.js index c2c03f2e..9518591b 100644 --- a/lib/hci-socket/gap.js +++ b/lib/hci-socket/gap.js @@ -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);