Skip to content
Snippets Groups Projects

Dev ubuntu20.04

Merged Dominik Mehlem requested to merge dev_ubuntu20.04 into master
All threads resolved!

Files

tools/btbind 0 → 100755
+ 110
0
#!/bin/bash
# script to ease connecting to an EV3 brick
# (c) 2018 Johannes Sauer
# (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##*/} <device name> [rfcomm device number]
${0##*/} establishes a persistent connection to the 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.
The device needs to be paired already. Use the Bluetooth setting GUI to do this.
The device will be bound to the device number. Connection is established, once
it is used.
Examples:
${0##*/} EV3-20-B 2
This connects the EV3 brick named "EV3-20-B" to rfcomm2.
${0##*/} 00:16:53:06:D8:67
This connects the EV3 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
# get mac from device name
NAME="$1"
MAC=$(echo -e 'devices \n' | bluetoothctl | awk -F ' ' '$2 ~ /^([0-9a-fA-F]+:)+[0-9a-fA-F]+$/ && $3 == "'"$NAME"'" { print $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. You have to pair the device first! Use the bluetooth setting GUI." >&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 "Not supported by ${0##*/}. Make sure the devices have different names." >&2
exit 2
fi
echo "found paired device $MAC." >&2
# 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
echo "Binding rfcomm${DEVICE} to bluetooth device with address '${MAC}' ..."
rfcomm bind rfcomm$DEVICE $MAC &
Loading