Skip to content

Instantly share code, notes, and snippets.

@arall
Created January 10, 2020 09:45
Show Gist options
  • Save arall/81c47abb0252884c2d0d8acae9666dfc to your computer and use it in GitHub Desktop.
Save arall/81c47abb0252884c2d0d8acae9666dfc to your computer and use it in GitHub Desktop.
Python BT LE device scanner with RSSI using btmgmt
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