多个蓝牙设备扫描

时间:2018-06-10 07:38:37

标签: python raspberry-pi

新手在这里。我一直试图扩展一些代码来扫描几个设备,而不是只扫描一部手机。 这是原始代码:

import automationhat
import bluetooth
import time

while True:
    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
    result = bluetooth.lookup_name('B0:70:2D:D0:C9:XX', timeout=5)
    if (result != None):
        print "User present"
        automationhat.relay.one.on()
        time.sleep(2)
        automationhat.relay.one.off()
        time.sleep(200)
    else:
        print "User out of range"
        automationhat.relay.one.off()
    time.sleep(5)

这是在我尝试扩展它之后。

import automationhat
import bluetooth
import time

DEVICES=['D8:BB:2C:XX:22:17', '34:AB:37:EA:XX:XX', '74:8D:08:XX:XX:7B']

while True:

        print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())

        for device in DEVICES:
                result = bluetooth.lookup_name(device, timeout=5)
        if (result != None):
                print "User present"
                automationhat.relay.one.on()
                time.sleep(2)
                automationhat.relay.one.off()
                time.sleep(20)
        else:
                print "User out of range"
                automationhat.relay.one.off()

        time.sleep(10)

但它似乎只考虑DEVICES列表中的最后一个设备。 我错过了什么? 对不起“愚蠢”的问题,但我刚刚开始,我在网上找不到任何东西。 干杯

1 个答案:

答案 0 :(得分:1)

处理result的操作看起来不在for device in DEVICES:循环的范围内,因此result始终具有最后一个设备的值。

缩进result = bluetooth.lookup_name(device, timeout=5)下方的代码,使其处于同一级别。

相关问题