Skip to content

Instantly share code, notes, and snippets.

@itskenny0
Last active October 8, 2020 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itskenny0/96d02f96deb5bda8b215e39f810b31ba to your computer and use it in GitHub Desktop.
Save itskenny0/96d02f96deb5bda8b215e39f810b31ba to your computer and use it in GitHub Desktop.
Scans for BLE devices and publishes found devices to nsq topic.
from bluepy.btle import Scanner, DefaultDelegate
import json
import time
import gnsq
nsq = gnsq.Producer('127.0.0.1:4150')
nsq.start()
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
output = {'ts': time.time(), 'rssi': dev.rssi, 'mac': dev.addr}
for (adtype, desc, value) in dev.getScanData():
#if desc in ["Complete Local Name"]:
output[desc.replace(" ", "")] = value;
jsonOut = json.dumps(output)
nsq.publish('bluetooth', jsonOut)
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment