Issue
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.
Solution
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 ).
Answered By - imjosh