Skip to content

Instantly share code, notes, and snippets.

@itskenny0
Created November 26, 2019 13:47
Show Gist options
  • Save itskenny0/ae263358a5d08c1f9696e617d7a95256 to your computer and use it in GitHub Desktop.
Save itskenny0/ae263358a5d08c1f9696e617d7a95256 to your computer and use it in GitHub Desktop.
submit scanned BLE devices into NSQ queue in JSON format
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