Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-red-contrib-generic-ble

CANDY-LINE137Apache-2.04.0.3

Node-RED nodes for generic BLE devices

node-red, bluetooth, BLE, bluetooth low energy, bluetooth smart, CANDY RED, CANDY EGG

readme

node-red-contrib-generic-ble

GitHub release master Build Status

A Node-RED node set for providing access to generic BLE peripheral GATT characteristics.

As of v4.0.0, this node is optmized for Linux with BlueZ 5 D-Bus API (HCI socket is no longer used on Linux). The node should still work on macOS and Windows as nothing is modified for these platforms.

Supported operations are as follows:

  • Start BLE Scanning
  • Stop BLE Scanning
  • Restart BLE Scanning (Stop then start BLE Scanning again)
  • Connect to a peripheral device
  • Disonnect from a peripheral device
  • Read
  • Write
  • Write without Response
  • Notify (Subscribing the Notify events)

The node status modes are as follows:

  • missing the configured BLE peripheral device is missing. When the device is discovered, the state transitions to disconnected. The disconnected device may transiton to missing again when RSSI is invalidated (Linux only)
  • disconnected when the configured BLE peripheral device is found but not conncted
  • connecting when the configured BLE peripheral device is being connected
  • connected when the configured BLE peripheral device is connected
  • disconnecting when the configured BLE peripheral device is being disconnected
  • error when unexpected error occurs

Known issues for Linux BlueZ D-Bus API:

  • Unlike the older version, you must set the process owner's permission properly and manually. Non-root user's Node-RED process will fail to get this node working. Read Installation Note (Linux) below.
  • It seems the local name in advertisement packet isn't transferred to LocalName property in org.bluez.Device1 BlueZ D-Bus API. With the HCI socket implementaion, the local name was resolved. So the local name can be resolved on macOS and Windows.
  • Bluetooth: hci0: hardware error 0x03 error sometimes occurs (and logged in syslog). When it's observed, all devices are disconnected and cahches are gone. The node tries to power on the BLE adapter again.

How to use

How to configure a new BLE peripheral device

At first, drag either a Generic BLE in node or a Generic BLE out node to the workspace from the node palette and double-click the node. And you can find the following dialog. Here, click the pencil icon (1) to add a new BLE peripheral or edit the existing one.

ble out node

Then the new config node dialog appears as shown below.

The BLE Scanning shows whether or not BLE scanning is going on. In order to start BLE scanning, check it (2).

ble config node

As soon as you check it, Scan Result select box and Apply button appear. The scan results are automatically fufilled in the select box. The content will be refreshed every 10 seconds.

ble config node

Chosoe one of the listed devices and then click Apply to populate Local Name, MAC and UUID input text boxes. Clicking Apply button also triggers GATT characteristics discovery as well.

The following picure shows the Apply button clicking results. GATT Characteristics has a characteristic list of the selected device. When you see (not available) message in the box, check if the device is NOT sleeping (a sleeping device fails to respond to a connect request) and click Apply again.

GATT Characteristics must be populated as the node uses the list to verify if a given characteristic UUID is valid on performing Read, Write and Subscribe requests.

Click Add (3) to save the information when everything is OK.

ble config node

Now back to Generic BLE out node. Click Done (4) to finish the Generic BLE out node settings.

ble config node

You can also import an example flow from the menu icon() > Import > Examples > node-red-contrib-generic-ble > 01-read-write for learning more about this node.

How to translate gatttool command into flow

In this example, we show how to describe gatttool commands for characteristic value write and read with Generic BLE nodes.

NOTICE: As of BlueZ 5, gatttool is deprecated. gatttool will be removed in the future relesase.

Characteristics Value Write

The following simple command line just issues a characteristic write request to the handle 0x0027, which the BLE peripheral associates with the characteristic uuid f000aa02-0451-4000-b000-000000000000(uuids and handles can be listed by gatttool -b 88:99:00:00:FF:FF --characteristics command).

