Skip to content

Instantly share code, notes, and snippets.

@electronut
Created July 9, 2017 06:06
Show Gist options
  • Save electronut/180b2b75fc047bd6d330a03786bb1ef4 to your computer and use it in GitHub Desktop.
Save electronut/180b2b75fc047bd6d330a03786bb1ef4 to your computer and use it in GitHub Desktop.
bluey-beacon - python
class BLEScanner:
hcitool = None
hcidump = None
def start(self):
print('Start receiving broadcasts')
DEVNULL = subprocess.DEVNULL if sys.version_info > (3, 0) else open(os.devnull, 'wb')
subprocess.call('sudo hciconfig hci0 reset', shell = True, stdout = DEVNULL)
self.hcitool = subprocess.Popen(['sudo', '-n', 'hcitool', 'lescan', '--duplicates'], stdout = DEVNULL)
self.hcidump = subprocess.Popen(['sudo', '-n', 'hcidump', '--raw'], stdout=subprocess.PIPE)
def stop(self):
print('Stop receiving broadcasts')
subprocess.call(['sudo', 'kill', str(self.hcidump.pid), '-s', 'SIGINT'])
subprocess.call(['sudo', '-n', 'kill', str(self.hcitool.pid), '-s', "SIGINT"])
def get_lines(self):
data = None
try:
print("reading hcidump...\n")
#for line in hcidump.stdout:
while True:
line = self.hcidump.stdout.readline()
line = line.decode()
if line.startswith('> '):
yield data
data = line[2:].strip().replace(' ', '')
elif line.startswith('< '):
data = None
else:
if data:
data += line.strip().replace(' ', '')
except KeyboardInterrupt as ex:
print("kbi")
return
except Exception as ex:
print(ex)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment