-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtconnect
More file actions
executable file
·87 lines (68 loc) · 2.29 KB
/
Copy pathbtconnect
File metadata and controls
executable file
·87 lines (68 loc) · 2.29 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# script to ease connecting to an NXT brick
# (c) 2007-2009 Johannes Ballé
# test if syntax is correct; if not, print help and exit
if [ $# = 0 -o $# -gt 2 ] || [ "$1" = "--help" -o "$1" = "-h" ] || ! { [ $# = 1 ] || [ "$2" -ge 0 ] 2>/dev/null; }; then
cat <<-END_HELP >&2
${0##*/} <MAC address/device name> [rfcomm device number]
${0##*/} establishes a persistent connection to the MAC address
or device name given in the first argument. The second, optional argument
specifies which rfcomm device should be used. If omitted, the first
available device will be used.
Once the connection is established, ${0##*/} waits until it receives
any termination signal (which happens, for instance, when Ctrl-C is
pressed or the shell window is closed). Then, the connection is
taken down.
Examples:
${0##*/} NXT-20-B 2
This connects the NXT brick named "NXT-20-B" to rfcomm2.
${0##*/} 00:16:53:06:D8:67
This connects the NXT brick with the hardware address 00:16:53:06:D8:67
to the first available rfcomm device (if no others are used, this will
be rfcomm0).
Depending on your Linux distribution, you will find the rfcomm device file
either at /dev/rfcommX or /dev/bluetooth/rfcommX.
END_HELP
exit 128
fi
# if device name is given, check if it's already used
if [ $# = 2 ]; then
(( DEVICE=$2 ))
if [ -e /dev/rfcomm$DEVICE -o -e /dev/bluetooth/rfcomm$DEVICE ]; then
echo "rfcomm$DEVICE is already in use!"
exit 3
fi
fi
# if hardware address is given, we're all set. otherwise, perform a scan
if [[ $1 =~ "^[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$" ]]; then
MAC=$1
else
NAME="$1"
echo -n "Scanning for '$NAME' ... " >&2
MAC=$( hcitool scan | awk -F '\t' '$2 ~ /^([0-9a-fA-F]+:)+[0-9a-fA-F]+$/ && $3 == "'"$NAME"'" { print $2 }' )
if [ -z $MAC ]; then
echo "not found." >&2
exit 1
fi
if [[ "$MAC" == "*\ *" ]]; then
echo "found multiple devices:" >&2
echo >&2
for i in $MAC; do
echo ' '$i >&2
done
echo >&2
echo "Use ${0##*/} with one of the addresses above." >&2
exit 2
fi
echo "found $MAC." >&2
fi
# if device is not given, find first unused device
if [ -z $DEVICE ]; then
(( DEVICE=0 ))
while [ -e /dev/rfcomm$DEVICE -o -e /dev/bluetooth/rfcomm$DEVICE ]; do
(( DEVICE++ ))
done
fi
rfcomm -r connect rfcomm$DEVICE $MAC &
trap "kill $!" 1 3 9 15
wait