Bluetooth distance sensing - phone etc without pairing


41 posts   Page 1 of 2   1, 2
by mikerr » Tue Jun 18, 2013 3:10 pm
Use your phone's built in bluetooth as a proximity sensor:

All bluetooth devices have a signal strength indicator (RSSI) which you can use to tell the (very rough) distance from the raspberry pi.

Note: works with any BT device including BT headsets/mice
- e.g. use an old small BT headset as a tracker since it's self contained with a battery.


This is widely dependent on your local conditions, and rf interference etc,
so while you won't be able to measure distance hugely accurately, it is enough to reliably distinguish whether the phone/device is:

    1. present - in the house
    1. in the same room, or
    2. close to the bluetooth dongle (a few inches)

but not much more than that - still very useful information - and this is available WITHOUT PAIRING,


E.g. signal strength values on my setup with a cheap bluetooth dongle

    38 - Phone touching bluetooth dongle

    25 - Phone an inch away

    10 - 6 inches away

    0 - anything from 2 feet to opposite side of room

    -3 - in next room (wall between)

    -10 - downstairs/ 2 rooms/2 walls away


INSTRUCTIONS:

1.Get the bluetooth stack on your Pi:
Code: Select all
sudo apt-get install --no-install-recommends bluetooth


2. You need to know the device's unique bluetooth address (BDADDR / MAC) - to get it directly the device needs to be discoverable, just for this stage.

Set the phone to be discoverable, then:
Code: Select all
hcitool scan

That returns something like: 12:34:56:78:90:00 GT-N7100

3. Connect to phone:

Code: Select all
sudo rfcomm connect 0 12:34:56:78:90:00 10 >/dev/null &


4. check status level
Code: Select all
hcitool rssi 12:34:56:78:90:00


or check it every 1/2 second on screen, so you can move the phone round (or leave it in another room) and watch the number change:

Code: Select all
 watch -n 0.5 hcitool rssi 12:34:56:78:90:00


I'll maybe do a python script tomorrow !
Android app - Raspi Card Imager - download and image SD cards - No PC required !
User avatar
Posts: 2420
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
by stevenmcastano » Thu Sep 05, 2013 9:11 am
This actually looks GREAT. I've been working on this myself for a while now with a package called "blueproximity" but it looks like it's not every well maintained right now...... I've got most of what I need up and running and it seems to be ok... what I'd really like to know is what bluetooth dongle you're using???

I've got an old RocketFish one here and I never get an RSSI value over 1 or 2.... I seem to get -5 to -15 from across the room... -25 if I sit on my iPhone 5 halfway across the apartment.... but even with the phone laying on the table next to my RasPi... I'm only getting 0... once in a while it bounces up to a 2!

Also... in your instructions you show to connect via rfcomm... the test RSSI. My question is... if you disable bluetooth on your phone (should be the same as walking far enough away that it can't connect anymore)... then enable it again, does it reconnect? If not... show are you making it reconnect when the device comes in range again?
Posts: 2
Joined: Thu Sep 05, 2013 9:06 am
by mikerr » Thu Sep 05, 2013 11:52 am
stevenmcastano wrote:Also... in your instructions you show to connect via rfcomm... the test RSSI. My question is... if you disable bluetooth on your phone (should be the same as walking far enough away that it can't connect anymore)... then enable it again, does it reconnect? If not... show are you making it reconnect when the device comes in range again?

Yes, the rfcomm in step 3 reconnects.

Only step 2 (scan) needs to be done when its discoverable - but once you know the MAC address, you don't need to do that ever again.
User avatar
Posts: 2420
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
by stevenmcastano » Sat Sep 07, 2013 6:53 pm
Thanks again... this was a really good start. What I found though, is that when your device goes out of range and drops the "rfcomm" connection, when it comes BACK in range again... the connection does not auto start again.

So what I needed to do is the following:

1) When the script first runs, attempt to connect to the bluetooth device:

Code: Select all
rfcomm connect 0 12:34:56:78:90:00 10 >/dev/null &


2) After a connection attempt, check the rssi level with the following:

Code: Select all
hcitool rssi 12:34:56:78:90:00


If you get a valid response, then the device is connected and you mark a variable to keep track of your connection state so you don't attempt to connect over and over again:

Code: Select all
btconnected=1


