Noble
A Node.js BLE (Bluetooth Low Energy) central module
Install / Use
/learn @noble/NobleREADME
A Node.js BLE (Bluetooth Low Energy) central module.
Want to implement a peripheral? Checkout bleno
Note: macOS / Mac OS X, Linux, FreeBSD and Windows are currently the only supported OSes. Other platforms may be developed later on.
Prerequisites
OS X
- install Xcode
Linux
- Kernel version 3.6 or above
libbluetooth-dev
Ubuntu/Debian/Raspbian
sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev
Make sure node is on your path, if it's not, some options:
- symlink
nodejstonode:sudo ln -s /usr/bin/nodejs /usr/bin/node - install Node.js using the NodeSource package
Fedora / Other-RPM based
sudo yum install bluez bluez-libs bluez-libs-devel
Intel Edison
See Configure Intel Edison for Bluetooth LE (Smart) Development
FreeBSD
Make sure you have GNU Make:
sudo pkg install gmake
Disable automatic loading of the default Bluetooth stack by putting no-ubt.conf into /usr/local/etc/devd/no-ubt.conf and restarting devd (sudo service devd restart).
Unload ng_ubt kernel module if already loaded:
sudo kldunload ng_ubt
Make sure you have read and write permissions on the /dev/usb/* device that corresponds to your Bluetooth adapter.
Windows
node-gyp requirements for Windows
Install the required tools and configurations using Microsoft's windows-build-tools from an elevated PowerShell or cmd.exe (run as Administrator).
npm install --global --production windows-build-tools
node-bluetooth-hci-socket prerequisites
- Compatible Bluetooth 4.0 USB adapter
- WinUSB driver setup for Bluetooth 4.0 USB adapter, using Zadig tool
See @don's set up guide on Bluetooth LE with Node.js and Noble on Windows
Notes
Maximum simultaneous connections
This limit is imposed upon by the Bluetooth adapter hardware as well as it's firmware.
| Platform | | | :------- | --- | | OS X 10.11 (El Capitan) | 6 | | Linux/Windows - Adapter dependent | 5 (CSR based adapter) |
Adapter specific known issues
Some BLE adapters cannot connect to a peripheral while they are scanning (examples below). You will get the following messages when trying to connect :
Sena UD-100 (Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)) : Error: Command disallowed
Intel Dual Band Wireless-AC 7260 (Intel Corporation Wireless 7260 (rev 73)) : Error: Connection Rejected due to Limited Resources (0xd)
You need to stop scanning before trying to connect in order to solve this issue.
Install
npm install noble
Usage
var noble = require('noble');
Actions
Start scanning
noble.startScanning(); // any service UUID, no duplicates
noble.startScanning([], true); // any service UUID, allow duplicates
var serviceUUIDs = ["<service UUID 1>", ...]; // default: [] => all
var allowDuplicates = <false|true>; // default: false
noble.startScanning(serviceUUIDs, allowDuplicates[, callback(error)]); // particular UUID's
NOTE: noble.state must be poweredOn before scanning is started. noble.on('stateChange', callback(state)); can be used register for state change events.
Stop scanning
noble.stopScanning();
Peripheral
Connect
peripheral.connect([callback(error)]);
Disconnect or cancel pending connection
peripheral.disconnect([callback(error)]);
Update RSSI
peripheral.updateRssi([callback(error, rssi)]);
Discover services
peripheral.discoverServices(); // any service UUID
var serviceUUIDs = ["<service UUID 1>", ...];
peripheral.discoverServices(serviceUUIDs[, callback(error, services)]); // particular UUID's
Discover all services and characteristics
peripheral.discoverAllServicesAndCharacteristics([callback(error, services, characteristics)]);
Discover some services and characteristics
var serviceUUIDs = ["<service UUID 1>", ...];
var characteristicUUIDs = ["<characteristic UUID 1>", ...];
peripheral.discoverSomeServicesAndCharacteristics(serviceUUIDs, characteristicUUIDs, [callback(error, services, characteristics));
Service
Discover included services
service.discoverIncludedServices(); // any service UUID
var serviceUUIDs = ["<service UUID 1>", ...];
service.discoverIncludedServices(serviceUUIDs[, callback(error, includedServiceUuids)]); // particular UUID's
Discover characteristics
service.discoverCharacteristics() // any characteristic UUID
var characteristicUUIDs = ["<characteristic UUID 1>", ...];
service.discoverCharacteristics(characteristicUUIDs[, callback(error, characteristics)]); // particular UUID's
Characteristic
Read
characteristic.read([callback(error, data)]);
Write
characteristic.write(data, withoutResponse[, callback(error)]); // data is a buffer, withoutResponse is true|false
withoutResponse:false: send a write request, used with "write" characteristic propertytrue: send a write command, used with "write without response" characteristic property
Broadcast
characteristic.broadcast(broadcast[, callback(error)]); // broadcast is true|false
Subscribe
characteristic.subscribe([callback(error)]);
- subscribe to a characteristic, triggers
'data'events when peripheral sends an notification or indication - use for characteristics with notify or indicate properties
Unsubscribe
characteristic.unsubscribe([callback(error)]);
- unsubscribe to a characteristic
- use for characteristics with notify or indicate properties
Discover descriptors
characteristic.discoverDescriptors([callback(error, descriptors)]);
Read value
descriptor.readValue([callback(error, data)]);
Write value
descriptor.writeValue(data[, callback(error)]); // data is a buffer
Handle
Read
peripheral.readHandle(handle, callback(error, data));
Write
peripheral.writeHandle(handle, data, withoutResponse, callback(error));
Events
See Node.js EventEmitter docs for more info. on API's.
Adapter state change
state = <"unknown" | "resetting" | "unsupported" | "unauthorized" | "poweredOff" | "poweredOn">
noble.on('stateChange', callback(state));
Scan started:
noble.on('scanStart', callback);
The event is emitted when scanning is started or if another application enables scanning or changes scanning settings.
Scan stopped
noble.on('scanStop', callback);
The event is emitted when scanning is stopped or if another application stops scanning.
Peripheral discovered
peripheral = {
id: "<id>",
address: "<BT address">, // Bluetooth Address of device, or 'unknown' if not known
addressType: "<BT address type>", // Bluetooth Address type (public, random), or 'unknown' if not known
connectable: <connectable>, // true or false, or undefined if not known
advertisement: {
localName: "<name>",
txPowerLevel: <int>,
serviceUuids: ["<service UUID>", ...],
serviceSolicitationUuid: ["<service solicitation UUID>", ...],
manufacturerData: <Buffer>,
serviceData: [
{
uuid: "<service UUID>"
data: <Buffer>
},
...
]
},
rssi: <rssi>
};
noble.on('discover', callback(peripheral));
Note: on OS X the address will be set to 'unknown' if the device has not been connected previously.
Warnings
noble.on('warning', callback(message));
Peripheral
Connected
peripheral.once('connect', callback);
Disconnected:
peripheral.once('disconnect', callback);
RSSI update
peripheral.once('rssiUpdate', callback(rssi));
Services discovered
peripheral.once('servicesDiscover', callback(services));
Service
Included services discovered
service.once('includedServicesDiscover', callback(includedServiceUuids));
Characteristics discovered
characteristic = {
uuid: "<uuid>",
// properties: 'broadcast', 'read', 'writeWithoutResponse', 'write', 'notify', 'indicate', 'authenticatedSignedWrites', 'extendedProperties'
properties: [...]
};
service.once('characteristicsDiscover', callback(characteristics));

