Python 如何让pybluez每X秒返回一个已发现设备的列表,然后重复?
我一直在想我怎样才能监控附近的设备 我希望能够运行我的程序,让它每20秒搜索一次设备。问题是,我如何让pybluez放置得很好/ 使用他们的示例代码,很容易让它发现设备。你运行该代码,它会吐出MAC地址,如果你选择,还会吐出设备名称Python 如何让pybluez每X秒返回一个已发现设备的列表,然后重复?,python,bluetooth,Python,Bluetooth,我一直在想我怎样才能监控附近的设备 我希望能够运行我的程序,让它每20秒搜索一次设备。问题是,我如何让pybluez放置得很好/ 使用他们的示例代码,很容易让它发现设备。你运行该代码,它会吐出MAC地址,如果你选择,还会吐出设备名称 我怎样才能把这个循环?我一直在玩下面的代码,但它失败了>我不知道pybluez,但是bluetooth.discover\u devices(lookup\u names=True)本身已经返回了一个iterable,所以你应该循环它 def search():
我怎样才能把这个循环?我一直在玩下面的代码,但它失败了>我不知道pybluez,但是
bluetooth.discover\u devices(lookup\u names=True)
本身已经返回了一个iterable,所以你应该循环它
def search():
while True:
devices = bluetooth.discover_devices(lookup_names = True)
for x in devices: # <--
yield x # <--
def search():
尽管如此:
设备=蓝牙。发现设备(查找名称=真)
对于x-in设备:#此代码对我有效:
'''
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
它搜索一个设备20秒,然后睡眠1分钟,所有这些都在一个无限循环中。我在Windows上工作,在Serioux BT加密狗上有默认的Windows驱动程序
希望有帮助。这很有效,但是。。。看起来设备被缓存了?我想得到一个通知,如果蓝牙设备不存在,但即使我关闭它,它仍然出现在扫描…哈哈,这几乎是难以置信的。我来到这里的想法是:“太棒了,这和我的问题一样!”两年前是我:”(@Pitto)
'''
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