| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # script to ease connecting to an NXT brick |
|---|
| 4 | # (c) 2007-2009 Johannes Ballé |
|---|
| 5 | |
|---|
| 6 | # test if syntax is correct; if not, print help and exit |
|---|
| 7 | |
|---|
| 8 | if [ $# = 0 -o $# -gt 2 ] || [ "$1" = "--help" -o "$1" = "-h" ] || ! { [ $# = 1 ] || [ "$2" -ge 0 ] 2>/dev/null; }; then |
|---|
| 9 | cat <<-END_HELP >&2 |
|---|
| 10 | ${0##*/} <MAC address/device name> [rfcomm device number] |
|---|
| 11 | |
|---|
| 12 | ${0##*/} establishes a persistent connection to the MAC address |
|---|
| 13 | or device name given in the first argument. The second, optional argument |
|---|
| 14 | specifies which rfcomm device should be used. If omitted, the first |
|---|
| 15 | available device will be used. |
|---|
| 16 | |
|---|
| 17 | Once the connection is established, ${0##*/} waits until it receives |
|---|
| 18 | any termination signal (which happens, for instance, when Ctrl-C is |
|---|
| 19 | pressed or the shell window is closed). Then, the connection is |
|---|
| 20 | taken down. |
|---|
| 21 | |
|---|
| 22 | Examples: |
|---|
| 23 | |
|---|
| 24 | ${0##*/} NXT-20-B 2 |
|---|
| 25 | |
|---|
| 26 | This connects the NXT brick named "NXT-20-B" to rfcomm2. |
|---|
| 27 | |
|---|
| 28 | ${0##*/} 00:16:53:06:D8:67 |
|---|
| 29 | |
|---|
| 30 | This connects the NXT brick with the hardware address 00:16:53:06:D8:67 |
|---|
| 31 | to the first available rfcomm device (if no others are used, this will |
|---|
| 32 | be rfcomm0). |
|---|
| 33 | |
|---|
| 34 | Depending on your Linux distribution, you will find the rfcomm device file |
|---|
| 35 | either at /dev/rfcommX or /dev/bluetooth/rfcommX. |
|---|
| 36 | END_HELP |
|---|
| 37 | exit 128 |
|---|
| 38 | fi |
|---|
| 39 | |
|---|
| 40 | # if device name is given, check if it's already used |
|---|
| 41 | |
|---|
| 42 | if [ $# = 2 ]; then |
|---|
| 43 | (( DEVICE=$2 )) |
|---|
| 44 | if [ -e /dev/rfcomm$DEVICE -o -e /dev/bluetooth/rfcomm$DEVICE ]; then |
|---|
| 45 | echo "rfcomm$DEVICE is already in use!" |
|---|
| 46 | exit 3 |
|---|
| 47 | fi |
|---|
| 48 | fi |
|---|
| 49 | |
|---|
| 50 | # if hardware address is given, we're all set. otherwise, perform a scan |
|---|
| 51 | |
|---|
| 52 | if [[ $1 =~ ^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$ ]]; then |
|---|
| 53 | MAC=$1 |
|---|
| 54 | else |
|---|
| 55 | NAME="$1" |
|---|
| 56 | echo -n "Scanning for '$NAME' ... " >&2 |
|---|
| 57 | MAC=$( hcitool scan | awk -F '\t' '$2 ~ /^([0-9a-fA-F]+:)+[0-9a-fA-F]+$/ && $3 == "'"$NAME"'" { print $2 }' ) |
|---|
| 58 | if [ -z $MAC ]; then |
|---|
| 59 | echo "not found." >&2 |
|---|
| 60 | exit 1 |
|---|
| 61 | fi |
|---|
| 62 | if [[ $MAC == *\ * ]]; then |
|---|
| 63 | echo "found multiple devices:" >&2 |
|---|
| 64 | echo >&2 |
|---|
| 65 | for i in $MAC; do |
|---|
| 66 | echo ' '$i >&2 |
|---|
| 67 | done |
|---|
| 68 | echo >&2 |
|---|
| 69 | echo "Use ${0##*/} with one of the addresses above." >&2 |
|---|
| 70 | exit 2 |
|---|
| 71 | fi |
|---|
| 72 | echo "found $MAC." >&2 |
|---|
| 73 | fi |
|---|
| 74 | |
|---|
| 75 | # if device is not given, find first unused device |
|---|
| 76 | |
|---|
| 77 | if [ -z $DEVICE ]; then |
|---|
| 78 | (( DEVICE=0 )) |
|---|
| 79 | while [ -e /dev/rfcomm$DEVICE -o -e /dev/bluetooth/rfcomm$DEVICE ]; do |
|---|
| 80 | (( DEVICE++ )) |
|---|
| 81 | done |
|---|
| 82 | fi |
|---|
| 83 | |
|---|
| 84 | rfcomm -r connect rfcomm$DEVICE $MAC & |
|---|
| 85 | |
|---|
| 86 | trap "kill $!" 1 3 9 15 |
|---|
| 87 | wait |
|---|