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
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.{diff,md}]
quote_type = single
[*.{diff,md}]
trim_trailing_whitespace = false
11 changes: 6 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
root: true,
extends: ["eslint:recommended", "semistandard"],
extends: ['eslint:recommended', 'semistandard'],
parserOptions: {
ecmaVersion: 2017
},
Expand All @@ -13,12 +13,13 @@ module.exports = {
Promise: true
},
rules: {
"no-unused-vars": [
"error",
'space-before-function-paren': ['error', 'always'],
'no-unused-vars': [
'error',
{
args: "none"
args: 'none'
}
],
"semi": "error"
semi: 'error'
}
};
10 changes: 10 additions & 0 deletions examples/cache-gatt-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ const findServices = function (noble, peripheral) {
});

peripheral.discoverServices([], (error, services) => {
if (error) {
console.error(error);
return;
}

let sensorCharacteristic;

servicesToRead = services.length;
Expand All @@ -108,6 +113,11 @@ const findServices = function (noble, peripheral) {
});

service.discoverCharacteristics([], function (error, characteristics) {
if (error) {
console.error(error);
return;
}

console.log(`SRV\t${service.uuid} characteristic decoded data: `);
for (let j = 0; j < characteristics.length; j++) {
const ch = characteristics[j];
Expand Down
10 changes: 10 additions & 0 deletions examples/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ noble.on('discover', peripheral => {

function connectAndSetUp (peripheral) {
peripheral.connect(error => {
if (error) {
console.error(error);
return;
}

console.log('Connected to', peripheral.id);

// specify the services and characteristics to discover
Expand All @@ -47,6 +52,11 @@ function connectAndSetUp (peripheral) {
}

function onServicesAndCharacteristicsDiscovered (error, services, characteristics) {
if (error) {
console.error(error);
return;
}

console.log('Discovered services and characteristics');
const echoCharacteristic = characteristics[0];

Expand Down
38 changes: 37 additions & 1 deletion examples/peripheral-explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ function explore (peripheral) {
});

peripheral.connect(function (error) {
if (error) {
console.error(error);
return;
}

peripheral.discoverServices([], function (error, services) {
if (error) {
console.error(error);
return;
}

let serviceIndex = 0;

async.whilst(
Expand All @@ -76,6 +86,11 @@ function explore (peripheral) {
console.log(serviceInfo);

service.discoverCharacteristics([], function (error, characteristics) {
if (error) {
console.error(error);
return;
}

let characteristicIndex = 0;

async.whilst(
Expand All @@ -93,6 +108,11 @@ function explore (peripheral) {
async.series([
function (callback) {
characteristic.discoverDescriptors(function (error, descriptors) {
if (error) {
console.error(error);
return;
}

async.detect(
descriptors,
function (descriptor, callback) {
Expand All @@ -105,6 +125,10 @@ function explore (peripheral) {
function (userDescriptionDescriptor) {
if (userDescriptionDescriptor) {
userDescriptionDescriptor.readValue(function (error, data) {
if (error) {
console.error(error);
}

if (data) {
characteristicInfo += ` (${data.toString()})`;
}
Expand All @@ -122,6 +146,10 @@ function explore (peripheral) {

if (characteristic.properties.indexOf('read') !== -1) {
characteristic.read(function (error, data) {
if (error) {
console.error(error);
}

if (data) {
const string = data.toString('ascii');

Expand All @@ -141,13 +169,21 @@ function explore (peripheral) {
]);
},
function (error) {
if (error) {
console.error(error);
}

serviceIndex++;
callback();
}
);
});
},
function (err) {
function (error) {
if (error) {
console.error(error);
}

peripheral.disconnect();
}
);
Expand Down
42 changes: 33 additions & 9 deletions examples/pizza/central.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ noble.on('discover', function (peripheral) {
//
// Once the peripheral has been discovered, then connect to it.
//
peripheral.connect(function (err) {
peripheral.connect(function (error) {
if (error) {
console.error(error);
}

//
// Once the peripheral has been connected, then discover the
// services and characteristics of interest.
//
peripheral.discoverServices([pizzaServiceUuid], function (err, services) {
peripheral.discoverServices([pizzaServiceUuid], function (error, services) {
if (error) {
console.error(error);
return;
}

services.forEach(function (service) {
//
// This must be the service we were looking for.
Expand All @@ -53,7 +62,12 @@ noble.on('discover', function (peripheral) {
//
// So, discover its characteristics.
//
service.discoverCharacteristics([], function (err, characteristics) {
service.discoverCharacteristics([], function (error, characteristics) {
if (error) {
console.error(error);
return;
}

characteristics.forEach(function (characteristic) {
//
// Loop through each characteristic and match them to the
Expand Down Expand Up @@ -118,17 +132,27 @@ function bakePizza () {
if (data.length === 1) {
const result = data.readUInt8(0);
console.log('The result is',
result === pizza.PizzaBakeResult.HALF_BAKED ? 'half baked.'
: result === pizza.PizzaBakeResult.BAKED ? 'baked.'
: result === pizza.PizzaBakeResult.CRISPY ? 'crispy.'
: result === pizza.PizzaBakeResult.BURNT ? 'burnt.'
: result === pizza.PizzaBakeResult.ON_FIRE ? 'on fire!'
result === pizza.PizzaBakeResult.HALF_BAKED
? 'half baked.'
: result === pizza.PizzaBakeResult.BAKED
? 'baked.'
: result === pizza.PizzaBakeResult.CRISPY
? 'crispy.'
: result === pizza.PizzaBakeResult.BURNT
? 'burnt.'
: result === pizza.PizzaBakeResult.ON_FIRE
? 'on fire!'
: 'unknown?');
} else {
console.log('result length incorrect');
}
});
pizzaBakeCharacteristic.subscribe(function (err) {
pizzaBakeCharacteristic.subscribe(function (error) {
if (error) {
console.error(error);
return;
}

//
// Bake at 450 degrees!
//
Expand Down
14 changes: 9 additions & 5 deletions examples/pizza/pizza.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ Pizza.prototype.bake = function (temperature) {
console.log('baking pizza at', temperature, 'degrees for', time, 'milliseconds');
setTimeout(function () {
const result =
(temperature < 350) ? PizzaBakeResult.HALF_BAKED
: (temperature < 450) ? PizzaBakeResult.BAKED
: (temperature < 500) ? PizzaBakeResult.CRISPY
: (temperature < 600) ? PizzaBakeResult.BURNT
: PizzaBakeResult.ON_FIRE;
(temperature < 350)
? PizzaBakeResult.HALF_BAKED
: (temperature < 450)
? PizzaBakeResult.BAKED
: (temperature < 500)
? PizzaBakeResult.CRISPY
: (temperature < 600)
? PizzaBakeResult.BURNT
: PizzaBakeResult.ON_FIRE;
self.emit('ready', result);
}, time);
};
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Noble = require('./lib/noble');
var bindings = require('./lib/resolve-bindings')();
const Noble = require('./lib/noble');
const bindings = require('./lib/resolve-bindings')();

module.exports = new Noble(bindings);
4 changes: 2 additions & 2 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ NobleBindings.prototype.onDiscover = function (status, address, addressType, con
}

if (hasScanServiceUuids) {
var uuid = address.split(':').join('');
const uuid = address.split(':').join('');
this._addresses[uuid] = address;
this._addresseTypes[uuid] = addressType;
this._connectable[uuid] = connectable;
Expand Down Expand Up @@ -276,7 +276,7 @@ NobleBindings.prototype.onEncryptChange = function (handle, encrypt) {
};

NobleBindings.prototype.onMtu = function (address, mtu) {
var uuid = address.split(':').join('').toLowerCase();
const uuid = address.split(':').join('').toLowerCase();

this.emit('onMtu', uuid, mtu);
};
Expand Down
20 changes: 11 additions & 9 deletions lib/hci-socket/gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ Gap.prototype.onLeScanEnableSetCmd = function (enable, filterDuplicates) {

Gap.prototype.onHciLeAdvertisingReport = function (status, type, address, addressType, eir, rssi) {
const previouslyDiscovered = !!this._discoveries[address];
const advertisement = previouslyDiscovered ? this._discoveries[address].advertisement : {
localName: undefined,
txPowerLevel: undefined,
manufacturerData: undefined,
serviceData: [],
serviceUuids: [],
solicitationServiceUuids: []
};
const advertisement = previouslyDiscovered
? this._discoveries[address].advertisement
: {
localName: undefined,
txPowerLevel: undefined,
manufacturerData: undefined,
serviceData: [],
serviceUuids: [],
solicitationServiceUuids: []
};

let discoveryCount = previouslyDiscovered ? this._discoveries[address].count : 0;
let hasScanResponse = previouslyDiscovered ? this._discoveries[address].hasScanResponse : false;
Expand All @@ -123,7 +125,7 @@ Gap.prototype.onHciLeAdvertisingReport = function (status, type, address, addres
let i = 0;

while ((i + 1) < eir.length) {
var length = eir.readUInt8(i);
const length = eir.readUInt8(i);

if (length < 1) {
debug(`invalid EIR data, length = ${length}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/hci-socket/gatt.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ Gatt.prototype.discoverCharacteristics = function (serviceUuid, characteristicUu
if (opcode !== ATT_OP_READ_BY_TYPE_RESP || characteristics[characteristics.length - 1].valueHandle === service.endHandle) {
const characteristicsDiscovered = [];
for (i = 0; i < characteristics.length; i++) {
var properties = characteristics[i].properties;
const properties = characteristics[i].properties;

const characteristic = {
properties: [],
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 @@ -393,7 +393,7 @@ Hci.prototype.connUpdateLe = function (handle, minInterval, maxInterval, latency
};

Hci.prototype.cancelConnect = function () {
var cmd = Buffer.alloc(4);
const cmd = Buffer.alloc(4);

// header
cmd.writeUInt8(HCI_COMMAND_PKT, 0);
Expand Down Expand Up @@ -609,9 +609,9 @@ Hci.prototype.onSocketData = function (data) {
handle = data.readUInt16LE(1) & 0x0fff;

if (ACL_START === flags) {
var cid = data.readUInt16LE(7);
const cid = data.readUInt16LE(7);

var length = data.readUInt16LE(5);
const length = data.readUInt16LE(5);
const pktData = data.slice(9);

debug(`\t\tcid = ${cid}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ Noble.prototype.onHandleNotify = function (peripheralUuid, handle, data) {
};

Noble.prototype.onMtu = function (peripheralUuid, mtu) {
var peripheral = this._peripherals[peripheralUuid];
const peripheral = this._peripherals[peripheralUuid];
peripheral.mtu = mtu;
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"ws": "^7.4.2"
},
"scripts": {
"lint": "eslint '**/*.js'",
"lint": "eslint \"**/*.js\"",
"lint-fix": "eslint \"**/*.js\" --fix",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use \" to be able to run on windows env.

"test": "mocha -R spec test/*.js"
},
"browser": {
Expand Down
Loading