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

329 Views Asked by At

I'm currently working on some code for one of my classes which is shown right here (changed names/addresses to hide names).

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

My issue is when the script starts, I get this output.

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

My issue with this is not them being absent. I have another device paired under Name7 that says absent no matter what. I believe it actually checks on the first one and fails the rest once one goes absent. The reason I think this is because they all go absent at the exact same time, it doesn't have any delay unlike the first which has a delay when checking for the nearby device.

1

There are 1 best solutions below

5
On BEST ANSWER

__sizeof__ returns the internal size in bytes for the given object, not the number of items. I think you should use len(blue_address_list) instead of it.

Edit: increasing timeout will solve problem, seems bluetooth.lookup_name can not respond on given time.