一旦使用 PyBluez 在附近找不到一台设备,代码就会失败

Code failing once one device isn't found nearby using PyBluez

我目前正在为我的 类 之一编写一些代码,它显示在此处(已更改 names/addresses 以隐藏名称)。

# coding=utf:8
#————————————————————————Attendance Checker Start————————————————————————#
import bluetooth
import time

#-----Function Definition Start-----#
def student_check(index):
    result = bluetooth.lookup_name(blue_address_list[index], timeout=1)

    if (result is not None):
        return True
    else:
        return False
#-----Function Definition End-----#


#————————Defined Dictionary Start————————#
blue_student_list = ['Name1', 'Name2', 'Name3', 'Name4',
                  'Name5', 'Name6', 'Name7', 'Name8',
                  'Name9']
blue_address_list = ['Address1', 'Address2', 'Address3', 'Address4', 'Address5', 'Address6', 'Address7', 'Address8', 'Address9']
#—————————Defined Dictionary End—————————#

#———————————————Print Method Start———————————————#

print ' '
time.sleep(1)
print 'Checking who is here on ' + time.strftime('%b %d, %Y', time.gmtime())
print ' '
time.sleep(1)

for i in range(0, len(blue_address_list)):
    if (student_check(i)):
        print blue_student_list[i] + ': Present '
    else:
        print blue_student_list[i] + ': Absent '

print 'Script Completed'

#————————————————Print Method End————————————————#

#—————————————————————————Attendance Checker End—————————————————————————#

我的问题是当脚本启动时,我得到了这个输出。

Checking who is here on Feb 24, 2016

Name1: Present
Name2: Absent
Name3: Absent
Name4: Absent
Name5: Absent
Name6: Absent
Name7: Absent
Name8: Absent
Name9: Absent

Script Completed

我的问题不是他们缺席。我在 Name7 下配对了另一个设备,无论如何都显示不存在。我相信它实际上会检查第一个,一旦缺席,其余的就会失败。我认为这是因为它们都在同一时间消失,它没有任何延迟,不像第一个在检查附近的设备时有延迟。

__sizeof__ returns 给定对象的内部大小(以字节为单位),而不是项目数。我认为你应该使用 len(blue_address_list) 而不是它。

编辑:增加超时将解决问题,似乎bluetooth.lookup_name 无法在给定时间响应。