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

I was trying to figure out how I can use pybluez to monitor neighboring devices ...

I want to be able to run my program and search for it every 20 seconds. The problem is, how can I get pybluez beautifully?: /

Using your http://code.google.com/p/pybluez/source/browse/trunk/examples/simple/inquiry.py code example , it's easy enough to find it for device discovery. You run this code and it will spit out the MAC address and, if you choose, device names.

How can I put this in a loop? I play with the following code, but it does not work>. <

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)
+3
2

:

'''
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 Dongle.

, .

+4

pybluez, bluetooth.discover_devices(lookup_names = True) , yielding.

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

Source: https://habr.com/ru/post/1756729/


All Articles