forked from adangert/JoustMania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.py
executable file
·45 lines (38 loc) · 1.46 KB
/
pair.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
35
36
37
38
39
40
41
42
43
44
45
import psmove
import os
BT_DIR = '/var/lib/bluetooth/'
class Pair():
def __init__(self):
self.bt_devices = {}
devices = os.listdir(BT_DIR)
for device in devices:
self.bt_devices[device] = []
self.pre_existing_devices()
def pre_existing_devices(self):
for device in self.bt_devices.keys():
device_path = os.path.join(BT_DIR, device)
print ('trust file is ' + str(device_path))
if os.path.exists(device_path):
self.bt_devices[device] = [bt for bt in os.listdir(device_path) if ':' in bt]
else:
print('the path doesnt exist! ' )
def check_if_not_paired(self, addr):
for devs in self.bt_devices.keys():
if addr in self.bt_devices[devs]:
return False
return True
def get_lowest_bt_device(self):
num = 9999999
for dev in self.bt_devices.keys():
if len(self.bt_devices[dev]) < num:
num = len(self.bt_devices[dev])
for dev in self.bt_devices.keys():
if len(self.bt_devices[dev]) == num:
return dev
return ''
def pair_move(self, move):
if move and move.get_serial():
if move.connection_type == psmove.Conn_USB:
self.pre_existing_devices()
if self.check_if_not_paired(move.get_serial().upper()):
move.pair_custom(self.get_lowest_bt_device())