Package conduit :: Package modules :: Package PhoneModule :: Module ScanThreads
[hide private]

Source Code for Module conduit.modules.PhoneModule.ScanThreads

 1  import logging 
 2  log = logging.getLogger("modules.Phone") 
 3   
 4  import thread 
 5   
 6  import threading 
 7  import bluetooth 
 8  import time 
 9   
10  import conduit 
11   
12  UNKNOWN_VALUE = "Unknown" 
13   
14 -class ScanThread(threading.Thread):
15 SLEEP_TIME = 20 16 SLEEP = 0.1
17 - def __init__(self):
18 threading.Thread.__init__(self) 19 self._found = {} 20 self._cancelled = False 21 self._foundLock = threading.Lock()
22
23 - def found_phone(self, address, name):
24 #locked because, AIUI, the DeviceDiscover callsback anytime, 25 #from another thread 26 self._foundLock.acquire() 27 if address not in self._found: 28 log.info("Thread %s Found Phone: %s" % (thread.get_ident(),address)) 29 self._found[address] = {"name":name} 30 self._foundLock.release()
31
32 - def pause_scanning(self):
33 i = 0 34 while ( i < (self.SLEEP_TIME/self.SLEEP) ) and ( self.is_cancelled() == False ): 35 time.sleep(self.SLEEP) 36 i += 1
37
38 - def is_cancelled(self):
39 return conduit.GLOBALS.cancelled or self._cancelled
40
41 - def cancel(self):
42 self._cancelled = True
43
44 -class DeviceDiscovererFilter(bluetooth.DeviceDiscoverer):
45 - def __init__(self, parent):
46 bluetooth.DeviceDiscoverer.__init__(self) 47 self.parent = parent
48
49 - def device_discovered(self, address, device_class, name):
50 ''' 51 Called when device is iscovered, checks device is phone 52 ''' 53 log.info("Bluetooth Device Discovered: %s@%s" % (name,address)) 54 major_class = ( device_class & 0xf00 ) >> 8 55 # We want only devices with phone class 56 # See https://www.bluetooth.org/apps/content/?doc_id=49706 57 if major_class == 2: 58 self.parent.found_phone(address, name)
59
60 - def inquiry_complete(self):
61 log.debug("Bluetooth Search Complete")
62