3) When you finally get an "error" response from checking rssi levels, mark the device "NOT connected" and attempt to connect again.

Here is the code I've got written so far... it's sloppy and I'd invite anyone out there to clean it up and repost!
(this script is built to send me notification via Pushover.net when the device is in range vs. out of range, as well as update my home automation system)

Code: Select all
#Global VARS:
device="XX:YY:ZZ:11:22:33"
btconnected=0
btcurrent=-1
counter=0
notconnected="0"
connected="1"
rssi=-1

#Command loop:
while [ 1 ]; do
cmdout=$(hcitool rssi $device)
btcurrent=$(echo $cmdout | grep -c "RSSI return value") 2> /dev/null
rssi=$(echo $cmdout | sed -e 's/RSSI return value: //g')

if [ $btcurrent = $notconnected ]; then
        echo "Attempting connection..."
        rfcomm connect 0 $device 1 2> /dev/null >/dev/null &
        sleep 1
fi

if [ $btcurrent = $connected ]; then
        echo "Device connected. RSSI: "$rssi
fi

if [ $btconnected -ne $btcurrent ]; then
        if [ $btcurrent -eq 0 ]; then
                echo "GONE!"
        fi
        if [ $btcurrent -eq 1 ]; then
                echo "HERE!"
        fi
        btconnected=$btcurrent
fi

sleep 1

done


It works like a gem so far... it connects and STAYS connected, connects in anywhere from 1 to 3 seconds when the device is finally back in range and reports the RSSI once a second. It's been running for a few days now and I've only had it "disconnect" and miss an RSSI while I was still home twice and the disconnect lasted for under 5 seconds.

My next plan will be to take the RSSI numbers, keep a running average of the last 30 seconds or so and use that to report the "approximate proximity" to the device. From there I will hopefully find a way to run updates from multiple bluetooth connections to the same device (at least 3) from either different bluetooth dongles, or most likely, multiple raspberry pi's around the house so I can "triangulate" where each device is in the house.

My goal is to give the ability to use "indoor location aware" type commands to my home automation system. Like, when using my XBMC remote app on our iPhones... rather than have to hit the settings button and select which XBMC (also running on Raspberry Pi) to connect to, I'll have a single DNS name called "xbmccontrol.myhouse.com" and when the proximity system thinks you've entered a particular room it will remap that DNS entry over to the IP of the XBMC you're near.

The same thing goes for lighting control using Siri-proxy... when you pick up your phone and say "turn the lights on" it will already know where you are, possibly even ASK you when it thinks you're between rooms and help update the control IP.
Posts: 2
Joined: Thu Sep 05, 2013 9:06 am
by FozzyFoster » Tue Oct 01, 2013 2:50 pm
Hi all,

I am interested in doing the same thing but with a Bluetooth LE device. It is probably quite simple, but I cannot seem to get the rssi from a few Bluetooth LE devices.

Thanks in advance!
Posts: 2
Joined: Tue Oct 01, 2013 2:47 pm
by delante_v » Fri Oct 04, 2013 7:46 pm
Thank you mikerr for posting this valuable information!
I have a project going on, and I too am working on a device detection via bluetooth (or WiFi).

I just have a question regarding about device reconnection using the command in "Step 3" with
Code: Select all
sudo rfcomm connect 0 [MAC address] 10 >/dev/null &


Is it possible to have the device reconnect automatically, or the rfcomm command must be used every time?
Posts: 4
Joined: Fri Oct 04, 2013 6:36 pm
Location: British Columbia, Canada
by HarmlessSaucer » Sat Jan 18, 2014 1:00 am
Been having a go at this (Awesome idea btw!)
And I'm having a bit of an issue connecting to the device as you suggested in this step:

Code: Select all
sudo rfcomm connect 0 12:34:56:78:90:00 10 >/dev/null &


I get the error:
Code: Select all
Can't connect RFCOMM socket: Connection refused


If I watch the "Bluetooth Devices" window in X when I run this, I can see it connect briefly then drop.

This happens on both my iPhone and a Pebble smart watch I tried.

Is this something you've run into?
Posts: 1
Joined: Sat Jan 18, 2014 12:39 am
by camillo777 » Wed Feb 12, 2014 3:46 pm
Hi! What USB dongle are You using?
Are You using an USB hub or straight into the Pi?
Do You know if exists a Bluetooth USB Dongle with the new BLE stack and the old Bluetooth compatibility as well?
Thank you!

