/
btstdinclient.py
69 lines (55 loc) · 1.74 KB
/
btstdinclient.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python3
import bluetooth
from bluetooth import BluetoothError
import os
import time
import sys
import argparse
import fileinput
import re
parser = argparse.ArgumentParser(description="UInput forwarding over bluetooth. Connects to RFCOMM with uuid of 1111 by default")
parser.add_argument('-u', '--uuid', dest="uuid", default="1111", action="store", help="UUID of remote service")
args = parser.parse_args()
def connect():
print("Trying connect")
service_matches = bluetooth.find_service(uuid=args.uuid, address=None) # set as none to scan?
if len(service_matches) == 0:
print("Couldn't find the UInput KVM service.")
return None
# time.sleep(5)
#go with the first match
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Found server at:", port, name, host)
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((host, port))
return sock
#first run init
sock = None
while sock is None:
sock = connect()
time.sleep(1)
data = ""
for line in fileinput.input():
line = line.strip()
if "SYN_REPORT" in line and data!="":
try:
# print(data)
ret = sock.send(data)
data = ""
except BluetoothError:
print("Device disconnect; BluetoothError")
sock = None
while sock is None:
sock = connect()
time.sleep(1)
else:
try:
mat = re.findall(r'type (.*?) .*?, code (.*?) .*?, value (.*?)$', line)[0]
# print(int(mat[2], 16))
data += '{"type":'+mat[0]+',"code":'+mat[1]+',"value":'+mat[2]+'}'
except:
pass
sock.close()