开发者

How can I make pybluez return a list of discovered devices every X seconds and then repeat?

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/py开发者_如何学编程bluez/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)


This code worked for me:

'''
Created on Nov 16, 2011    
@author: Radu
'''
import time
import bluetooth

def search():         
    devices = bluetooth.discover_devices(duration=20, lookup_names = True)
    return devices

if __name__=="__main__":
    while True:        
        results = search()
        if (results!=None):
            for addr, name in results:
                print "{0} - {1}".format(addr, name)
            #endfor
        #endif
        time.sleep(60)
    #endwhile

It searches for 20 seconds for a device, and then sleeps for 1 minute, all in an infinite loop. I am working on Windows, with default windows drivers on a Serioux BT Dongle.

Hope it helps.


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        # <-- 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