-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_tools.py
34 lines (24 loc) · 1010 Bytes
/
bluetooth_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import subprocess
import log
import time
def fix_device(device_mac_address, scan_timeout_in_seconds):
subprocess.run(["bluetoothctl", "remove", device_mac_address])
scan_for_device(device_mac_address, scan_timeout_in_seconds)
subprocess.run(["bluetoothctl", "connect", device_mac_address])
def scan_for_device(mac_address, timeout):
bluetooth_device_scan = subprocess.Popen(
["bluetoothctl", "scan", "on"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
log.info("Bluetooth device scan for {} initiated!".format(mac_address))
time.sleep(timeout)
bluetooth_device_scan.terminate()
log.info("Device scan is complete.")
while bluetooth_device_scan.stdout.peek():
scan_result = bluetooth_device_scan.stdout.readline().decode("UTF-8")
if mac_address in scan_result:
log.info("Found device {} while scanning.".format(mac_address))
return True
log.error("Could not located device {} while scanning!".format(mac_address))
return False