@stoprocent/noble 1.17.4 → 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.
@@ -61,20 +61,26 @@ NobleBindings.prototype.connect = function (peripheralUuid, parameters = {}) {
61
61
  addressType = parameters && parameters.addressType ? parameters.addressType : 'random';
62
62
  }
63
63
 
64
- const createConnection = () => {
65
- if (!this._pendingConnectionUuid) {
66
- this._pendingConnectionUuid = peripheralUuid;
67
- this._hci.createLeConn(address, addressType, parameters);
68
- } else {
69
- this._connectionQueue.push({ id: peripheralUuid, params: parameters });
70
- }
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);
71
77
  };
72
78
 
73
79
  if (this._isScanning) {
74
- this.once('scanStop', createConnection);
80
+ this.once('scanStop', processNextConnection);
75
81
  this.stopScanning();
76
82
  } else {
77
- createConnection();
83
+ processNextConnection();
78
84
  }
79
85
  };
80
86
 
@@ -128,6 +134,21 @@ NobleBindings.prototype.onSigInt = function () {
128
134
  const sigIntListeners = process.listeners('SIGINT');
129
135
 
130
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
+
131
152
  // we are the last listener, so exit
132
153
  // this will trigger onExit, and clean up
133
154
  process.exit(1);
@@ -259,8 +280,8 @@ NobleBindings.prototype.onLeConnComplete = function (
259
280
  // not master, ignore
260
281
  return;
261
282
  }
262
- let uuid = null;
263
283
 
284
+ let uuid = null;
264
285
  let error = null;
265
286
 
266
287
  if (status === 0) {
@@ -351,27 +372,23 @@ NobleBindings.prototype.onLeConnComplete = function (
351
372
 
352
373
  this._gatts[handle].exchangeMtu();
353
374
  } else {
354
- uuid = this._pendingConnectionUuid;
375
+ const currentConn = this._connectionQueue[0];
376
+ uuid = currentConn ? currentConn.id : null;
355
377
  let statusMessage = Hci.STATUS_MAPPER[status] || 'HCI Error: Unknown';
356
378
  const errorCode = ` (0x${status.toString(16)})`;
357
379
  statusMessage = statusMessage + errorCode;
358
380
  error = new Error(statusMessage);
359
381
  }
360
382
 
361
- this.emit('connect', uuid, error);
362
-
363
- if (this._connectionQueue.length > 0) {
364
- const queueItem = this._connectionQueue.shift();
365
- const peripheralUuid = queueItem.id;
383
+ // Remove the completed/failed connection attempt from queue
384
+ this._connectionQueue.shift();
366
385
 
367
- address = this._addresses[peripheralUuid];
368
- addressType = this._addresseTypes[peripheralUuid];
369
-
370
- this._pendingConnectionUuid = peripheralUuid;
386
+ this.emit('connect', uuid, error);
371
387
 
372
- this._hci.createLeConn(address, addressType, queueItem.params);
373
- } else {
374
- 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);
375
392
  }
376
393
  };
377
394
 
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.4",
9
+ "version": "1.18.0",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"