Best regards,
Camillo

mikerr wrote:Use your phone's built in bluetooth as a proximity sensor:

All bluetooth devices have a signal strength indicator (RSSI) which you can use to tell the (very rough) distance from the raspberry pi.

Note: works with any BT device including BT headsets/mice
- e.g. use an old small BT headset as a tracker since it's self contained with a battery.


This is widely dependent on your local conditions, and rf interference etc,
so while you won't be able to measure distance hugely accurately, it is enough to reliably distinguish whether the phone/device is:

    1. present - in the house
    1. in the same room, or
    2. close to the bluetooth dongle (a few inches)

but not much more than that - still very useful information - and this is available WITHOUT PAIRING,


E.g. signal strength values on my setup with a cheap bluetooth dongle

    38 - Phone touching bluetooth dongle

    25 - Phone an inch away

    10 - 6 inches away

    0 - anything from 2 feet to opposite side of room

    -3 - in next room (wall between)

    -10 - downstairs/ 2 rooms/2 walls away


INSTRUCTIONS:

1.Get the bluetooth stack on your Pi:
Code: Select all
sudo apt-get install --no-install-recommends bluetooth


2. You need to know the device's unique bluetooth address (BDADDR / MAC) - to get it directly the device needs to be discoverable, just for this stage.

Set the phone to be discoverable, then:
Code: Select all
hcitool scan

That returns something like: 12:34:56:78:90:00 GT-N7100

3. Connect to phone:

Code: Select all
sudo rfcomm connect 0 12:34:56:78:90:00 10 >/dev/null &


4. check status level
Code: Select all
hcitool rssi 12:34:56:78:90:00


or check it every 1/2 second on screen, so you can move the phone round (or leave it in another room) and watch the number change:

Code: Select all
 watch -n 0.5 hcitool rssi 12:34:56:78:90:00


I'll maybe do a python script tomorrow !
Posts: 26
Joined: Tue Feb 04, 2014 1:21 pm
by mikerr » Wed Feb 12, 2014 5:13 pm
User avatar
Posts: 2420
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
by camillo777 » Wed Feb 12, 2014 5:20 pm
mikerr wrote:I was using a £1 dongle like this:

http://www.amazon.co.uk/TINY-WIRELESS-B ... B0013BFQUE


Thank you!
Directly connected to the RPi or via a powered hub?
I guess You cannot discover devices which have BLE v4 (like iPhone or iPad)??
Posts: 26
Joined: Tue Feb 04, 2014 1:21 pm
by calvinkehl » Wed Jul 09, 2014 1:34 pm
Hi,
I wonder if it is possible to send the RSSI data dierctly via Lan to a PC or Server (another RasPi) where you can work with it. For example to find out in which room the person is if there are more than one "Bluetooth-Points".

Greets
Posts: 1
Joined: Wed Jul 09, 2014 8:03 am
Location: Germany
by Rugbywarrio89 » Tue Feb 24, 2015 4:44 pm
I know I'm rehashing an old thread but the author hinted at writing a python code to find RSSI and that is exactly what I need.

I am trying to write an IF loop that will output a GPIO based on RSSI. Basically, IF RSSI < ??, then GPIO.output(X,GPIO.HIGH).
Engineers aren't boring people, we just get excited over boring things! - Anonymous
Posts: 38
Joined: Wed Nov 12, 2014 9:49 pm
Location: NE Ohio
by Douglas6 » Tue Feb 24, 2015 6:02 pm
I'm not with my Pi, or I'd whip something up, but I'd start with
Code: Select all
import subprocess
bdaddr = "AA:BB:CC:DD:EE:FF"
result = subprocess.check_output(["hcitool", "rssi", bdaddr])
User avatar
Posts: 3866
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by blakeman399 » Wed May 20, 2015 2:53 pm
I created a Bluetooth Proximity Light!! Check it out and subscribe for a tutorial! https://www.youtube.com/watch?v=x51BgeOjYxo
Posts: 1
Joined: Wed May 20, 2015 2:51 pm
by Douglas6 » Wed May 20, 2015 11:33 pm
blakeman399 wrote:subscribe for a tutorial!
Pass
User avatar
Posts: 3866
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by dq11 » Mon Jun 01, 2015 7:23 am
blakeman399 wrote:I created a Bluetooth Proximity Light!! Check it out and subscribe for a tutorial! https://www.youtube.com/watch?v=x51BgeOjYxo


