Giter VIP home page Giter VIP logo

bluetooth-proximity's Introduction

Bluetooth Proximity Detection

Python code for getting the RSSI value of a Bluetooth device by address. Based on the value returned, can determine the proximity of the device.

This code was adapted from this Github by Daniel Agar.

Requirements

This code requires the bluetooth and python-bluez modules to be installed. On Ubuntu/Debian systems, this can usually be done with the following commands:

sudo apt-get install bluetooth
sudo apt-get install python-bluez

Note: Your system must also have a Bluetooth adapter.

Installation

git clone https://github.com/ewenchou/bluetooth-proximity.git
cd bluetooth-proximity
sudo python setup.py install

Examples

test_address.py

This is a simple script to scan and output the RSSI value of a Bluetooth address in a loop. This can be used to test if the code is working on your setup and also to output the detected RSSI values as you move your Bluetooth device closer/further from the Bluetooth adapter.

Use Ctrl + C to exit the script or wait until number of loops has finished.

python test_address.py <bluetooth-address> [number-of-loops]

bluetooth_scanner.py

An example script that uses threads to scan for bluetooth addresses in a loop and invokes a callback function when the RSSI value is within a specified threshold.

Edit the script and set the BT_ADDR_LIST variable to the list of bluetooth addresses to scan.

Use Ctrl + C to exit the script.

python bluetooth_scanner.py

lnsm.py (Log Normal Shadowing Model)

An example script to scan & output the distance between two bluetooth devices using the RSSI value . The distance between the two devices is calculated using the Log-Normal Shadowing Model/Log-Distance Pathloss Model.

Use Ctrl + C to exit the script or wait until number of loops has finished. Recommended number of loops = 30

python lnsm.py <bluetooth-address> [number-of-loops]

Notes

  • The RSSI values returned will differ depending on your Bluetooth devices and your surroundings (e.g. are there walls/obstructions, multiple floors, etc).
  • Use the test script mentioned above to see what values are returned in your setup.
  • RSSI values may be positive or negative integers. You can use the absolute value (i.e. always positive) if you see both.

bluetooth-proximity's People

Contributors

aude avatar ewenchou avatar jonathanrjpereira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bluetooth-proximity's Issues

is it really working?

image

this is what I get with my phone BT disabled.

The very same thing I get with BT enabled.

Does that mean it's working?

I receive all rssi 0

Really thank you for your code. I run it, it didn't show wrong, but the result is strange, all rssi: 0. I use debug and think it cannot send command to request rssi. I don't know how to correct it. Please give me some advice.

Not working in python3

Traceback (most recent call last):
File "lnsm.py", line 1, in
from bt_proximity import BluetoothRSSI
File "/usr/local/lib/python3.5/dist-packages/bt_proximity-0.1-py3.5.egg/bt_proximity/init.py", line 1, in
from bt_rssi import BluetoothRSSI
ImportError: No module named 'bt_rssi'

Trying to figure out the RSSI value

I am stumbled by the RSSI value that I get from my Android phone... Phone is sitting right next to the scanner computer and it always returns 0. I move away about 10 feet and I still get a 0. What is the -10, 10 tuple supposed to represent? I was reading https://iotandelectronics.wordpress.com/2016/10/07/how-to-calculate-distance-from-the-rssi-value-of-the-ble-beacon/ and it gives the following as the equation to estimate distance

Distance = 10 ^ ((Measured Power – RSSI)/(10 * N))

Another question...

Thanks in advance for any help.

Question about bluetooth proximity.

Hi Ewen!

I have to ask through github inevitably because there is a limited number of questions to ask on Twitter.

The first thing I want to ask is about the query you said.
I want to putting a longer wait time between queries, but I do not know what the query is in the code. What is a query?

The second question is to implement code that finds a larger RSSI value.
I stored two Bluetooth addresses in BT_ADDR_LIST.
I want to compare the RSSI values ​​of two Bluetooth addresses and implement code that finds a larger value depending on the situation. But I could not implement it.

At first, I tried to implement the method 'bluetooth_listen ()' by dividing b, which is a variable, into two variables b and c, and storing each address separately.
Ex)
B = BluetoothRSSI (addr = first Bluetooth address)
C = BluetoothRSSI (addr = second Bluetooth address)
But I failed.

In the next method I tried creating two threads. However, I do not know how to call the RSSI value of each thread from the main function.
Ex)
Th1 = start_thread (addr = first Bluetooth address, callback = dummy_callback)
Th2 = start_thread (addr = second Bluetooth address, callback = dummy_callback)
This also failed.

So I try to get some advice.
I do not use Alexa, but I want to use your source for a similar purpose.
Can I see what I want?

causes my raspberry pi to crash

Running the scanner causes system to become unstable.

---
addr: 06:AE:D6:31:A0:12, rssi: None
---
addr: 40:4E:31:42:F0:CB, rssi: None
Write failed: Connection reset by peer
shadycuz@La-Tower:/mnt/c/linux/repos/shadycuz$ ssh [email protected]
^C
shadycuz@La-Tower:/mnt/c/linux/repos/shadycuz$ ping raspberrypi.la-tech.home
PING raspberrypi.la-tech.home (10.20.30.124) 56(84) bytes of data.
64 bytes from raspberrypi.la-tech.home (10.20.30.124): icmp_seq=5 ttl=64 time=3409 ms
64 bytes from raspberrypi.la-tech.home (10.20.30.124): icmp_seq=8 ttl=64 time=2392 ms
64 bytes from raspberrypi.la-tech.home (10.20.30.124): icmp_seq=10 ttl=64 time=2603 ms

