User avatar
lp7
Posts: 6
Joined: Thu Jun 22, 2017 2:18 pm
Location: UK

Python Script to Save list of Bluetooth address detected

Mon Jun 26, 2017 1:04 pm

Hello,

I would like to save the Bluetooth Addresses of all the Bluetooth Devices detected by my RPi 3. I am not that familiar with Python, could you please suggest a way to keep a list of the addresses as the devices are detected.

1. Detect Bluetooth Addresses
2. Script appends every address detected in a document and saved on SD Card.

Regards,
LP

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python Script to Save list of Bluetooth address detected

Mon Jun 26, 2017 4:43 pm

Something like this should do. You may have to install pexpect:

Code: Select all

sudo apt-get install python-pexpect
Then run this:

Code: Select all

import pexpect

results = open("scan_results.txt", 'w')
child = pexpect.spawn("bluetoothctl")
child.send("scan on\n")
bdaddrs = []

try:
    while True:
        child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))")
        bdaddr = child.match.group(1)
        if bdaddr not in bdaddrs:
            bdaddrs.append(bdaddr)
            results.write(bdaddr+"\n")
except KeyboardInterrupt:
    child.close()
    results.close()

ericcooper
Posts: 140
Joined: Sat Apr 08, 2017 6:23 pm

Re: Python Script to Save list of Bluetooth address detected

Mon Jun 26, 2017 5:12 pm

Or just use bt-device --list from the bluez-tools package.

User avatar
lp7
Posts: 6
Joined: Thu Jun 22, 2017 2:18 pm
Location: UK

Re: Python Script to Save list of Bluetooth address detected

Mon Jun 26, 2017 9:57 pm

Thank you for your replies. I will give this a try.

How would I also be able to store the time a device is detected along with the Bluetooth Address?
So I need to be able to see the Bluetooth address and the time it was detected along with it.

Regards,
LP

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python Script to Save list of Bluetooth address detected

Mon Jun 26, 2017 10:20 pm

Using my code, you could add a timestamp to the results.write() line. I'll let you do some research to figure that out.

User avatar
lp7
Posts: 6
Joined: Thu Jun 22, 2017 2:18 pm
Location: UK

Re: Python Script to Save list of Bluetooth address detected

Tue Jul 04, 2017 10:02 pm

Thanks @Douglas6. I should have googled before asking anyways.

I was wondering if you could suggest a way I could set this up on a second RPi 3 and then send this data to that second RPi to be compared. Basically comparing the two timestamps. For e.g setting up maybe like a TCP client server or something similar in order to communicate maybe on ad hoc connection. Googling a bit, looks like sockets through python is possible but are there other ways if so simpler?

Many thanks,

LP

User avatar
paddyg
Posts: 2529
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python Script to Save list of Bluetooth address detected

Tue Jul 04, 2017 10:07 pm

I've not used it but everyone (on this forum) seems to swear by mosquitto as a simple to use wrapper for socket communication.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python Script to Save list of Bluetooth address detected

Tue Jul 04, 2017 10:18 pm

I'd have to repeat what paddyg said.

I'd be curious to know what you're actually trying to do.

User avatar
lp7
Posts: 6
Joined: Thu Jun 22, 2017 2:18 pm
Location: UK

Re: Python Script to Save list of Bluetooth address detected

Wed Jul 05, 2017 12:58 am

Thank you Paddy and Douglas.

Douglas, I am trying to see how long people take (provided that their smartphone Bluetooth is on) to get from location A to location B.

I am just all over the place trying to figure out how to do this. The more I google the numerous things I find, not all related to my idea but can be applied. I was thinking about this again, would it better to store the data (timestamp, bdaddr) in MySQL on Pi 1 and then send it over to Pi 2. Then somehow compare the differences and get a time taken from A to B by each Bluetooth device.
Or if possible then just send the data from Pi 1 and store it in database on Pi 2. This way Pi 1 is the client and Pi 2 is the server.

Some insight into a solution would be very helpful and any suggestions for a better approach.

Regards,
LP

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python Script to Save list of Bluetooth address detected

Wed Jul 05, 2017 1:13 am

I guess I would have two (or more) Pi's publish bdaddrs, timestamps, and which machine it came from. Then have one Pi as subscriber and database logger. Then another process to crunch the numbers.

Remember, the scan will only find devices that are actively advertising or in discoverable mode. I'd start with one Pi scanning to see what kind of traffic you get.

User avatar
lp7
Posts: 6
Joined: Thu Jun 22, 2017 2:18 pm
Location: UK

Re: Python Script to Save list of Bluetooth address detected

Sun Jul 09, 2017 11:41 am

Subscriber and database logger. I am not sure how that works. i will google a bit to find more about that. Thanks

I have noticed that the code with pexpect has a timeout of 30.
Douglas6 wrote:

Code: Select all

import pexpect

results = open("scan_results.txt", 'w')
child = pexpect.spawn("bluetoothctl")
child.send("scan on\n")
bdaddrs = []

try:
    while True:
        child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))")
        bdaddr = child.match.group(1)
        if bdaddr not in bdaddrs:
            bdaddrs.append(bdaddr)
            results.write(bdaddr+"\n")
except KeyboardInterrupt:
    child.close()
    results.close()
.

Is it possible to increase or have no timeout. I came across

Code: Select all

child.expect(pexpect.EOF, timeout=None)
but not sure where to implement it in that code.

Thanks
LP

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python Script to Save list of Bluetooth address detected

Sun Jul 09, 2017 11:52 am

Add the time 'timeout=None' to the child.expect() line. Simps.

Publisher and subscriber are MQTT terms, so study up on that. Data logger is just a reference to whatever is storing the data, whether in a database, a flat file, or however you decide to record it.

david_1234
Posts: 328
Joined: Mon Jan 01, 2018 2:14 pm

Re: Python Script to Save list of Bluetooth address detected

Wed May 20, 2020 2:36 pm

sorry for the bump
but I want to save time

I want to change something
in the scan resualt I want to see the mac and the rssi of the device
now I can ony see the mac
what I need to add\chagne?
I thought to add

Code: Select all

child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})) RSSI",timeout=None)
but it didn't work

can somene guide me?

also is there any why to put all the scan in interval?
for example to run the scan for 10 seconds once evry 1 min?
Thanks,

Return to “Python”