Bluetooth node for linux

1,552 views
Skip to first unread message

Mattias Månsson

unread,
Apr 9, 2015, 2:38:31 PM4/9/15
to node...@googlegroups.com
Hi!

I'm thinking a little bit about adding bluetooth support to use it for checking if I (or any other user) is at home. I tried a little bit in command line with hcitool and l2ping and I could find out the bluetooth address of my phone and also ping it. I'm wondering if there is any good node implementations for this? I noticed that l2ping is not runnable without root however, so I'm guessing there could be a problem doing this from node-red if I'm not running it under root?

Luis Montes

unread,
Apr 9, 2015, 2:40:35 PM4/9/15
to node...@googlegroups.com
Noble is the node ble stack, and there's already a node for it:  https://github.com/kmi/node-red-contrib-noble

On Thu, Apr 9, 2015 at 11:38 AM, Mattias Månsson <mattias...@gmail.com> wrote:
Hi!

I'm thinking a little bit about adding bluetooth support to use it for checking if I (or any other user) is at home. I tried a little bit in command line with hcitool and l2ping and I could find out the bluetooth address of my phone and also ping it. I'm wondering if there is any good node implementations for this? I noticed that l2ping is not runnable without root however, so I'm guessing there could be a problem doing this from node-red if I'm not running it under root?

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mattias Månsson

unread,
Apr 9, 2015, 2:43:59 PM4/9/15
to node...@googlegroups.com
I will check it out! Thanks! :)

Mattias Månsson

unread,
Apr 9, 2015, 2:50:38 PM4/9/15
to node...@googlegroups.com
Only get "Unable to start BLE scan. Adapter state: unsupported" from that node. I have a simple BT 4.0 dongle that says it supports bluetooth low energy on the package. Anyone knows what could be wrong? Do I have to run node-red with root?

Mattias Månsson

unread,
Apr 9, 2015, 2:58:11 PM4/9/15
to node...@googlegroups.com
Think it might have needed root yes. Found a fix by using setcap:

sudo setcap cap_net_raw+eip /usr/lib/node_modules/node-red-contrib-noble/node_modules/noble/build/Release/hci-ble

Edward Vielmetti

unread,
Apr 9, 2015, 3:02:54 PM4/9/15
to node...@googlegroups.com, mattias...@gmail.com
Mattias, 

You either have to run Node-RED as root, or you have to have "capabilities" turned on in Linux.

https://github.com/sandeepmistry/noble has the instructions; look for the bit where you do "sudo apt-get install libcap2-bin" to get the "setcap" tool installed. From there it's easy.

Mattias Månsson

unread,
Apr 9, 2015, 3:17:34 PM4/9/15
to node...@googlegroups.com, mattias...@gmail.com
Yep, I managed to do that. However, it seems my dongle is not BLE compliant after all:

noble warning: adapter does not support Bluetooth Low Energy (BLE, Bluetooth Smart).
               Try to run with environment variable:
               [sudo] NOBLE_HCI_DEVICE_ID=x node ..

I thought all BT 4.0 adapters was that? It's only a simple china product, but still...

Edward Vielmetti

unread,
Apr 9, 2015, 3:31:46 PM4/9/15
to node...@googlegroups.com
Hm, Mattias, that's interesting. What does "lsusb" say about the device? Perhaps it's an imitation part (the label says CSR for "Cambridge Silicon Radio", which is now owned by Qualcomm).

Geoffrey Arnold

unread,
Apr 9, 2015, 3:37:18 PM4/9/15
to node...@googlegroups.com, mattias...@gmail.com
Hey Mattias,

If `sudo hcitool lescan` is showing your BLE device, then your dongle is working fine.  If you get an I/O error, you'll have to cycle your BLE adapter (`sudo hciconfig hci0 down && sudo hciconfig hci0 up`) and try again.

I often run into this issue, but I'm not sure if it's a problem with Noble or Bluez.  Normally cycling the BLE adapter, then restarting Node-RED, fixes the issue.

Hope this helps,
Geoff.

Mattias Månsson

