Using Node-RED to scan for BLE devices

After playing around with Node-RED, I decided to use it for the next project I had in mind. The idea is to use a BLE (aka Bluetooth Low Energy or Bluetooth 4.0) device to automate some actions at home or on my computer. There are a couple (or more) ways to scan for BLE devices, one of them is to use directly some command line tools, like the hcitool, part of BlueZ (assuming that you are on Linux). The other is to use an existing library for your favorite developing environment, like noble for Node.js.

One of the first projects that came in mind was to use my Fitbit Flex for locking my screen/session when I am leaving my computer. The idea is not unique (see here and here for example), but since Fitbit is something I carry always on me, it looks more easy-to-use (and less pervasive) than using a smartphone.

The first step was to write a special node for Node-RED that would do the scanning and checking for a specific BLE device (by name and UUID). Writing the module was quite easy (after spending some time to understand the logic, documentation is still a bit poor on this. The 99-sample.js.demo is much more helpful). I also decided to share the new node to the community by making a pull request at the node-red-nodes repository which is dedicated for hosting nodes created by the community. The request was accepted (btw I was the first non-IBM-er to pull to  the repository) and now you can find the scanBLE node here.  The node icon looks like this:

Many thanks to Dave C-J  for adding the Bluetooth icon on the node.

So to use scanBLE in your flow, you install it first (check here for short instructions) and add it in your canvas. Doubleclick and enter the required information (BLE device name for the name of the Bluetooth device you are looking for, and UUID for its UUID). The node will scan all BLE devices and will check for the name and UUID you have entered. When found, it will trigger the message output with the name of the device as payload. For example, if you are looking for your ‘Fitbit’ device, then msg.payload=’Fitbit’.

To have the computer screen locked when the Fitbit is not around you can use the following flow:



{"id":"d3947a09.2c6b88","type":"scanBLE","name":"ScanBLE","ble_name":"Fitbit","ble_uuid":"123455555","x":271,"y":203,"wires":[["31e80866.ce17f8"]]},{"id":"31e80866.ce17f8","type":"function","name":"Check","func":"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n//   console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n//   context = {};\n\nif(msg.payload!='Fitbit'){\n\t//exec('/System/Library/CoreServices/Menu\\ Extras/ -suspend');\n\treturn msg;\n}\n","outputs":1,"x":417,"y":130,"wires":[["d1efa92e.2e1058"]]},

{"id":"d1efa92e.2e1058","type":"exec","command":"/System/Library/CoreServices/Menu\\ Extras/ -suspend'","append":"","useSpawn":"","name":"Lock()","x":556,"y":219,"wires":[["6ccbcca2.933434"],["6ccbcca2.933434"],


EDIT: Of course you need to execute a shell command for locking your screen. The flow above executes a command that will lock your Mac session:

 /System/Library/CoreServices/Menu\ Extras/ -suspend 

If you are on Linux you can try something like

  gnome-screensaver-command -l 

Note: You need a BLE interface for your Mac/Linux computer, I have used this Bluetooth 4.0 Adapter from TRUST.

4 thoughts on “Using Node-RED to scan for BLE devices

    • Hi David,

      Thanks for pointing this out, I forgot to mention in the original post about the hardware, I don’t think there is a BLE-ready Mac yet, so for both my Mac and Linux machines I have used a Bluetooth 4.0 adapter from Trust.

Leave a Reply

Your email address will not be published. Required fields are marked *