-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetoothScanner.py
More file actions
65 lines (56 loc) · 1.76 KB
/
Copy pathBluetoothScanner.py
File metadata and controls
65 lines (56 loc) · 1.76 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from subprocess import Popen, PIPE, STDOUT
import time
import sys
import os
LOCK_FILE = 'bluetooth.lck'
# wait for bluetooth.lck to be released
while os.path.isfile(LOCK_FILE):
time.sleep(5)
created_lck = False
try:
# create lock file
lck_file = open(LOCK_FILE, 'w')
lck_file.write('scanner')
lck_file.close()
created_lck = True
# Default Timeout
TIMEOUT_SCAN = 60
# If timeout passed in as argument, use it
if len(sys.argv) >= 2:
TIMEOUT_SCAN = int(sys.argv[1])
# Start subprocess with stdout and stdin as Pipes
p = Popen(['sudo bluetoothctl', 'f'], shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
# Wait for bluetoothctl to initialize the controller
time.sleep(5);
# Start scanning
print('Starting Scan...')
p.stdin.write(b'scan on\n')
# Wait for scan to capture devices
time.sleep(TIMEOUT_SCAN)
# Stop Scanning
print('Scan done')
p.stdin.write(b'scan off\n')
p.stdout.flush()
# Print out devices
p.stdin.write(b'devices\n')
# Grab the output from process
bluetooth_output = p.communicate()[0]
# Split output by lines
bluetooth_list = bluetooth_output.decode().splitlines()
# Create list of devices
device_list = set()
# Parse the output for lines that start with Device
for line in bluetooth_list:
if line.startswith('Device'):
identifiers = line.split(' ')
device_list.add('{0}|{1}'.format(identifiers[1], " ".join(identifiers[2:])))
output = open('devices.list', 'w');
# Print the devices to file
for device in device_list:
output.write("{0}\n".format(device))
output.close()
except Exception:
print('Errored Out')
finally:
if created_lck:
os.remove(LOCK_FILE)