unread,
Apr 9, 2015, 4:16:25 PM4/9/15
to node...@googlegroups.com
It says Bus 004 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

--
http://nodered.org
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/HhTItZrXNkA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

Dave C-J

unread,
Apr 9, 2015, 4:21:00 PM4/9/15
to node...@googlegroups.com
Mattias, 

are you sure you only have one bluetooth device ? - My Thinkpad has (an old) Bluetooth2 adapter in as well - and I need to disable that - as noble seems to want to use the first adapter found... What does 
hcitool dev
report ?

Mattias Månsson

unread,
Apr 9, 2015, 4:26:03 PM4/9/15
to node...@googlegroups.com
Yes, this is a stationary server, it has no bluetooth builtin. 

$ hcitool dev/
Devices:
hci0 00:1A:7D:DA:71:13

However a reset seemed to at least make lescan possible:

$ sudo hciconfig hci0 reset
$ sudo hcitool lescan
LE Scan ...

I can't find my phone though (Xperia Z3 compact), it's suppose to support BLE, but maybe not through the Bluetooth settings, maybe you have to do it through the android API, I'm not sure. It comes up with hcitool scan, but not with hcitool lescan...

Julian Knight

unread,
Apr 9, 2015, 4:47:14 PM4/9/15
to node...@googlegroups.com
I've just put this onto my new Pi2 - thanks for the reminded by the way, it was on my todo list to try the new BT 4 dongle I got to go with it :)

I followed the instructions and it has all worked for me.

However, I have the same scanning issue. I've tried with an iPhone 5, Nokia 835, Microsoft Surface Pro 3. I'm sure at least one of those supports BTLE but the scan isn't finding any of them though I have them all on the config pages in discoverable mode.

I guess, I'll need to wait now until I get a low cost BTLE addon for the Arduino's. Like you, I want to do presence sensing via people's phones so I know who is in the house and how to set things according to their preferences. So it is a little disappointing that they don't recognise the phones. Perhaps you have to pair them first?

Mattias Månsson

unread,
Apr 9, 2015, 4:50:27 PM4/9/15
to node...@googlegroups.com
Don't really know exactly how BLE is suppose to work with phones either. So any hints are welcome. I'm waiting for these toys too, not exactly sure what I'm going to use them for, but hoping to reverse engineer the protocol and use them for something...


--

Julian Knight

unread,
Apr 9, 2015, 5:03:43 PM4/9/15
to node...@googlegroups.com

Julian Knight

unread,
Apr 9, 2015, 5:17:16 PM4/9/15
to node...@googlegroups.com
OK, first problem!

dpkg --status bluez | grep '^Version:'
Version: 4.99-2

It would seem as though most BTLE requires Bluez v5+ so the version on the Pi is too old. Certainly trying to follow some of the commands in the guides fail so that isn't that helpful.

Geoffrey Arnold

unread,
Apr 9, 2015, 5:21:52 PM4/9/15
to node...@googlegroups.com

You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Julian Knight

unread,
Apr 9, 2015, 5:36:25 PM4/9/15
to node...@googlegroups.com
Ha ha! Well met Geoff, I should have remembered that.

I'll clone your scripts and see how I get on. Many thanks.

Julian Knight

unread,
Apr 9, 2015, 5:58:42 PM4/9/15
to node...@googlegroups.com
Annoyingly, the Bluez 5.30 install is failing with:

configure: error: GLib >= 2.28 is required

I've done an:

sudo apt-get install glib-2.6

Which seemed to work fine but I still get the error.

All of this reminds me why I stay on Windows for my desktop!!

Julian Knight

unread,
Apr 9, 2015, 6:07:40 PM4/9/15
to node...@googlegroups.com
Looks like the instructions here: http://www.elinux.org/RPi_Bluetooth_LE
may be working. I'm compiling now. Should take around 1/2 Hr.

Geoffrey Arnold

unread,
Apr 9, 2015, 10:15:56 PM4/9/15
to node...@googlegroups.com
Hey Julian, 

I'm a bit bummed that our scripts didn't work for you.  We imaged 4 Pis today (one of which was an Pi 2) with BlueZ 5.30, all successful.  Could you let me know what Pi distro are you running, and how did you install it (NOOBS, Raspbian image, etc.)?

