@stoprocent/noble 1.17.3 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ const Signaling = require('./signaling');
9
9
 
10
10
  const NobleBindings = function (options) {
11
11
  this._state = null;
12
+ this._isScanning = false;
12
13
 
13
14
  this._addresses = {};
14
15
  this._addresseTypes = {};
@@ -53,21 +54,33 @@ NobleBindings.prototype.stopScanning = function () {
53
54
 
54
55
  NobleBindings.prototype.connect = function (peripheralUuid, parameters = {}) {
55
56
  let address = this._addresses[peripheralUuid];
56
- let addressType = this._addresseTypes[peripheralUuid] || 'random'; // Default to 'random' if type is not defined
57
+ let addressType = this._addresseTypes[peripheralUuid] || 'random';
57
58
 
58
- // If address is not available, generate it from the UUID using the transformation logic inline
59
59
  if (!address) {
60
- address = peripheralUuid.match(/.{1,2}/g).join(':'); // Converts UUID back to MAC address format
61
- addressType = typeof(parameters) === 'object' && parameters.addressType ? parameters.addressType : 'random';
60
+ address = peripheralUuid.match(/.{1,2}/g).join(':');
61
+ addressType = parameters && parameters.addressType ? parameters.addressType : 'random';
62
62
  }
63
63
 
64
- // Manage connection attempts
65
- if (!this._pendingConnectionUuid) {
66
- this._pendingConnectionUuid = peripheralUuid;
67
- this._hci.createLeConn(address, addressType, parameters);
64
+ // Add connection request to queue
65
+ this._connectionQueue.push({
66
+ id: peripheralUuid,
67
+ address,
68
+ addressType,
69
+ params: parameters
70
+ });
71
+
72
+ const processNextConnection = () => {
73
+ if (this._connectionQueue.length === 0) return;
74
+
75
+ const nextConn = this._connectionQueue[0]; // Look at next connection but don't remove yet
76
+ this._hci.createLeConn(nextConn.address, nextConn.addressType, nextConn.params);
77
+ };
78
+
79
+ if (this._isScanning) {
80
+ this.once('scanStop', processNextConnection);
81
+ this.stopScanning();
68
82
  } else {
69
- // If there is already a pending connection, queue this one
70
- this._connectionQueue.push({ id: peripheralUuid, params: parameters });
83
+ processNextConnection();
71
84
  }
72
85
  };
73
86
 
@@ -121,6 +134,21 @@ NobleBindings.prototype.onSigInt = function () {
121
134
  const sigIntListeners = process.listeners('SIGINT');
122
135
 
123
136
  if (sigIntListeners[sigIntListeners.length - 1] === this.onSigIntBinded) {
137
+ // Stop scanning if active
138
+ if (this._isScanning) {
139
+ this.stopScanning();
140
+ }
141
+
142
+ // Disconnect all active connections
143
+ for (const handle in this._handles) {
144
+ if (this._handles.hasOwnProperty(handle)) {
145
+ this.disconnect(this._handles[handle]);
146
+ }
147
+ }
148
+
149
+ // Clear connection queue
150
+ this._connectionQueue = [];
151
+
124
152
  // we are the last listener, so exit
125
153
  // this will trigger onExit, and clean up
126
154
  process.exit(1);
@@ -158,7 +186,7 @@ NobleBindings.prototype.onStateChange = function (state) {
158
186
  );
159
187
  console.log(' Try to run with environment variable:');
160
188
  console.log(' [sudo] NOBLE_HCI_DEVICE_ID=x node ...');
161
- }
189
+ }
162
190
 
163
191
  this.emit('stateChange', state);
164
192
  };
@@ -172,10 +200,12 @@ NobleBindings.prototype.onScanParametersSet = function () {
172
200
  };
173
201
 
174
202
  NobleBindings.prototype.onScanStart = function (filterDuplicates) {
203
+ this._isScanning = true;
175
204
  this.emit('scanStart', filterDuplicates);
176
205
  };
177
206
 
178
207
  NobleBindings.prototype.onScanStop = function () {
208
+ this._isScanning = false;
179
209
  this.emit('scanStop');
180
210
  };
181
211
 
@@ -250,8 +280,8 @@ NobleBindings.prototype.onLeConnComplete = function (
250
280
  // not master, ignore
251
281
  return;
252
282
  }
253
- let uuid = null;
254
283
 
284
+ let uuid = null;
255
285
  let error = null;
256
286
 
257
287
  if (status === 0) {
@@ -342,27 +372,23 @@ NobleBindings.prototype.onLeConnComplete = function (
342
372
 
343
373
  this._gatts[handle].exchangeMtu();
344
374
  } else {
345
- uuid = this._pendingConnectionUuid;
375
+ const currentConn = this._connectionQueue[0];
376
+ uuid = currentConn ? currentConn.id : null;
346
377
  let statusMessage = Hci.STATUS_MAPPER[status] || 'HCI Error: Unknown';
347
378
  const errorCode = ` (0x${status.toString(16)})`;
348
379
  statusMessage = statusMessage + errorCode;
349
380
  error = new Error(statusMessage);
350
381
  }
351
382
 
352
- this.emit('connect', uuid, error);
353
-
354
- if (this._connectionQueue.length > 0) {
355
- const queueItem = this._connectionQueue.shift();
356
- const peripheralUuid = queueItem.id;
383
+ // Remove the completed/failed connection attempt from queue
384
+ this._connectionQueue.shift();
357
385
 
358
- address = this._addresses[peripheralUuid];
359
- addressType = this._addresseTypes[peripheralUuid];
360
-
361
- this._pendingConnectionUuid = peripheralUuid;
386
+ this.emit('connect', uuid, error);
362
387
 
363
- this._hci.createLeConn(address, addressType, queueItem.params);
364
- } else {
365
- this._pendingConnectionUuid = null;
388
+ // Process next connection in queue if any
389
+ if (this._connectionQueue.length > 0 && !this._isScanning) {
390
+ const nextConn = this._connectionQueue[0];
391
+ this._hci.createLeConn(nextConn.address, nextConn.addressType, nextConn.params);
366
392
  }
367
393
  };
368
394
 
@@ -276,13 +276,7 @@ Gap.prototype.parseServices = function (
276
276
  serviceUuids: [],
277
277
  solicitationServiceUuids: []
278
278
  };
279
-
280
- if (leMetaEventType !== LE_META_EVENT_TYPE_SCAN_RESPONSE) {
281
- // reset service data every non-scan response event
282
- advertisement.serviceUuids = [];
283
- advertisement.serviceSolicitationUuids = [];
284
- }
285
-
279
+
286
280
  while (i + 1 < eir.length) {
287
281
  const length = eir.readUInt8(i);
288
282
 
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "license": "MIT",
7
7
  "name": "@stoprocent/noble",
8
8
  "description": "A Node.js BLE (Bluetooth Low Energy) central library.",
9
- "version": "1.17.3",
9
+ "version": "1.18.0",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"