Created
January 10, 2020 09:45
-
-
Save arall/81c47abb0252884c2d0d8acae9666dfc to your computer and use it in GitHub Desktop.
Python BT LE device scanner with RSSI using btmgmt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
devices = [] | |
subprocess.Popen('btmgmt le on', stdout=subprocess.PIPE, shell=True) | |
proc = subprocess.Popen('sudo btmgmt find', stdout=subprocess.PIPE, shell=True) | |
output = proc.communicate() | |
for line in str(output).split('\\n')[:-1]: | |
if 'hci0 dev_found' in line: | |
if 'device' in locals(): | |
devices.append(device); | |
device = {} | |
m = re.search('dev_found: (([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})) type (.*?) rssi (-\d+) flags', line) | |
device['mac'] = m.group(1) | |
device['type'] = m.group(4) | |
device['rssi'] = m.group(5) | |
elif 'AD flags ' in line: | |
m = re.search('AD flags (.*)', line) | |
device['flags'] = m.group(1).rstrip() | |
elif 'name ' in line: | |
m = re.search('name (.*)', line) | |
device['name'] = m.group(1).rstrip() | |
for device in devices: | |
print device |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment