Bluetooth setup / (iBeacon) presence detection

Hi,

I want to use bluetooth dongle to detect presence of some devices (iBeacons, mobiles etc.).Have installed the bluetooth addon in openHAB but gettings errors that it is not able to initialize the bluetooth device.

Setup:

  • HW: Raspberry Pi 2 model B
  • OS: Raspian
  • OpenHAB 1.7.1
  • Bluetooth 4.0 (BLE) dongle

I read that Bluecove needs to be installed as well, however have not succeeded installing this on the Raspberry.
Does anybody have tips on how to get this installed / working?

Thanks,
Jeroen

1 Like

I tried for about a month to try and get the bluetooth binding to work in openHAB and eventually gave up. But I did find this MakeUsOf.com article that I adapted. Follow the instructions to get everything you need installed.

Using the example Python scripts on the article I modified a Python script I already wrote to report the status of GPIO pins on the Raspberry Pi over MQTT to query for the presence of a device with the given Bluetooth address and report whether it is there or not. If you know the address you can see if that device is nearby even without pairing it with your computer. The article has a script to discover your device’s addresses.

I will eventually update the script on gitHub so the same script works for both. I also have some changes so it reports a Last Will and Testament (LWT) message so I can tell from openHAB when the scripts go down but not yet posted those updates. But if you want to see the changes for the bluetooth and let me know and I’ll post it somewhere you can get to it or update the gitHub.

The range isn’t all that great but I happen to have my server on the top floor, the garage controller on the main floor and the alarm controller in the basement so was able to put a dongle on each and have all three of them reporting BT presence. If any one of them report the presence of the device I count that person as present. It works pretty well.

Hi Rich, thanks for your answer and suggestions. Maybe I will use the scripts you suggested later on but first I am trying to get it working within OH. Therefore I have been playing a bit with the openHab bluetooth binding in order to get the correct BlueCove jar for Raspberry. Now I have generated the correct JARs and based on that I exported (eclipse) a new bluetooth binding/addon - this is working and bluetooth devices are correct recognized what they are in discovery mode (once I am done I will summarize all steps and place it here).

However I want to find a way in which openHAB actually pings the devices (those which are specified in Items) to detect their presence. Not sure yet how this should work. Also a challenge is that an iBeacon is not detected by openHAB.

According to the wiki page you should only need to define a switch and it will be on when a device with that address is in range and off if it can’t detect the device within range. Are you looking to manually trigger a ping?

Or are you wanting some sort of way to check the BT presence in one of your rules? If this is the case all you need to do is check the current state of your item. You can also trigger a rule based on the switch changing from OFF to ON or ON to OFF.

I guess I don’t understand what you are trying to do.

In the setup I have defined several switches - e.g.

Switch MyMobiel "Mobile " {bluetooth=“0FEEBA55F1BA”}

This BT switch is “seen” by openhab when the mobile actually starts scanning for other BT devices. This means that when my mobile has activated bluetooth but is not scanning it will not be recognized by openhab. This is what I am currently experiencing.

In the above switch definition it is configured to switch for an unpaired BT device (have not used “!” - see BT binding). I expected that openhab would recognize a bt device even when the device (e.g. a mobile) is not actively scanning.

Interesting. I noticed a similar issue the first time I attempted to get some sort of BT presence detection working. It ended up being pretty useless to me because of that. Even when I paired my phone with the computer it would not keep connected and I had to go back into pairing mode to reconnect.

However, I discovered when I tried again and using the scripts I linked to that those scripts can see the devices even if they are not in pairing mode. You do have to put them into pairing mode to run the script that discovers the address but once you have the address the other script will detect it even if the phone is not in pairing mode and even if the phone was never paired with the computer, which I thought was kind of cool.

So your script could do the trick and enables you to detect presence. However would it not be easier “just” to configure the bluetooth switch (incl mac address) in items and that openhab detects presence of the unpaired devices? When I read wiki I believe that was the intention of this binding …

Will investigate a bit more and keep you posted

The problem I had was that since I was on Linux I had to recompile the jar file and I never could get BLueCove to build properly for me and I decided I had better things to do with my time than fighting Maven builds.

A more important problem for me is that I have a three story house (counting the basement) and BT is not strong enough to reach the entire house so I need remote sensors anyway. So reporting the BT detections over MQTT makes perfect sense for me. If it could reach the entire house from one location I would probably use the Exec binding instead but I I don’t see myself going back to fight BlueCove.

Finally these Python scripts I’m using are the only ways I’ve determined this far that reliably detects both my Android and my wife’s iPhone even with they are not in pairing mode.

I just posted my full config for this in another thread,

Maybe I will use your scripts as well (just want to try a bit more :-)). I guess some other OH users have been struggling with the same topic (?)

PS: Interesting upcoming product is the crownstone (dobots) - they provide bluetooth outlets which are fully meshed (all outlets connect to each other to form a fully meshed network) and accessible by apps as well. So you should be able to cover your house easible with BT coverage

On Windows or OSX I think the bluetooth binding works pretty well. It is only on Linux that I’ve seen people have trouble. Unfortunately I think Linux is the most common platform. Thus the number of articles and postings for ways to achieve the same things using the surprisingly primitive support of bluetooth on Linux.

Only if I can get those devices to report when they sense one of the mobile BT devices (e.g. smartphone) nearby. Usually devices like that are limited to providing their own functionality (e.g. I can’t get any information out of my bluetooth speakers) but that would be cool if they provide that extra flexibility.

I’ve already invested in z-wave so I’ll have to watch from afar but it is an intriguing development.

I have seen one presentation of them and they seem fully flex (incl. API, no restrictions etc). When they are available I will buy a few.

Interesting that bluetooth is not that enhanced on Linux (especially because the industry seems pretty keen on BLE).

Presence for BT devices is working (on Raspberry PI):

  • Compiled (via eclipse) a new bluetooth addon JAR (incl. Bluecove)
  • Use exec to call (in items) a simple external script to actually do the monitoring of known BT devices (via l2ping). Result is returned to OH.

The existing OH commands are (imho) not able to scan unpaired or BT devices which are not scanning themselves.

Now trying to include BLE beacon scanning / have not found a solution. hcitool lescan could be part of solution - anybody has an idea?

You can run hcitool lescan using the Exec binding. That was my first approach after failing to build Bluecove on Ubuntu. But I don’t think that worked for me for BT devices which weren’t paired and actively scanning either. Pybluez was the first instance where I was able to find a solution that could detect unpaired and unscanning devices which is why I wrote the script.

Afaik the hcitool lescan is meant for beacons (BLE). Issue with this command is that it does not halt and continues to run.

The one that is used in the script works fine l2ping -c 1 <BTaddress>

And the hcitool scan only returns the devices which are actively scanning for other BT devices (nice but not useful for me).

1 Like

Sorry to resurrect an old thread but I successfully used this shell script https://gist.github.com/richbeales/d7177734e1804a56a72a to compile bluecove for bluetooth support on linux. Hopefully it’ll help someone. It’ll work if you can type ‘jar’ at the command line, if not you’ll need to find out where java is installed

Thanks Rich.

I did already compile bluecove successfully a while ago but your script is always good to have.
As stated earlier I have not found a way to scan BLE beacons with OH. So for BLE beacons and BT devices I am using some scripting. This is not the ideal solution but it works fine.

Hi Jeroen,

How have you done this?

Shame no BT support.

Hi Scott,

I defined BT / iBeacon Items (for presence detection). And the Items run the specific scripts.

Switch BT_JvB “Jeroen’s mobile” (Btpresence) {exec="<[/scripts/scan_btdev.sh 1:30000:REGEX((.?))]"}
Switch BT_BMW “Jeroen’s car " (Btpresence) {exec=”<[/scripts/scan_ibeacon3.sh 1:20000:REGEX((.
?))]"}

Inside the scripts the Linux tools (hcidump / tool …) is used. Script below FYI (quickly put it together it for own use)

!/bin/bash
iBeacon Scan / Jeroen / Nov 2015
$1 = parse -> parse is used by the script itself / normally not by the end-user
$2 = -b -> output is raw, no explanation
$2 = -uuid -> BT address expexted in $3
$3 = (when -uuid is used)

List of iBeacons which you want to scan for

case $1 in
1) UUID=“E2C56DB5-DFFB-48D2-B060-D0F5A71096E0”;;
2) UUID=“FDA50693-A4E2-4FB1-AFCF-C6EB07647825”;;
esac

output=$( sudo hcidump --raw | /scripts/scan_ibeacon.sh parse -uuid $UUID -t 6 )
if [[ $output == “ON” ]]; then
echo “ON”
else
echo “OFF”
fi

The script which is used inside (scan_ibeacon.sh) was developed by Radius Networks. Changed it a bit for usage at home (when you want the original just google)

if [ ! “$(pidof hcitool)” ]; then
sudo hcitool lescan --duplicates 1>/dev/null &
fi

if [[ $1 == “parse” ]]; then
packet=""
capturing=""
count=0
now=date +%s

if [[ $4 == “-t” ]]; then
finish=$((now+$5))
else
finish=$((now+300))
fi

while read line && [[ “$now” -lt “$finish” ]]
do
count=$[count + 1]
now=date +%s
if [ “$capturing” ]; then
if [[ $line =~ ^[0-9a-fA-F]{2}\ [0-9a-fA-F] ]]; then
packet="$packet $line"
else
if [[ $packet =~ ^04\ 3E\ 2A\ 02\ 01\ .{26}\ 02\ 01\ .{14}\ 02\ 15 ]]; then
UUID=echo $packet | sed 's/^.\{69\}\(.\{47\}\).*$/\1/'
MAJOR=echo $packet | sed 's/^.\{117\}\(.\{5\}\).*$/\1/'
MINOR=echo $packet | sed 's/^.\{123\}\(.\{5\}\).*$/\1/'
POWER=echo $packet | sed 's/^.\{129\}\(.\{2\}\).*$/\1/'
UUID=echo $UUID | sed -e 's/\ //g' -e 's/^\(.\{8\}\)\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)\(.\{12\}\)$/\1-\2-\3-\4-\5/'
MAJOR=echo $MAJOR | sed 's/\ //g'
MAJOR=echo "ibase=16; $MAJOR" | bc
MINOR=echo $MINOR | sed 's/\ //g'
MINOR=echo "ibase=16; $MINOR" | bc
POWER=echo "ibase=16; $POWER" | bc
POWER=$[POWER - 256]
RSSI=echo $packet | sed 's/^.\{132\}\(.\{2\}\).*$/\1/'
RSSI=echo "ibase=16; $RSSI" | bc
RSSI=$[RSSI - 256]

      if [[ $2 == "-b" ]]; then
    echo "$UUID $MAJOR $MINOR $POWER $RSSI"

  elif [[ $2 == "-uuid" ]]; then
    if [[ $UUID == $3 ]]; then
      echo "ON"
      break;
    fi
      fi
    fi
    capturing=""
    packet=""
  fi
fi
if [ ! "$capturing" ]; then
  if [[ $line =~ ^\> ]]; then
    packet=`echo $line | sed 's/^>.\(.*$\)/\1/'`
    capturing=1
  fi
fi

done

Below a script for Bluetooth

!/bin/bash
Script to ping BT addresses on layer 2
List of known MAC addresses are provided
The device does NOT necessarily need to be scanning for other devices as this is a layer 2 ping.
Layer 2 ping does not work for (i)Beacon) - use “hcitool lescan” instead

case $1 in
1) MAC=“00:EE:BD:55:F1:BA”;;
2) MAC=“00:0E:9F:F2:D0:67”;;
3) MAC=“74:E1:B6:8D:FE:81”;;
esac

if l2ping -c 1 $MAC &> /dev/null
then
echo “ON”
else
echo “OFF”
fi

Hope it helps :smile:

Chrs - Jeroen

1 Like

Hello Rich,

This is perfect, I installed it on my system and working well! Thanks