-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect.sh
60 lines (49 loc) · 1.29 KB
/
connect.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
#!/bin/bash
# colour assignments, see -> http://linuxtidbits.wordpress.com/2008/08/13/output-color-on-bash-scripts-advanced/
ORANGE="\033[38;5;214m"
GREEN="\033[38;5;46m"
RED="\033[38;5;196m"
# print function, first parameter is the string, second parameter the colour.
# colour is optional
function print {
if [ -z $2 ]; then
echo -e $1
else
COLOR_RESET="\033[39m"
echo -e $2$1$COLOR_RESET
fi
}
if [ -z $1 ]; then
print "FATAL: need to provide a address as parameter" $RED
exit 1
fi
if [ -z $2 ]; then
PORTNR=0
else
PORTNR=$2
fi
print "connecting to $1 on rfcomm$PORTNR ..." $ORANGE
# check if we are connected already
go_on=`rfcomm show $PORTNR 2>/dev/null | grep connected`
if [[ -z $go_on ]]; then
sudo rfcomm release $PORTNR 2>/dev/null
# start rfcomm connect in another shell
sudo rfcomm connect /dev/rfcomm$PORTNR $1 > /dev/null &
sleep 1
# wait for connection to be established. wait at most 10 seconds
declare -i COUNTER
COUNTER=0
go_on=`rfcomm show $PORTNR 2>/dev/null | grep connected`
while [[ -z $go_on ]]; do
sleep 1
let COUNTER=$COUNTER+1
go_on=`rfcomm show $PORTNR 2>/dev/null | grep connected`
if [ $COUNTER -eq 10 ]; then
print "connection failed!" $RED
exit 1
fi
done
print "-> connection ok" $GREEN
else
print "-> already connected" $GREEN
fi