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 = ; var YOUR_THING_SERVICE_UUID = 'xxxxxxxxxxxxxxxxxxxxxxxx';var YOUR_THING_NOTIFY_CHAR = 'xxxxxxxxxxxxxxxxxxxxxxxx';var YOUR_THING_READ_CHAR = 'xxxxxxxxxxxxxxxxxxxxxxxx';var YOUR_THING_WRITE_CHAR = 'xxxxxxxxxxxxxxxxxxxxxxxx'; // then create your thing with the object patternvar { // call nobles super constructor NobleDevice; // setup or do anything else your module needs here}; // tell Noble about the service uuid(s) your peripheral advertises (optional)YourThingSCAN_UUIDS = YOUR_THING_SERVICE_UUID; // and/or specify method to check peripheral (optional)YourThing { return peripheraladvertisementlocalName === 'My Thing\'s Name';}; // inherit noble deviceNobleDeviceUtil; // you can mixin other existing service classes here too,// noble device provides battery and device information,// add the ones your device providesNobleDeviceUtil;NobleDeviceUtil; // export your devicemoduleexports = 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 = ; var id = '<your devices id>';YourThing;
It doesn't do much yet, let's go back and add to our Device definition (right before module.exports
)
// you could send some dataYourThingprototype { this;}; // read some dataYourThingprototype { this;};
Now in our connect and setup we can:
yourThing; yourThing;
Optionally, if you need to do some device setup or close something down before disconnect, you can override those functions:
YourThingprototype { NobleDeviceprototypeconnectAndSetup
Discovery API
Discover All
{// called for all devices discovered}YourThing;
Stopping a Discover All
YourThing;
Discover a single device
YourThing;
Stopping a Discover
YourThing;
Discover with Filter
YourThing;
Discover by ID
var id = " ... "; // id of device we want to discoverYourThing;