root/trunk/tools/LinuxConnection/btconnect @ 742

Revision 742, 2.3 KB (checked in by balle, 4 years ago)

* merging updates to btconnect from livecd branch

RevLine 
[153]1#!/bin/bash
2
3# script to ease connecting to an NXT brick
[635]4# (c) 2007-2009 Johannes Ballé
[153]5
[635]6# test if syntax is correct; if not, print help and exit
[153]7
[742]8if [ $# = 0 -o $# -gt 2 ] || [ "$1" = "--help" -o "$1" = "-h" ] || ! { [ $# = 1 ] || [ "$2" -ge 0 ] 2>/dev/null; }; then
[635]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).
[742]33
34                Depending on your Linux distribution, you will find the rfcomm device file
35                either at /dev/rfcommX or /dev/bluetooth/rfcommX.
[635]36        END_HELP
37        exit 128
38fi
39
40# if device name is given, check if it's already used
41
42if [ $# = 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
48fi
49
50# if hardware address is given, we're all set. otherwise, perform a scan
51
52if [[ $1 =~ ^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$ ]]; then
53        MAC=$1
[153]54else
[742]55        NAME="$1"
[635]56        echo -n "Scanning for '$NAME' ... " >&2
[742]57        MAC=$( hcitool scan | awk -F '\t' '$2 ~ /^([0-9a-fA-F]+:)+[0-9a-fA-F]+$/ && $3 == "'"$NAME"'" { print $2 }' )
[153]58        if [ -z $MAC ]; then
59                echo "not found." >&2
60                exit 1
61        fi
[635]62        if [[ $MAC == *\ * ]]; then
[153]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
73fi
74
[635]75# if device is not given, find first unused device
[153]76
[635]77if [ -z $DEVICE ]; then
78        (( DEVICE=0 ))
79        while [ -e /dev/rfcomm$DEVICE -o -e /dev/bluetooth/rfcomm$DEVICE ]; do
80                (( DEVICE++ ))
81        done
82fi
83
84rfcomm -r connect rfcomm$DEVICE $MAC &
85
[153]86trap "kill $!" 1 3 9 15
87wait
Note: See TracBrowser for help on using the browser.