I subscribed but see no tutorial for what you did. Thanks in advance for sharing the tutorial so I can replicate your setup.
Posts: 4
Joined: Mon Jun 01, 2015 7:12 am
by LennySh » Wed Feb 10, 2016 1:44 pm
For those of you that missed his video tutorial, here's the link: https://www.youtube.com/watch?v=stBKy-1gbA4

The video above has a link also to his GitHub Repo here: https://github.com/blakeman399/Bluetooth-Proximity-Light/blob/master/https/github.com/blakeman399/Bluetooth-Proximity-Light.py
Posts: 2
Joined: Wed Feb 10, 2016 1:39 pm
by shuckle » Fri Mar 11, 2016 11:03 am
This does not seem to be working with the internal bluetooth of rpi3?
I only get
RSSI return value: 0

Any ideas how to get signal strength to the connected devide using rpi3 internal bluetooth?
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland
by Douglas6 » Fri Mar 11, 2016 9:03 pm
It's not working as advertised for me either. After the rfcomm connect command, the phone prompts for a pairing PIN. While waiting, the hcitool rssi command returns valid values. This only lasts for a few seconds however. After that, hcitool rssi responds with 'not connected'.

Not sure if it's down to a difference in Android, BlueZ, or the adapter.
User avatar
Posts: 3866
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by blaablaaguy » Fri Mar 18, 2016 2:28 pm
I stuck my bluetooth address in the variable "bdaddr" and tried to print out the output of "result". Like this:

Code: Select all
import subprocess
bdaddr = "AA:BB:CC:DD:EE:FF"
result = subprocess.check_output(["hcitool", "rssi", bdaddr])

while True:
    print result


However when i run the code i get this error:

Traceback (most recent call last):
File "/home/pi/ProximityBluetooth.py", line 3, in <module>
result = subprocess.check_output(["hcitool", "rssi", bdaddr])
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['hcitool', 'rssi', 'BC:B3:08:02:FB:7D']' returned non-zero exit status 1


Can anyone help me to get the code working?
This signature intentionally left blank.
Posts: 618
Joined: Sun Sep 27, 2015 3:26 pm
by DirkS » Fri Mar 18, 2016 2:50 pm
blaablaaguy wrote:Traceback (most recent call last):
File "/home/pi/ProximityBluetooth.py", line 3, in <module>
result = subprocess.check_output(["hcitool", "rssi", bdaddr])
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['hcitool', 'rssi', 'BC:B3:08:02:FB:7D']' returned non-zero exit status 1


Can anyone help me to get the code working?

What is the output if you try the command on the command line?
Code: Select all
hcitool rssi BC:B3:08:02:FB:7D
Posts: 6936
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK
by Douglas6 » Fri Mar 18, 2016 3:02 pm
'device is not connected', I suspect.
User avatar
Posts: 3866
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by blaablaaguy » Fri Mar 18, 2016 3:53 pm
Yeah, it gives "Not connected."
This signature intentionally left blank.
Posts: 618
Joined: Sun Sep 27, 2015 3:26 pm
by blaablaaguy » Fri Mar 18, 2016 4:10 pm
okay, so i realized i missed a step and changed my code to this:

Code: Select all
import subprocess
bdaddr = "BC:B3:08:02:FB:7D"
result = subprocess.check_output(["hcitool", "rssi", bdaddr])

subprocess.call(["sudo rfcomm connect 0 BC:B3:08:02:FB:7D 10 >/dev/null &"])

while True:
    print result


but still get the same error

EDIT: when i run in the command line I get

Code: Select all
[1] 6241
pi@raspberrypi:~ $ Can't connect RFCOMM socket: Connection refused


The numbers "6241" change when i retry the command
Last edited by blaablaaguy on Fri Mar 18, 2016 4:18 pm, edited 2 times in total.
This signature intentionally left blank.
Posts: 618
Joined: Sun Sep 27, 2015 3:26 pm
by DirkS » Fri Mar 18, 2016 4:14 pm
Again: try it on the command line first.
You will probably see that the connect failed...
Posts: 6936
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK