views:

59

answers:

1

I've been trying to figure out how I can use pybluez to monitor nearby devices...

I want to be able to run my program and have it search for devices every 20 seconds. The problem is, how do I get pybluez to place nicely? :/

Using their example code http://code.google.com/p/pybluez/source/browse/trunk/examples/simple/inquiry.py, it's easy enough to get it to discover devices. You run that code and it'll spit out MAC address and, if you choose, the device names.

How can I put this in a loop? I've been playing around with the following code but it's failing >.<

import bluetooth

def search():
   while True:
      devices = bluetooth.discover_devices(lookup_names = True)

      yield devices

for addr, name in search():
   print "{0} - {1}".format(addr, name)
A: 

I don't know pybluez, but bluetooth.discover_devices(lookup_names = True) itself already returns an iterable, so you should loop it for yielding.

def search():
   while True:
      devices = bluetooth.discover_devices(lookup_names = True)
      for x in devices: # <--
         yield x        # <-- 
KennyTM