• Stars
    star
    308
  • Rank 135,712 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus

node-ble

Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus

chrvadala Test Coverage Status npm Downloads Donate

Documentation

Install

npm install node-ble

Examples

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)

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
  "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy user="%userid%">
   <allow own="org.bluez"/>
    <allow send_destination="org.bluez"/>
    <allow send_interface="org.bluez.GattCharacteristic1"/>
    <allow send_interface="org.bluez.GattDescriptor1"/>
    <allow send_interface="org.freedesktop.DBus.ObjectManager"/>
    <allow send_interface="org.freedesktop.DBus.Properties"/>
  </policy>
</busconfig>

STEP 1: Get Adapter

To start a Bluetooth Low Energy (BLE) connection you need a Bluetooth adapter.

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.

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.

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.

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.

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()

STEP 5: Disconnect

When you have done you can disconnect and destroy the session.

await device.disconnect()
destroy()

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/about

Node-ble has been tested on the following environment:

  • Raspbian
  • Ubuntu

Changelog

  • 0.x - Beta version
  • 1.0 - First official version
  • 1.1 - Migrates to gh-workflows
  • 1.2 - Upgrades deps
  • 1.3 - Adds typescript definitions #10
  • 1.4 - Upgrades deps
  • 1.5 - Adds write options configuration async writeValue (value, optionsOrOffset = {}) #20; Upgrades deps
  • 1.6 - Upgrades deps and removes some dependencies; migrates to npm; improves gh-actions
  • 1.7 - Fixes compatibility issue #30; Adds JSdoc; Deprecates NodeJS 10 and 12; Upgrades deps;
  • 1.8 - Upgrades deps and gh-actions os; Adds Bluetooth.activeAdapters() func #45;
  • 1.9 - Upgrades deps; Adds writeValueWithoutResponse() and writeValueWithResponse methods #47; Improves typescript definition #48

Contributors

References

Similar libraries

Useful commands

Command Description
rm -r /var/lib/bluetooth/* Clean Bluetooth cache
hciconfig -a Adapter info
hcitool dev Adapter info (through Bluez)
d-feet DBus debugging tool
nvram bluetoothHostControllerSwitchBehavior=never Only on Parallels

More Repositories

1

react-svg-pan-zoom

๐Ÿ‘€ A React component that adds pan and zoom features to SVG
JavaScript
677
star
2

transformation-matrix

Javascript isomorphic 2D affine transformations written in ES6 syntax. Manipulate transformation matrices with this totally tested library!
JavaScript
360
star
3

music-beat-detector

music-beat-detector is a library that analyzes a music stream and detects any beat. It can be used to control lights or any magic effect by the music wave.
JavaScript
254
star
4

awesome-protocols

Curated list of awesome technology protocols with a reference to official RFCs
28
star
5

create-music-stream

Creates a PCM 16 bit Little Endian Stream from a mp3 file or youtube video
JavaScript
24
star
6

potree-converter-docker

Potree Converter utility packed as Docker Container
Dockerfile
19
star
7

react-refactor

๐Ÿ”ง Convert your React Class Component to Functional Component and vice-versa
JavaScript
16
star
8

neopixel

Node.js library to control Adafruit NeoPixel based on ws2812 led family
C++
15
star
9

docker-compose-examples

๐Ÿ’ป This project intends to collect common docker-compose use cases. Each example can be used as a boilerplate.
PHP
13
star
10

kafka-test-helper

Utility library that simplify testing of Node.js components that interacts with Kafka broker.
JavaScript
8
star
11

weather-station

Connector able to download weather data from an Oregon Scientific EMR211X station, leveraging on Bluetooth Low Energy connection
JavaScript
6
star
12

mqtt-dispatcher

MQTT dispatcher is a library that extends MQTT.js and allows to route incoming messages on a specific handler, according to defined routing rules.
JavaScript
6
star
13

biconnected-components

Find biconnected component in a graph using algorithm by John Hopcroft and Robert Tarjan
JavaScript
3
star
14

aggregate-fn

Aggregate fn is a tiny Javascript utility that groups together multiple async operations.
JavaScript
3
star
15

const-version

โœจ A tiny command line tool that extract `package.json` version and generate `export const VERSION='...'` file. Useful when you want to package your software version.
JavaScript
3
star
16

tableauSolver

Prolog algorithm that applies the method of analytic tableaux and creates a graphical representation of a propositional tableau
Prolog
2
star
17

github-actions

Github actions that automates some common scenarios.
2
star
18

react-redux-boilerplate

๐Ÿ˜‹ A suggested React + Redux projects structure.
JavaScript
2
star
19

aws-cloud-stacks

AWS CloudFormation stacks that speed-up projects development.
Shell
2
star
20

cisco-decrypt-docker

๐Ÿ™ˆ Docker container with Decoder for password encoding of Cisco VPN client
C
2
star
21

chrvadala.github.io

Hello, this is Christian Vadalร 's website. Here you can find a list of open source projects, backed by React, Node.JS and AWS technologies.
JavaScript
1
star
22

react-stylerify

A browserify wrapper around react-styling
JavaScript
1
star
23

sudoku.go

๐ŸŽฒ Sudoku solver written in golang
Go
1
star
24

sliding-window-max

Given a stream of data, this algorithm returns (for every added value) the current max value.
JavaScript
1
star
25

setup-my-pc

setup-my-pc.sh is a simple script that configures personal computers, virtual machines and servers with some common tools. It fastify the setup of a new development environment.
Shell
1
star