Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus
npm install @sunookitsune/node-bleBluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus






sh
yarn add node-ble
`Example
Provide permissions
In order to allow a connection with the DBus daemon, you have to set up right permissions.Create the file
/etc/dbus-1/system.d/node-ble.conf with the following content (customize with userid)`xml
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
`STEP 1: Get Adapter
To start a Bluetooth Low Energy (BLE) connection you need a Bluetooth adapter.`javascript
const {createBluetooth} = require('node-ble')
const {bluetooth, destroy} = createBluetooth()
const adapter = await bluetooth.defaultAdapter()
`STEP 2: Start discovering
In order to find a Bluetooth Low Energy device out, you have to start a discovery operation.
`javascript
if (! await adapter.isDiscovering())
await adapter.startDiscovery()
`STEP 3: Get a device, Connect and Get GATT Server
Use an adapter to get a remote Bluetooth device, then connect to it and bind to the GATT (Generic Attribute Profile) server.`javascript
const device = await adapter.waitDevice('00:00:00:00:00:00')
await device.connect()
const gattServer = await device.gatt()
`STEP 4a: Read and write a characteristic.
`javascript
const service1 = await gattServer.getPrimaryService('uuid')
const characteristic1 = await service1.getCharacteristic('uuid')
await characteristic1.writeValue(Buffer.from("Hello world"))
const buffer = await characteristic1.readValue()
console.log(buffer)
`STEP 4b: Subscribe to a characteristic.
`javascript
const service2 = await gattServer.getPrimaryService('uuid')
const characteristic2 = await service2.getCharacteristic('uuid')
await characteristic2.startNotifications()
characteristic2.on('valuechanged', buffer => {
console.log(buffer)
})
await characteristic2.stopNotifications()
`$3
When you have done you can disconnect and destroy the session.
`javascript
await device.disconnect()
destroy()
`Reference
`javascript
const {createBluetooth} = require('node-ble')
const {bluetooth, destroy} = createBluetooth()
`| Method | Description |
| --- | --- |
|
Bluetooth bluetooth |
|void destroy() |Bluetooth
| Method | Description |
| --- | --- |
| Promise | List of available adapters |
| Promise | Get an available adapter |
| Promise | Get a specific adapter (one of available in adapters())|Adapter
| Method | Description |
| --- | --- |
| Promise | The Bluetooth device address. |
| Promise| The Bluetooth Address Type. One of public or random. |
| Promise| The Bluetooth system name (pretty hostname). |
| Promise| The Bluetooth friendly name. |
| Promise| Adapter power status. |
| Promise| Indicates that a device discovery procedure is active. |
| Promise| Starts the device discovery session. |
| Promise| Cancel any previous StartDiscovery transaction. |
| Promise| List of discovered Bluetooth Low Energy devices |
| Promise| Returns an available Bluetooth Low Energy (waitDevice is preferred)|
| Promise| Returns a Bluetooth Low Energy device as soon as it is available |
| Promise| User friendly adapter name |Device extends EventEmitter
| Method | Description |
| --- | --- |
| Promise | The Bluetooth remote name. |
| Promise | The Bluetooth device address of the remote device. |
| Promise | The Bluetooth Address Type. One of public or random. |
| Promise | The name alias for the remote device. |
| Promise | Received Signal Strength Indicator of the remote device. |
| Promise | Indicates if the remote device is paired. |
| Promise | Indicates if the remote device is currently connected. |
| Promise | Connects to the remote device, initiate pairing and then retrieve GATT primary services (needs a default agent to handle wizard).|
| Promise | This method can be used to cancel a pairing operation initiated by the Pair method. |
| Promise | This is a generic method to connect any profiles the remote device supports that can be connected to and have been flagged as auto-connectable on our side. |
| Promise | This method gracefully disconnects all connected profiles and then terminates low-level ACL connection. |
| Promise | Waits services resolving, then returns a connection to the remote Gatt Server
| Promise | User friendly device name. || Event | Description |
| --- | --- |
|
connect | Connected to device |
| disconnect | Disconnected from device |GattServer
| Method | Description |
| --- | --- |
| Promise | List of available services |
| Promise | Returns a specific Primary Service |GattService
| Method | Description |
| --- | --- |
| Promise | Indicates whether or not this GATT service is a primary service. |
| Promise | 128-bit service UUID. |
| Promise | List of available characteristic UUIDs. |
| Promise | Returns a specific characteristic. |
| Promise | User friendly service name. |GattCharacteristic extends EventEmitter
| Method | Description |
| --- | --- |
| Promise | 128-bit characteristic UUID. |
| Promise | Defines how the characteristic value can be used. |
| Promise | True, if notifications or indications on this characteristic are currently enabled. |
| Promise | Issues a request to read the value of the characteristic and returns the value if the operation was successful. |
| Promise | Issues a request to write the value of the characteristic. |
| Promise | Starts a notification session from this characteristic if it supports value notifications or indications. |
| Promise | This method will cancel any previous StartNotify transaction. |
| Promise | User friendly characteristic name. || Event | Description |
| --- | --- |
| valuechanged | New value is notified. (invoke
startNotifications() to enable notifications)Compatibility
This library works on many architectures supported by Linux.
It leverages on Bluez driver, a component supported by the following platforms and distributions https://www.bluez.org/aboutNode-ble has been tested on the following environment:
- Raspbian GNU/Linux 10 (buster)
- Ubuntu 18.04.4 LTS
Changelog
- 0.x - Beta version
- 1.0 - First official version
- 1.1 - Migrates to gh-workflows
- 1.2 - Upgrades depsContributors
- chrvadala (author)Run tests
In order to run test suite you have to set up right DBus permissions.Create the file
/etc/dbus-1/system.d/node-ble-test.conf with the following content (customize with userid)`xml
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
`$3
`
yarn test
`$3
The end to end test will try to connect to a real bluetooth device and read some characteristics. To do that, you need two different devices.
#### Device 1
`shell script
wget https://git.kernel.org/pub/scm/bluetooth/bluez.git/plain/test/example-advertisement
wget https://git.kernel.org/pub/scm/bluetooth/bluez.git/plain/test/example-gatt-server
python example-advertisement
python example-gatt-server
hcitool dev #this command shows bluetooth mac address
`#### Device 2
`shell script
.env
TEST_DEVICE=00:00:00:00:00:00
TEST_SERVICE=12345678-1234-5678-1234-56789abcdef0
TEST_CHARACTERISTIC=12345678-1234-5678-1234-56789abcdef1
TEST_NOTIFY_SERVICE=0000180d-0000-1000-8000-00805f9b34fb
TEST_NOTIFY_CHARACTERISTIC=00002a37-0000-1000-8000-00805f9b34fb
``shell script
yarn test:e2e
``