# clone_node-ble **Repository Path**: rose0gun/clone_node-ble ## Basic Information - **Project Name**: clone_node-ble - **Description**: clone_node-ble - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-09-06 - **Last Updated**: 2022-09-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # node-ble Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus [![chrvadala](https://img.shields.io/badge/website-chrvadala-orange.svg)](https://chrvadala.github.io) [![Test](https://github.com/chrvadala/node-ble/workflows/Test/badge.svg)](https://github.com/chrvadala/node-ble/actions) [![Coverage Status](https://coveralls.io/repos/github/chrvadala/node-ble/badge.svg?branch=master)](https://coveralls.io/github/chrvadala/node-ble?branch=master) [![npm](https://img.shields.io/npm/v/node-ble.svg?maxAge=2592000?style=plastic)](https://www.npmjs.com/package/node-ble) [![Downloads](https://img.shields.io/npm/dm/node-ble.svg)](https://www.npmjs.com/package/node-ble) [![Donate](https://img.shields.io/badge/donate-PayPal-green.svg)](https://www.paypal.me/chrvadala/25) # Documentation - [Documentation testing](https://github.com/chrvadala/node-ble/blob/main/docs/documentation-testing.md) - [APIs](https://github.com/chrvadala/node-ble/blob/main/docs/api.md) - [createBluetooth](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#createBluetooth) - [Bluetooth](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#Bluetooth) - [Adapter](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#Adapter) - [Device](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#Device) - [GattServer](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#GattServer) - [GattService](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#GattService) - [GattCharacteristic](https://github.com/chrvadala/node-ble/blob/main/docs/api.md#GattCharacteristic) # Install ```sh 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) ```xml ``` ## 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() ``` ## STEP 5: Disconnect When you have done you can disconnect and destroy the session. ```javascript 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 GNU/Linux 10 (buster) - Ubuntu 18.04.4 LTS - Ubuntu 20.04 LTS - Ubuntu 21.10 # 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](https://github.com/chrvadala/node-ble/pull/10) - **1.4** - Upgrades deps - **1.5** - Adds write options configuration `async writeValue (value, optionsOrOffset = {})` [#20](https://github.com/chrvadala/node-ble/pull/20); Upgrades deps - **1.6** - Upgrades deps and removes some dependencies; migrates to npm; improves gh-actions - **1.7** - Fixes compatibility issue [#30](https://github.com/chrvadala/node-ble/issues/30); Adds JSdoc; Deprecates NodeJS 10 and 12; Upgrades deps; # Contributors - [chrvadala](https://github.com/chrvadala) (author) - [pascalopitz](https://github.com/pascalopitz) - [lupol](https://github.com/lupol) # References - https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt - https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt - https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/gatt-api.txt - https://webbluetoothcg.github.io/web-bluetooth - method signatures follow, when possible, WebBluetooth standards - https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web - method signatures follow, when possible, WebBluetooth standards # Similar libraries - https://github.com/noble/noble - https://github.com/abandonware/noble (noble fork) - https://www.npmjs.com/package/node-web-bluetooth # 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 |