Measuring proximity with bluetooth on raspberry Pi
Solution 1
Try this:
Run hcitool dev
to get the address of your bluetooth device.
In the script you linked to, change line 120 from:
dev_id = 0
to:
dev_id = bluez.hci_get_route(ADDRESS_FOR_YOUR_BLUETOOTH_DEVICE)
To measure proximity, the script calls the function
device_inquiry_with_with_rssi(sock)
which should print a list of bluetooth device ids and their corresponding RSSI values (see lines 95-102). Typically, devices must be in pairing mode to show up in the inquiry results. The function also returns the list of IDs/RSSIs as an array, so you can call it from your own code and process the returned results. The RSSI value indicates the signal strength of a device, and so is an indirect measure of proximity (see Finding distance from RSSI value of Bluetooth Low Energy enabled device ).
Solution 2
Depending on the device you want to use, Bluepy in Python might be a better method. I used a Pi3 to measure RSSI from Bluetooth modules (HM-10, CC254x-based devices) and was able to get reasonable estimates of distance. There's a ton of noise in RSSI, so expect inaccuracies of no less than 1m with some signal processing. I wrote a blog post on RSSI from HM-10 and Rpi, check it out for a more in-depth method of how I do it. I even included some Python code:
Daniel Nill
I like Python, Javascript, Go, Ruby and Beer. Contact at Daniel.L.Nill at gmail dot com
Updated on June 16, 2022Comments
-
Daniel Nill almost 2 years
I've been trying to use this script https://github.com/karulis/pybluez/blob/master/examples/advanced/inquiry-with-rssi.py but it seems that
sock = bluez.hci_open_dev(dev_id)
returns a non-working socket. Every time sock is passed into a functionerror(9, 'Bad file descriptor')
is thrown.This script is pretty old so there is a decent chance it doesn't work any more. So I have two questions. Does anyone know how to use the pybluez library (or a more modern equivalent) to measure proximity of a bluetooth device with a raspberry pi?
And what am I doing wrong with this script that is causing me to build a broken socket?
Thanks.
-
Daniel Nill about 10 yearsThis seems to me on the right track but NULL is not the null singleton in python and None doesn't work. The hci_get_route function wants a stringified address of your receiving bluetooth device. This can be obtained by running hcitool dev in your terminal
-
imjosh about 10 yearsSorry about that; I've revised the answer. Instead of NULL, -1
-
imjosh about 10 yearsI think dev_id = bluez.hci_get_route() may also work.