1

I am working on a program to help my 'bar code scanner'(HID) connect to back to my Raspberry Pi once it loses connection. There is an issue with Ubuntu based computers with reconnecting to Bluetooth devices.

Long story short I realized a folder appears when my device is connected, my goal is to detect the folder and reboot the bluetooth communications as soon as the folder is not detected anymore. here is my code:

import os, time
while(1):
    if os.path.isdir("/sys/bus/usb/devices/1-1.5:1.0/bluetooth/hci0/hci0:72"):
        print("Safe!")
        time.sleep(10)
    else:
        os.system("sudo rfkill block bluetooth && sudo rfkill unblock bluetooth")  #RESET BLUETOOTH COMMUNICATIONS - allows device to reconnect!!!!
        print("Bluetooth system rebooted")
        time.sleep(10)

Everything works great however from time to time the "hci0:72" changes to "hci0:71" or "hci0:XX" and I want to cover myself for these situations.

P.S. "hci0:XX" is a directory....

I've tried

os.path.isdir("/sys/bus/usb/devices/1-1.5:1.0/bluetooth/hci0/hci0:**"):

and

os.path.isdir("/sys/bus/usb/devices/1-1.5:1.0/bluetooth/hci0/hci0:.."):

but False is returned whenever I try using "*" or ".".

Would love some help with polishing off this program, thank you for any and all help!!!!

A. Cola
  • 49
  • 2
  • 9
  • I guess you could just find all directories in the parent directory of the last one and check if one exists with the desired name. That should be some directory starting with: hci0 in this case. But im not sure if this is the pythonic way. good luck – Wolf Vos Apr 13 '16 at 16:15

1 Answers1

0

Try this answer:

python - Use wildcard with os.path.isfile()

import glob
glob.golb('/sys/bus/usb/devices/1-1.5:1.0/bluetooth/hci0/hci0:*')
Community
  • 1
  • 1
Levi Muniz
  • 389
  • 3
  • 16