#!/usr/bin/python import subprocess import sqlite3 import time def collect(): data = [] b = subprocess.Popen(['hcitool', 'scan'], stdout=subprocess.PIPE) x = b.communicate()[0] for l in x.splitlines()[1:]: l = l.strip().decode('utf8', 'replace') parts = l.split('\t', 1) mac, name = parts if len(parts)==2 else (l, '') data.append((mac, name)) return data conn = sqlite3.connect('bt.sqlite3') cur = conn.cursor() cur.execute('CREATE TABLE IF NOT EXISTS bt (date INTEGER, mac TEXT, name TEXT)') conn.commit() while 1: print 'collecting' devs = collect() for m,n in devs: print m, n t = int(time.time()) cur.executemany('INSERT INTO bt VALUES (?,?,?)', map(lambda (m,n): (t,m,n), devs)) conn.commit() time.sleep(10)