View Single Post
Posts: 30 | Thanked: 42 times | Joined on Oct 2010 @ Russia
#4
Here is my script that scans bluetooth in a loop and records discovered devices in a sqlite3 database. BT must be powered on before.

The idea is to datamine recorded info later. For example how often do I run twice into the same people in the subway or something. Sometimes phone names are rather funny.

The script may be extended to use obexftp to automatically send a picture to a newly discovered device, but I was unable to initiate transfer with my Razr V3 without pin exchange.

Code:
#!/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)
 

The Following 7 Users Say Thank You to butler For This Useful Post: