noble-device
A Node.js lib to abstract BLE (Bluetooth Low Energy) peripherals, using noble
Install
npm install noble-device
Usage
Take a look at the Tethercell and unofficial LightBlue Bean devices for examples, but this is how you make a basic device:
var NobleDevice = require('noble-device');
var YOUR_THING_SERVICE_UUID = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var YOUR_THING_NOTIFY_CHAR = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var YOUR_THING_READ_CHAR = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var YOUR_THING_WRITE_CHAR = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var YourThing = function(peripheral) {
NobleDevice.call(this, peripheral);
};
YourThing.SCAN_UUIDS = [YOUR_THING_SERVICE_UUID];
YourThing.is = function(peripheral) {
return (peripheral.advertisement.localName === 'My Thing\'s Name');
};
NobleDevice.Util.inherits(YourThing, NobleDevice);
NobleDevice.Util.mixin(YourThing, NobleDevice.BatteryService);
NobleDevice.Util.mixin(YourThing, NobleDevice.DeviceInformationService);
module.exports = YourThing;
Now to use YourThing
you must use one of the discover functions documented below which will find your device(s) and pass instances of your object to their callback where you must call connectAndSetUp
.
var YourThing = require('YourThing');
var id = '<your devices id>';
YourThing.discoverById(function(yourThingInstance) {
yourThingInstance.on('disconnect', function() {
console.log('we got disconnected! :( ');
});
yourThingInstance.connectAndSetUp(function(error) {
console.log('were connected!');
});
});
It doesn't do much yet, let's go back and add to our Device definition (right before module.exports
)
YourThing.prototype.send = function(data, done) {
this.writeDataCharacteristic(YOUR_THING_SERVICE_UUID, YOUR_THING_WRITE_CHAR, data, done);
};
YourThing.prototype.receive = function(callback) {
this.readDataCharacteristic(YOUR_THING_SERVICE_UUID, YOUR_THING_READ_CHAR, callback);
};
Now in our connect and setup we can:
yourThing.send(new Buffer([0x00, 0x01]), function() {
console.log('data sent');
});
yourThing.receive(function(error, data) {
console.log('got data: ' + data);
});
Optionally, if you need to do some device setup or close something down before disconnect, you can override those functions:
YourThing.prototype.connectAndSetup = function(callback) {
NobleDevice.prototype.connectAndSetUp.call(this, function(error) {
this.notifyCharacteristic(YOUR_THING_SERVICE_UUID, YOUR_THING_NOTIFY_CHAR, true, this._onRead.bind(this), function(err) {
callback(err);
});
}.bind(this);
};
YourThing.prototype.onDisconnect = function() {
NobleDevice.prototype.onDisconnect.call(this);
};
Discovery API
Discover All
function onDiscover(yourThingInstance) {
}
YourThing.discoverAll(onDiscover);
Stopping a Discover All
YourThing.stopDiscoverAll(onDiscover);
Discover a single device
YourThing.discover(function(yourThingInstance) {
});
Stopping a Discover
YourThing.stopDiscover(onDiscoverCallback);
Discover with Filter
YourThing.discoverWithFilter(function(device), {
return true;
}, function(yourThingInstance) {
});
Discover by ID
var id = " ... ";
YourThing.discoverById(id, function(yourThingInstance) {
});