$ gatttool -b 88:99:00:00:FF:FF --char-write-req --handle=0x0027 --value=ca
Characteristic value was written successfully

In this tutorial, we translate the above command into Node-RED flow.

First of all, we use the following nodes.

  1. inject node to trigger a write request
  2. Generic BLE out node to perform the write request

gatttool

So the first step to create a flow is to place the above nodes on the workspace and connect them as shown above.

Next, open the inject dialog so that you can provide the write request parameters, the characteristic uuid and the value.

Important!) Unlike gatttool, Generic BLE nodes NEVER use handles. Always use uuids instead.

gatttool

In this dialog, choose JSON at Payload input item since Generic BLE out node accepts a JSON object as its input value. See Inputs in the node description shown in the info tab for detail.

gatttool

Click/tap ... to launch JSON value editor and populate the following JSON text.

{
    "f000aa0204514000b000000000000000": "ca"
}

The property f000aa0204514000b000000000000000 is a characteristic uuid. However, unlike gatttool, you must strip hyphens from the original uuid value. Generic BLE nodes doesn't accept gatttool style uuid format.

The value ca is a hex string to be written, which is identical to the above command line.

So you'll see the following image.

gatttool

Close the dialog by clicking Done button after entering the JSON text.

Configure Generic BLE out node for your BLE peripheral (This step is already introduced above so we don't describe here. See How to configure a new BLE peripheral).

Now you're ready to issue a characteristic write request to your BLE peripheral. Click Deploy and click inject node to issue a characteristic write request.

gatttool

Node-RED shows the notification message after your write request is performed successfully.

Here in this tutorial, we use inject node to create characteristic write request parameters. However, this isn't the only way to do so. You can use other nodes than inject node. All you need is to prepare a valid JSON object for Generic BLE out node and provide it to the node.

In order to retrieve the written value from your BLE peripheral, go to the next step.

Characteristics Value Read

The both commands perform characteristic value read commands and return the same result, the characteristic value of the uuid f000aa02-0451-4000-b000-000000000000.

$ gatttool -b 88:99:00:00:FF:FF --char-read -u f000aa02-0451-4000-b000-000000000000
handle: 0x0027      value: ca

$ gatttool -b 88:99:00:00:FF:FF --char-read --handle=0x0027
Characteristic value/descriptor: ca

In this tutorial, we translate the above commands into Node-RED flow.

We use the following nodes this time.

  1. inject node to trigger a read command
  2. Generic BLE in node to perform the read command
  3. debug node to show the read value

gatttool

Put the above nodes onto your workspace and add connectors like above.

Open inject node dialog and enter the characteristic uuid at Topic input box. Leave default values other than Topic since Generic BLE in sees only the topic value.

You can also leave Topic empty when you want to retrieve all characteristics values.

gatttool

Click Done after entering the uuid to close the dialog. You need to configure Generic BLE in node to use your BLE peripheral but we skip to mention here as the instruction is described above (See How to configure a new BLE peripheral for detail).

Click Deploy to function the flow.

gatttool

Let's read the characteristic value by clicking inject node pedal. The read result will be displayed on the debug tab.

BLE in and out nodes

See info tab for detail on the editor UI.

Example Flow

You can import the example flow on Node-RED UI.

Installation Note (Linux)

The Node-RED process owner must belong to bluetooth group in order to access BlueZ D-Bus API, otherwise this node doesn't work at all because of bluetoothd permission issue. For example, if you're going to run the process by pi user, run the following command.

sudo usermod -G bluetooth -a pi

Then reboot the OS so that the policy changes take effect.

sudo reboot

Node-RED users

Run the following commands:

cd ~/.node-red
npm install node-red-contrib-generic-ble

Then restart Node-RED process. Again, for Linux users, read the above chapter Installation Note (Linux) to get this node working.

CANDY RED users

Run the following commands:

cd $(npm -g root)/candy-red
sudo npm install --unsafe-perm node-red-contrib-generic-ble

Then restart candy-red service.

sudo systemctl restart candy-red

Appendix

How to build

# build
$ NODE_ENV=development npm run build
# package
$ NODE_ENV=development npm pack

changelog

Revision History

  • 4.0.3

    • Always show BLE error info
    • Update dependencies
  • 4.0.2

    • Fix Module Loading Error on macOS
  • 4.0.1

    • Fix Module Loading Error
  • 4.0.0

    • The node category is now Network rather than Input and Output.
    • Improve stability on Linux by introducing BlueZ 5 D-Bus API
      • On Linux, this node is no longer dependent on the HCI socket library, which has lots of problematic issues that caused inconsistent results with old BlueZ CLI tools.
      • Note that when Node-RED process is run by non-root user, add the user to bluetooth group so to access BlueZ D-Bus API. For example, run sudo usermod -G bluetooth -a pi prior to starting the process if it's run by pi user.
      • BlueZ's BLE scanning seems to detect devices having random address type. But not sure if such devices work with this node properly.
      • Tested on Raspbian (4.19.97-v7l+) and Raspberry Pi 3/4.
        • With the following packages
          • bluez 5.50-1.2~deb10u1
          • bluez-firmware 1.2-4+rpt2
    • Improve BLE Device Scanning UI/UX
      • Check BLE Scanning in order to start scanning, and Scan Result select box will be fullfilled automatically whenever devices are found. The update will be performed every 10 seconds. The first scan result will appear after 5 seconds since the scanning starts. See the updated README document for detail.
    • GENERIC_BLE_TRACE environment variable is no longer working. Use DEBUG environment variable instead.
      • DEBUG=node-red-contrib-generic-ble:index is compatible with GENERIC_BLE_TRACE=true.
      • DEBUG=node-red-contrib-generic-ble:* will output all trace logs within the project.
      • DEBUG=node-red-contrib-generic-ble:index:api will output all API endpoint access logs.
      • DEBUG=node-red-contrib-generic-ble:noble:* will output all trace logs under src/noble modules.
  • 3.1.0

    • Support Node.js v10.x LTS (Fix #14 and #17)
  • 3.0.0

    • Refactor entire architecture
    • Peripheral connections are retained until it disconnects
    • Characteristic subscriptions are retained while the ongoing flows are running (will be unsubscribed on stopping them though)
    • The max number of concurrent BLE connections is 5 or 6 according to this document
  • 2.0.4

    • Fix an issue where this node don't work with noble@1.9.x
  • 2.0.3

    • Fix an issue where noble looses a reference to a peripheral after it is disconnected
  • 2.0.2

    • Fix an issue where Write operation cannot be performed properly (#4)
  • 2.0.1

    • Fix an issue where Select from scan result failed to list characteristics
  • 2.0.0

    • Add Poll Notify Events message support so that Generic BLE out node can start to subscribe the given characteristic events
    • Support characteristic query by one or more uuids
    • Add Mute Notify Events to Generic BLE config node for this node to avoid unnecessary device connection for event subscription
    • Replace RED.log functions with node logging functions as possible to offer precise logging control via UI
    • Add Operation Timeout to Generic BLE config node to set the waiting time for Read/Write/Notify response per characteristic rather than per device
    • GENERIC_BLE_OPERATION_WAIT_MS is introduced for default Operation Timeout value
    • Remove Listening Period from Generic BLE config node
    • GENERIC_BLE_NOTIFY_WAIT_MS is removed
  • 1.0.2

    • Improve README
    • Add an example flow file available from the editor UI
  • 1.0.1

    • Fix an issue where custom characteristics cannot be listed on the Generic BLE config node dialog
  • 1.0.0

    • Fix an issue where some devices cannot be discovered within a specific time window even after they can be connected
    • Fix an issue where the Scan Result select widget didn't show the same item as the stored device info
    • Update Scan Result option list whenever Local Name is resolved
    • Improve stability by fixing minor bugs
  • 0.1.0

    • Initial Release (alpha)
    • node-red keyword is not yet added