Thanks!
Geoff.



Walter Kraembring

unread,
Mar 12, 2016, 12:32:15 PM3/12/16
to Node-RED
Hello,
Playing a bit with Jessie on a Pi and got BLE scan working ok.

Now next, I would like to have access to noble from inside function nodes. So I started with adding this to settings.js:

    functionGlobalContext: {
        noble:require('/home/pi/.node-red/node_modules/node-red-contrib-noble/node_modules/noble')
// os:require('os'),
        // bonescript:require('bonescript'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },


Inside the function node, I tried this but it seems not to work:

var noble = context.global.noble;


Any hints on what is the proper way is very much appreciated

Kind regards, Walter

Dave C-J

unread,
Mar 12, 2016, 2:39:40 PM3/12/16
to node...@googlegroups.com

Looks about right. Have you run the setcap command to allow non-root access ?

Walter Kraembring

unread,
Mar 12, 2016, 4:57:33 PM3/12/16
to Node-RED
Hello Dave, the setcap did it!
Now the code in the function node is scanning

sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)



Thanks,
Walter

Walter Kraembring

unread,
Mar 12, 2016, 5:20:08 PM3/12/16
to Node-RED
I was too quick, it did not work (was fooled by another flow, sca ble + debug node that does work)

BR

Julian Knight

unread,
Mar 12, 2016, 7:43:26 PM3/12/16
to Node-RED
Ha, lost track of that conversation! Gave up on Bluetooth for the moment, too complex. I was not getting consistent results. Sometimes it would pick up things from people walking by in the street but often wouldn't recognise my phones nearby. Will try again when I have time.

Walter Kraembring

unread,
Mar 16, 2016, 2:00:22 PM3/16/16
to Node-RED
Alright, so got it working finally

See attached flow below

I have a small Kensington Proximo Tag that is recognized. You can adjust the rssi levels in the function node to set the tracking area


What would be nice if I could start it playing since it is supporting this. Currently that is too complex related to my javascript skills, maybe someone knows how to?

Best regards, Walter

You need to add the following to your settings.js

    functionGlobalContext: {
       noble:require('/home/pi/.node-red/node_modules/node-red-contrib-noble/node_modules/noble')
      // os:require('os'),
       // bonescript:require('bonescript'),
       // jfive:require("johnny-five"),
       // j5board:require("johnny-five").Board({repl:false})
   },



