5

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 function error(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.

Alex Lamson
  • 479
  • 5
  • 14
Daniel Nill
  • 5,539
  • 10
  • 45
  • 63

2 Answers2

3

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 ).

Community
  • 1
  • 1
imjosh
  • 4,734
  • 1
  • 19
  • 22
  • This 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 – Daniel Nill Mar 24 '14 at 00:41
  • Sorry about that; I've revised the answer. Instead of NULL, -1 – imjosh Mar 24 '14 at 14:49
  • I think dev_id = bluez.hci_get_route() may also work. – imjosh Mar 24 '14 at 14:55
1

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:

https://engineersportal.com/blog/2017/12/31/using-raspberry-pi-hm-10-and-bluepy-to-develop-an-ibeacon-mesh-network-part-1