#
# btPoll.py
#
# Perform inquiry of Bluetooth devices and check which of the
# target devices are present. Periodically re-check and report
# any change in state
#
# Jouni Paulus, jouni.paulus(a)iki.fi, 25.11.2007


import time
import sys
import bluetooth

pollPeriod = 120 # seconds

# list of devices to be polled
pollList = ["00:16:4E:XX:XX:XX"]

devPresent = [ [False] for i in xrange(len(pollList))]

while (1):
    print "discovering devices..."
    nearbyDevices = bluetooth.discover_devices()
    for pollIdx in range(0,len(pollList)-1):
        pollAddr = pollList[pollIdx]
        thisFound = False
        for foundAddr in nearbyDevices:
            if foundAddr == pollAddr:
                thisFound = True

        if thisFound:
            presenceStr = "present"
        else:
            presenceStr = "absent"
            
        if devPresent[pollIdx] != thisFound:
            print "device " + pollAddr + " changed its state to " + presenceStr
            devPresent[pollIdx] = thisFound
            
    print "sleeping"
    time.sleep(pollPeriod)


