@stoprocent/noble 2.1.5 → 2.1.7

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.
@@ -104,10 +104,8 @@ NobleBindings.prototype.startScanning = function (
104
104
  };
105
105
 
106
106
  NobleBindings.prototype.stopScanning = function () {
107
- if (this._isScanning) {
108
- this._gap.stopScanning();
109
- }
110
- else {
107
+ this._gap.stopScanning();
108
+ if (!this._isScanning) {
111
109
  this.emit('scanStop');
112
110
  }
113
111
  };
@@ -132,7 +130,7 @@ NobleBindings.prototype.connect = function (peripheralUuid, parameters = {}) {
132
130
  const processNextConnection = () => {
133
131
  if (this._connectionQueue.length === 1) {
134
132
  const nextConn = this._connectionQueue[0]; // Look at next connection but don't remove yet
135
- this._hci.createLeConn(nextConn.address, nextConn.addressType, nextConn.params);
133
+ this._hci.createLeConn(nextConn.address, nextConn.addressType, nextConn.params, Object.keys(this._handles).length === 0);
136
134
  }
137
135
  };
138
136
 
@@ -215,7 +213,9 @@ NobleBindings.prototype.onScanParametersSet = function () {
215
213
  };
216
214
 
217
215
  NobleBindings.prototype.onScanStart = function (filterDuplicates) {
218
- this._isScanning = true;
216
+ if (this._isScanningStarted) {
217
+ this._isScanning = true;
218
+ }
219
219
  this.emit('scanStart', filterDuplicates);
220
220
  };
221
221
 
@@ -292,10 +292,6 @@ NobleBindings.prototype.onLeConnComplete = function (
292
292
  supervisionTimeout,
293
293
  masterClockAccuracy
294
294
  ) {
295
- //
296
- this._isScanning = false;
297
- this._isScanningStarted = false;
298
-
299
295
  if (role !== 0 && role !== undefined) {
300
296
  // not master, ignore
301
297
  return;
@@ -408,7 +404,7 @@ NobleBindings.prototype.onLeConnComplete = function (
408
404
  // Process next connection in queue if any
409
405
  if (this._connectionQueue.length > 0 && !this._isScanning) {
410
406
  const nextConn = this._connectionQueue[0];
411
- this._hci.createLeConn(nextConn.address, nextConn.addressType, nextConn.params);
407
+ this._hci.createLeConn(nextConn.address, nextConn.addressType, nextConn.params, Object.keys(this._handles).length === 0);
412
408
  }
413
409
  };
414
410
 
@@ -40,11 +40,6 @@ Gap.prototype.setScanParameters = function (interval, window) {
40
40
  };
41
41
 
42
42
  Gap.prototype.startScanning = function (allowDuplicates) {
43
- this._hci.once('reset', () => this.startScanningAfterReset(allowDuplicates));
44
- this._hci.reset();
45
- };
46
-
47
- Gap.prototype.startScanningAfterReset = function (allowDuplicates) {
48
43
  this._scanState = 'starting';
49
44
  this._scanFilterDuplicates = !allowDuplicates;
50
45
 
@@ -508,9 +508,19 @@ Hci.prototype.setScanEnabled = function (enabled, filterDuplicates) {
508
508
  this._socket.write(cmd);
509
509
  };
510
510
 
511
- Hci.prototype.createLeConn = function (address, addressType, parameters = {}) {
512
- this.once('reset', () => this.createLeConnAfterReset(address, addressType, parameters));
513
- this.reset();
511
+ // This is a mystery case for me.
512
+ // I don't know why we need to reset the hci socket before creating a LE connection.
513
+ // Issues related to this:
514
+ // https://github.com/stoprocent/noble/issues/12
515
+ // https://github.com/bluez/bluez/issues/1178
516
+ Hci.prototype.createLeConn = function (address, addressType, parameters = {}, reset = true) {
517
+ if (reset) {
518
+ this.once('reset', () => this.createLeConnAfterReset(address, addressType, parameters));
519
+ this.reset();
520
+ }
521
+ else {
522
+ this.createLeConnAfterReset(address, addressType, parameters);
523
+ }
514
524
  };
515
525
 
516
526
  Hci.prototype.createLeConnAfterReset = function (address, addressType, parameters = {}) {
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": "2.1.5",
9
+ "version": "2.1.7",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/stoprocent/noble.git"
@@ -216,7 +216,7 @@ describe('hci-socket bindings', () => {
216
216
 
217
217
  bindings.stopScanning();
218
218
 
219
- expect(bindings._gap.stopScanning).not.toHaveBeenCalled();
219
+ expect(bindings._gap.stopScanning).toHaveBeenCalledTimes(1);
220
220
  expect(scanStopSpy).toHaveBeenCalledTimes(1);
221
221
  });
222
222
  });
@@ -239,7 +239,7 @@ describe('hci-socket bindings', () => {
239
239
  should(bindings._connectionQueue[0].params).eql({ addressType: 'public' });
240
240
 
241
241
  expect(bindings._hci.createLeConn).toHaveBeenCalledTimes(1);
242
- expect(bindings._hci.createLeConn).toHaveBeenCalledWith('11:22:33:44:55:66', 'public', { addressType: 'public' });
242
+ expect(bindings._hci.createLeConn).toHaveBeenCalledWith('11:22:33:44:55:66', 'public', { addressType: 'public' }, true);
243
243
  });
244
244
 
245
245
  it('missing peripheral, no queue, random address', () => {
@@ -252,7 +252,7 @@ describe('hci-socket bindings', () => {
252
252
  should(bindings._connectionQueue[0].params).eql({ addressType: 'random' });
253
253
 
254
254
  expect(bindings._hci.createLeConn).toHaveBeenCalledTimes(1);
255
- expect(bindings._hci.createLeConn).toHaveBeenCalledWith('f3:22:33:44:55:66', 'random', { addressType: 'random' });
255
+ expect(bindings._hci.createLeConn).toHaveBeenCalledWith('f3:22:33:44:55:66', 'random', { addressType: 'random' }, true);
256
256
  });
257
257
 
258
258
  it('existing peripheral, no queue', () => {
@@ -271,7 +271,7 @@ describe('hci-socket bindings', () => {
271
271
  should(bindings._connectionQueue[0].params).eql('parameters');
272
272
 
273
273
  expect(bindings._hci.createLeConn).toHaveBeenCalledTimes(1);
274
- expect(bindings._hci.createLeConn).toHaveBeenCalledWith('address', 'addressType', 'parameters');
274
+ expect(bindings._hci.createLeConn).toHaveBeenCalledWith('address', 'addressType', 'parameters', true);
275
275
  });
276
276
 
277
277
  it('missing peripheral, with queue', () => {
@@ -801,8 +801,8 @@ describe('hci-socket bindings', () => {
801
801
  expect(connectCallback).toHaveBeenCalledTimes(1);
802
802
  expect(connectCallback).toHaveBeenCalledWith('112233445566', null);
803
803
  expect(Hci.createLeConnSpy).toHaveBeenCalledTimes(2);
804
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('112233445566', 'random', { addressType: 'random' });
805
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('998877665544', 'public', { addressType: 'public' });
804
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('112233445566', 'random', { addressType: 'random' }, true);
805
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('998877665544', 'public', { addressType: 'public' }, false);
806
806
 
807
807
  should(bindings._connectionQueue).length(1);
808
808
  });
@@ -822,14 +822,16 @@ describe('hci-socket bindings', () => {
822
822
  bindings.connect('queuedId_2', { addressType: 'public' });
823
823
  bindings.connect('queuedId_3', { addressType: 'random' });
824
824
 
825
+ bindings.emit('reset');
826
+
825
827
  bindings.on('connect', connectCallback);
826
828
  bindings.onLeConnComplete(status, handle, role, addressType, address);
827
829
 
828
830
  expect(connectCallback).toHaveBeenCalledTimes(1);
829
831
  expect(connectCallback).toHaveBeenCalledWith('112233445566', null);
830
832
  expect(Hci.createLeConnSpy).toHaveBeenCalledTimes(2);
831
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('112233445566', 'random', { addressType: 'random' });
832
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('998877665544', 'public', { addressType: 'public' });
833
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('112233445566', 'random', { addressType: 'random' }, true);
834
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('998877665544', 'public', { addressType: 'public' }, false);
833
835
  expect(bindings._connectionQueue).toHaveLength(2);
834
836
  });
835
837
 
@@ -865,9 +867,9 @@ describe('hci-socket bindings', () => {
865
867
  expect(connectCallback).toHaveBeenCalledWith('998877665544', null);
866
868
 
867
869
  expect(Hci.createLeConnSpy).toHaveBeenCalledTimes(3);
868
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('112233445566', 'random', { addressType: 'random' });
869
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('998877665544', 'public', { addressType: 'public' });
870
- expect(Hci.createLeConnSpy).toHaveBeenCalledWith('aabbccddeeff', 'random', { addressType: 'random' });
870
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('112233445566', 'random', { addressType: 'random' }, true);
871
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('998877665544', 'public', { addressType: 'public' }, false);
872
+ expect(Hci.createLeConnSpy).toHaveBeenCalledWith('aabbccddeeff', 'random', { addressType: 'random' }, false);
871
873
  expect(bindings._connectionQueue).toHaveLength(1);
872
874
  });
873
875
  });
@@ -46,7 +46,6 @@ describe('hci-socket gap', () => {
46
46
  const hci = {
47
47
  on: sinon.spy(),
48
48
  once: sinon.spy(),
49
- reset: sinon.spy(),
50
49
  setScanEnabled: sinon.spy(),
51
50
  setScanParameters: sinon.spy()
52
51
  };
@@ -54,21 +53,6 @@ describe('hci-socket gap', () => {
54
53
  const gap = new Gap(hci);
55
54
  gap.startScanning(true);
56
55
 
57
- assert.callCount(hci.once, 1);
58
- assert.calledWithExactly(hci.reset);
59
- });
60
-
61
- it('startScanningAfterReset', () => {
62
- const hci = {
63
- on: sinon.spy(),
64
- once: sinon.spy(),
65
- setScanEnabled: sinon.spy(),
66
- setScanParameters: sinon.spy()
67
- };
68
-
69
- const gap = new Gap(hci);
70
- gap.startScanningAfterReset(true);
71
-
72
56
  should(gap._scanState).equal('starting');
73
57
  should(gap._scanFilterDuplicates).equal(false);
74
58