Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const NobleBindings = function () {

util.inherits(NobleBindings, events.EventEmitter);

NobleBindings.prototype.setScanParameters = function (interval, window) {
this._gap.setScanParameters(interval, window);
};

NobleBindings.prototype.startScanning = function (serviceUuids, allowDuplicates) {
this._scanServiceUuids = serviceUuids || [];

Expand Down Expand Up @@ -66,6 +70,7 @@ NobleBindings.prototype.updateRssi = function (peripheralUuid) {
NobleBindings.prototype.init = function () {
this.onSigIntBinded = this.onSigInt.bind(this);

this._gap.on('scanParametersSet', this.onScanParametersSet.bind(this));
this._gap.on('scanStart', this.onScanStart.bind(this));
this._gap.on('scanStop', this.onScanStop.bind(this));
this._gap.on('discover', this.onDiscover.bind(this));
Expand Down Expand Up @@ -129,6 +134,10 @@ NobleBindings.prototype.onAddressChange = function (address) {
this.emit('addressChange', address);
};

NobleBindings.prototype.onScanParametersSet = function () {
this.emit('scanParametersSet');
};

NobleBindings.prototype.onScanStart = function (filterDuplicates) {
this.emit('scanStart', filterDuplicates);
};
Expand Down
6 changes: 5 additions & 1 deletion lib/hci-socket/gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const Gap = function (hci) {

util.inherits(Gap, events.EventEmitter);

Gap.prototype.setScanParameters = function (interval, window) {
this._hci.setScanParameters(interval, window);
};

Gap.prototype.startScanning = function (allowDuplicates) {
this._scanState = 'starting';
this._scanFilterDuplicates = !allowDuplicates;
Expand Down Expand Up @@ -52,7 +56,7 @@ Gap.prototype.onHciError = function (error) {
};

Gap.prototype.onHciLeScanParametersSet = function () {

this.emit('scanParametersSet');
};

// Called when receive an event "Command Complete" for "LE Set Scan Enable"
Expand Down
6 changes: 3 additions & 3 deletions lib/hci-socket/hci.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ Hci.prototype.writeLeHostSupported = function () {
this._socket.write(cmd);
};

Hci.prototype.setScanParameters = function () {
Hci.prototype.setScanParameters = function (interval = 0x0010, window = 0x0010) {
const cmd = Buffer.alloc(11);

// header
Expand All @@ -311,8 +311,8 @@ Hci.prototype.setScanParameters = function () {

// data
cmd.writeUInt8(0x01, 4); // type: 0 -> passive, 1 -> active
cmd.writeUInt16LE(0x0010, 5); // internal, ms * 1.6
cmd.writeUInt16LE(0x0010, 7); // window, ms * 1.6
cmd.writeUInt16LE(interval, 5); // interval, ms * 1.6
cmd.writeUInt16LE(window, 7); // window, ms * 1.6
cmd.writeUInt8(0x00, 9); // own address type: 0 -> public, 1 -> random
cmd.writeUInt8(0x00, 10); // filter: 0 -> all event types

Expand Down
13 changes: 13 additions & 0 deletions lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function Noble (bindings) {

this._bindings.on('stateChange', this.onStateChange.bind(this));
this._bindings.on('addressChange', this.onAddressChange.bind(this));
this._bindings.on('scanParametersSet', this.onScanParametersSet.bind(this));
this._bindings.on('scanStart', this.onScanStart.bind(this));
this._bindings.on('scanStop', this.onScanStop.bind(this));
this._bindings.on('discover', this.onDiscover.bind(this));
Expand Down Expand Up @@ -93,6 +94,18 @@ Noble.prototype.onAddressChange = function (address) {
this.address = address;
};

Noble.prototype.setScanParameters = function (interval, window, callback) {
if (callback) {
this.once('scanParametersSet', callback);
}
this._bindings.setScanParameters(interval, window);
};

Noble.prototype.onScanParametersSet = function () {
debug('scanParametersSet');
this.emit('scanParametersSet');
};

const startScanning = function (serviceUuids, allowDuplicates, callback) {
if (typeof serviceUuids === 'function') {
this.emit('warning', 'calling startScanning(callback) is deprecated');
Expand Down
20 changes: 20 additions & 0 deletions test/test-noble.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('should');
const sinon = require('sinon');
const { fake, assert } = sinon;

const Noble = require('../lib/noble');

Expand All @@ -14,13 +15,18 @@ describe('Noble', () => {
mockBindings = {
init: () => {},
on: () => {},
setScanParameters: fake.returns(null),
startScanning: sinon.spy(),
stopScanning: sinon.spy()
};

noble = new Noble(mockBindings);
});

afterEach(() => {
sinon.reset();
});

describe('startScanningAsync', () => {
it('should delegate to binding', async () => {
const expectedServiceUuids = [1, 2, 3];
Expand Down Expand Up @@ -66,4 +72,18 @@ describe('Noble', () => {
await promise.should.be.resolved();
});
});

describe('setScanParameters', () => {
it('should delegate to binding', async () => {
const interval = 'interval';
const window = 'window';

const promise = noble.setScanParameters(interval, window);
noble.emit('scanParametersSet');
await promise;

assert.calledOnce(mockBindings.setScanParameters);
assert.calledWith(mockBindings.setScanParameters, interval, window);
});
});
});