Then I have to reset the power. Works fine if only one address in the list, but two causes it to crash.

Adding my bluetooth address?

When I edit my Bluetooth Address into the BT_ADDR_LIST =
I get a syntax error? Should it be in a different format than the Bluetooth address?

variation between 0 and -1

I'm trying to make your script to work, and I encounter some weird issues :
I want to detect distance to my phone from py PC.
I have internal BT and dongle BT,
With the internal BT, the values go from 0 to pretty decent value as I take away the phone from the PC.
With the dongle BT, the values only go from 0 to -1.0 !
And it is quite weird as it is really when i take the phone away, I'll get -1.0.

Do you have any idea ?
And even with the internal BT I get only whole numbers, not float.

Thanks

Test_add: No.of Request Limit

Whenever I execute the test_address.py script with a specified number of requests/loops, I get the RSSI values.
But I have obeserved that if the number of requests/loops is greater than 32, the values after the 32nd loop are always zero. I was wondering if there is some sort of timeout associated with this.

bluetooth_scanner.py returning rssi of 0 after around 15 iterations

First off, great code, works like a charm! I have it sending the RSSI of certain bluetooth via API, but when I run the bluetooth_scanner.py code, it polls correctly for only around 15 iterations, anything after that always returns 0.

Has anyone experienced anything like this? I am not seeing any errors. Appreciate any help you can provide.

PS - This is running on a raspberry pi.

JJ

Improvement: check against trend rather than discrete values

I've just discovered this project and have set it up to explore it - exactly the use case I'm trying to figure out. So thank you for this!

I find that my RSSI value fluctuates too much even when sitting at my desk within 0.5m of the Bluetooth receiver. I'm using my Samsung watch to track proximity. I find that the value jumps around between 0 and 15 quite a lot. So I set my trigger value to ~12, but it still results in false triggers, which are very impactful. Locking the screen accidentally, or pausing music/unpausing based on a single discrete measurement seems too fragile.

I was reading the Bluetooth doc about proximity and they suggest to "Use the Mode of a Range When Getting the RSSI Value" which makes a lot of sense, so those 1-off spikes can be ignored.

I was going to look at the script to think about how to track the last 5 readings and ignore high/low values against the mode but wonder if anyone else has already thought of this, or has a simple way to configure the script to avoid this problem.

Thanks again for your work, I'm not a script expert, more a dabbler, but I think this would be a great improvement to this script.

Getting AttributeError in bluetooth-scanner example

When running the bluetooth-scanner example on a raspberry pi with python3 (https://github.com/Adversitx/bluetooth-proximity actually, but in this case there should be no difference I think) I get the following error:

Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "bluetooth_scanner.py", line 47, in bluetooth_listen rssi = b.request_rssi() File "/usr/local/lib/python3.7/dist-packages/bt_proximity-0.2-py3.7.egg/bt_proximity/bt_rssi.py", line 60, in request_rssi rssi = struct.unpack('b', rssi[3].to_bytes(1, 'big')) AttributeError: 'str' object has no attribute 'to_bytes'

Why is this? I have not changed a thing in the code…
Also, if I bypass this just to see that the rest works by encoding it with latin1 instead of using to_bytes I get a runtime error after a few seconds:

line 63, in request_rssi rssi = bt.hci_send_req(self.hci_sock, bt.OGF_STATUS_PARAM, bt.OCF_READ_RSSI, bt.EVT_CMD_COMPLETE, 4, self.cmd_pkt) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 3: invalid start byte

Set request_rssi Timeout

I'm writing a script that scans for a list of bluetooth addresses periodically, but I've noticed that if a device is not in range it takes a full five seconds to timeout .request_rssi(), is there a way to change how long this timeout is?

Bluetooth Low Energy device

Hello,

I have a problem when I try to use the program 'bluetooth_scanner.py' into a Bluetooth Low Energy device. The fact is that rssi value is always 'none' and I know that is not 'none'. Do you have a solution about that?

Thanks.

detection glitch?

I'm running the test_address.py code with 999 cycles to detect my iPhone X.

It all runs fine until I turn off bluetooth on my phone, in which case the RSSI value becomes 0(I've seen other issues where it's supposed to be "None"?).

And even after I turn bluetooth back on, the value still stuck at 0 until I restart the script.

I also tried to turn off my phone and ran the script again, and i got "None", which is normal. But after I turn on my phone, it still repeats itself as "None" without giving the correct value. I'd assume something, like cache, stuck in there without being refreshed or something? Can this be fixed?

Not working on python 3

Can you help me getting this to work under python 3 ?

reqstr = struct.pack("6sB17s", bt.str2ba(self.addr), bt.ACL_LINK, "\0" * 17)
request = array.array("c", reqstr)

This is not working in python 3.

problems with close range

Hey im having a problem while testing test_address.py where i get 0 about 95% of the time in my room unless im moving the device(samsung A5). if i move the device around it moves around values between 1 and 7 and sometimes jumps to around 12, but as soon as it stops it goes back to 0.(sometimes i still get 0 while moving it)

ill take it out of the room and it will go to around 25 and then by the time i get back to my room i get 0s for the rest of the time (might have something to do with the request max issue the other user was having)

There is a wireless device somewhere in my apartment building (maybe speakers?) that i connected to just out of curiosity and it hovers at about 19-24 consistently and never drops to 0.

in your experience has this worked nicely in short ranges? also do you think this is a sufficient solution for long periods of time. (polling every second for hours )

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.