[{"id":"eb00084f.14fff8","type":"exec","z":"9703de.ff68fc2","command":"sudo hciconfig hci0 up","addpay":false,"append":"","useSpawn":"","name":"","x":559,"y":520,"wires":[[],[],[]]},{"id":"bb1be0fb.44e42","type":"inject","z":"9703de.ff68fc2","name":"","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":true,"x":130,"y":440,"wires":[["f1f8d3ce.0e073","e23458b3.1dcba8","1c4fa554.e3b05b","40e54401.bf1abc"]]},{"id":"6840f596.97bf0c","type":"comment","z":"9703de.ff68fc2","name":"Get the bluetooth adaptor running on startup","info":"","x":246.5,"y":360,"wires":[]},{"id":"1c4fa554.e3b05b","type":"exec","z":"9703de.ff68fc2","command":"sudo hciconfig hci0 down","addpay":false,"append":"","useSpawn":"","name":"","x":388,"y":440,"wires":[[],[],[]]},{"id":"549836fc.ab67c8","type":"function","z":"9703de.ff68fc2","name":"","func":"/*\n  Continously scans for peripherals and prints out message when they enter/exit\n\n    In range criteria:      RSSI > RSSI_THRESHOLD_IN\n    Out of range criteria:  RSSI < RSSI_THRESHOLD_OUT\n\n*/\nvar noble = context.global.noble;\nvar RSSI_THRESHOLD_IN    = -85;\nvar RSSI_THRESHOLD_OUT    = -87;\nvar EXIT_CHECK_PERIOD = 2000; // milliseconds\nvar inRange = [];\n\nnoble.on('discover', function(peripheral) {\n  if (peripheral.rssi < RSSI_THRESHOLD_IN) {\n    // ignore\n    return;\n  }\n  var id = peripheral.id;\n  var entered = !inRange[id];\n  if (entered) {\n    inRange[id] = {\n      peripheral: peripheral\n    };\n    msg.payload = ('\"' + peripheral.advertisement.localName + '\" entered (RSSI ' + peripheral.rssi + ') ' + new Date());\n    node.send(msg);\n  }\n  inRange[id].lastSeen = Date.now();\n});\n\nsetInterval(function() {\n  for (var id in inRange) {\n    var peripheral = inRange[id].peripheral;\n    if (peripheral.rssi < RSSI_THRESHOLD_IN) {\n      msg.payload = peripheral.rssi;\n      node.send(msg);\n    }\n    if (peripheral.rssi < RSSI_THRESHOLD_OUT) {\n      msg.payload =('\"' + peripheral.advertisement.localName + '\" exited (RSSI ' + peripheral.rssi + ') ' + new Date());\n      node.send(msg);\n      delete inRange[id];\n    }\n  }\n}, EXIT_CHECK_PERIOD);\n\nnoble.on('stateChange', function(state) {\n  if (state === 'poweredOn') {\n    noble.startScanning([], true);\n  } else {\n    noble.stopScanning();\n  }\n});\n","outputs":1,"noerr":0,"x":510,"y":680,"wires":[["3184b5bf.ce7b4a"]]},{"id":"3184b5bf.ce7b4a","type":"debug","z":"9703de.ff68fc2","name":"","active":true,"console":"false","complete":"payload","x":709,"y":680,"wires":[]},{"id":"813c8291.7ec38","type":"exec","z":"9703de.ff68fc2","command":"sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)","addpay":false,"append":"","useSpawn":"","name":"","x":684,"y":580,"wires":[[],[],[]]},{"id":"f1f8d3ce.0e073","type":"delay","z":"9703de.ff68fc2","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":337,"y":520,"wires":[["eb00084f.14fff8"]]},{"id":"e23458b3.1dcba8","type":"delay","z":"9703de.ff68fc2","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":337,"y":580,"wires":[["813c8291.7ec38"]]},{"id":"40e54401.bf1abc","type":"delay","z":"9703de.ff68fc2","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":337,"y":680,"wires":[["549836fc.ab67c8"]]}]




Marko S

unread,
Mar 17, 2016, 11:29:53 AM3/17/16
to Node-RED
So do you think all BLE devices would work using this approach?

Dne sreda, 16. marec 2016 19.00.22 UTC+1 je oseba Walter Kraembring napisala:

Walter Kraembring

unread,
Mar 18, 2016, 1:16:40 AM3/18/16
to Node-RED

If it works with all BLE devices? Well, I do not know but hopefully it will. Everything in the flow is depending on the installed libraries. The flow is of course experimental since I was interested to have access to BLE from code in a function node. So far it is just scanning for devices and reading information. Next interesting would be to see if I could write to a BLE device

Marko S

unread,
Mar 18, 2016, 7:22:03 AM3/18/16
to Node-RED
Interesting :). For example some cheap chinese "Magic Blue RGB bulbs" could be a good target to interact with.

Dne petek, 18. marec 2016 06.16.40 UTC+1 je oseba Walter Kraembring napisala:

Marko S

unread,
Jul 3, 2016, 5:34:06 PM7/3/16
to Node-RED
Walter, I have a follow up question, does this method of yours work for detecting Android phones - like Nexus 5?


Dne petek, 18. marec 2016 06.16.40 UTC+1 je oseba Walter Kraembring napisala:

Walter Kraembring

unread,
Jul 4, 2016, 1:57:29 AM7/4/16
to Node-RED
Hello Marko,

Regarding android phones, I do not really know, I have none myself and nobody else in the family either. However, earlier, before I added a filter to my flow, I did sometimes see entries in the log from BLE devices I did not know about, I assume those belonged to people passing by outside of our house

Reply all
Reply to author
Forward
0 new messages