#!/usr/bin/python DEVICES = ["00:19:4F:A4:B7:5B","08:00:28:45:60:09"] KEYS = [("/dev/lock0", "k6666")] NEEDED = 1 LOCK_CMD = "~/bin/onlock" UNLOCK_CMD = "~/bin/onunlock" locked = False haderror = False import bluetooth import _bluetooth as _bt import time import os LOCK_CMD = os.path.expanduser(LOCK_CMD) UNLOCK_CMD = os.path.expanduser(UNLOCK_CMD) class BluetoothError(Exception): pass def lock(): print "Locking..." os.system(LOCK_CMD) def unlock(): print "Unlocking..." os.system(UNLOCK_CMD) def discover(): try: sock = bluetooth._gethcisock() return _bt.hci_inquiry(sock, 5, True) except _bt.error: sock.close() print "error communicating with local bluetooth adapter" raise BluetoothError def examine(file, key): try: return open(file).read(1024).strip() == key except IOError: return False while 1: try: devices = discover() print devices if haderror: haderror = False continue if len([x for x in devices if x in DEVICES] + [x for x,y in KEYS if examine(x,y)]) >= NEEDED: if locked: locked = False unlock() else: if not locked: locked = True lock() except BluetoothError: haderror = True pass