-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbtautopair.sh
144 lines (108 loc) · 3.25 KB
/
btautopair.sh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
#script to run on system boot that enables bluetooth, and pairs any available game controllers
#that are available during the ~1-2 mins that the script runs for.
#requires empty (http://empty.sourceforge.net/)
EMPTYBIN='/storage/empty'
#log events to a file (helpful when script is run in background; use tail -f to see whats going on)
LOGPTH='/storage/bt_helper.log'
#string utils that work on busybox...
stringContain() { [ -z "${2##*$1*}" ]; }
strindex() {
x="${1%%$2*}"
[ "$x" = "$1" ] && echo -1 || echo ${#x}
}
echo "script started" > $LOGPTH
sleep 2
#systemctl status bluetooth
#make sure bluetooth is up and running.
touch /storage/.cache/services/bluez.conf
systemctl enable bluetooth
systemctl start bluetooth
echo "bluetooth up and running" >> $LOGPTH
#start the bluetooth interactive console
$EMPTYBIN -f bluetoothctl
sleep 2
echo "bluetooth setting up" >> $LOGPTH
$EMPTYBIN -s "agent on\n"
$EMPTYBIN -w "Agent registered"
sleep 1
$EMPTYBIN -s "default-agent\n"
$EMPTYBIN -w "Default agent request successful"
sleep 1
$EMPTYBIN -s "power on\n"
$EMPTYBIN -w "Changing power on succeeded"
sleep 1
$EMPTYBIN -s "discoverable on\n"
$EMPTYBIN -w "Changing discoverable on succeeded"
sleep 1
$EMPTYBIN -s "pairable on\n"
$EMPTYBIN -w "Changing pairable on succeeded"
sleep 1
$EMPTYBIN -s "scan on\n"
$EMPTYBIN -w "Discovery started"
echo "bluetooth scanning" >> $LOGPTH
#look for a bunch of messages... might want to tweak this
for i in `seq 1 40`
do
echo "waiting for event $i..." >> $LOGPTH
#look for a NEW, waiting up to 10s
ONELINE="$($EMPTYBIN -r -t 10)"
echo "saw $ONELINE" >> $LOGPTH
if [ -z "$ONELINE" ]; then
#echo "empty string"
#hack
ONELINE="cheese"
fi
if stringContain "NEW" "$ONELINE" && stringContain "Device " "$ONELINE"; then
echo "processing device" >> $LOGPTH
#capture MAC
MAC_INDEX=$(strindex "$ONELINE" "Device")
#the MAC address is offset 7 chars from the word Device...
MAC_INDEX=$((MAC_INDEX + 7))
#mac is 17 chars long
MACADDR=${ONELINE:$MAC_INDEX:17}
#for any new MAC, request info
echo "requesting info for $MACADDR" >> $LOGPTH
$EMPTYBIN -s "info $MACADDR\n"
#listen for "Icon: input-gaming"
$EMPTYBIN -w "Icon: input-" -t 5
ERRORCODE=$?
echo "ERRORCODE $ERRORCODE" >> $LOGPTH
if [ "$ERRORCODE" -eq 1 ]; then
#dont ask - workaround/hack for first command sent
$EMPTYBIN -s "poopies\n"
$EMPTYBIN -w "Invalid command"
#pair
echo "going to pair" >> $LOGPTH
$EMPTYBIN -s "pair $MACADDR\n"
#eat some lines (workaround/hack)
ONELINE="$($EMPTYBIN -r -t 10)"
echo "saw $ONELINE" >> $LOGPTH
ONELINE="$($EMPTYBIN -r -t 10)"
echo "saw $ONELINE" >> $LOGPTH
ONELINE="$($EMPTYBIN -r -t 10)"
echo "saw $ONELINE" >> $LOGPTH
ONELINE="$($EMPTYBIN -r -t 10)"
echo "saw $ONELINE" >> $LOGPTH
#actually wait for success
$EMPTYBIN -w "Pairing successful"
#connect
echo "going to connect" >> $LOGPTH
$EMPTYBIN -s "connect $MACADDR\n"
$EMPTYBIN -w "Connection successful"
#trust
echo "going to trust" >> $LOGPTH
$EMPTYBIN -s "trust $MACADDR\n"
sleep 2
else
echo "not a gamepad" >> $LOGPTH
fi
else
echo "not a new device event" >> $LOGPTH
fi
done
#exit!
echo "exiting" >> $LOGPTH
$EMPTYBIN -s "exit\n"
sleep 5
killall bluetoothctl