#!/bin/bash
#
# BTDUN ver.0.2.3
# Script for Pandora
# by PowerGod (12/01/2013)
#
export HOME="/mnt/utmp/btdun"
export BTSTATE=1
cd $HOME
# Find if the bluetooth is ON
INTERFACE="`hciconfig | grep "^hci" | cut -d ':' -f 1`"
if ! hciconfig "$INTERFACE" | grep UP &>/dev/null; then
# /usr/bin/zenity --question\
# --title="BTDUN"\
# --text="Bluetooth not enabled!\n\nWould you like to start it now?"\
# --ok-label="Yes" --cancel-label="Exit"\
# --window-icon="./icon.png"
# if [ $? -eq 0 ]; then
# Remembers that bluetooth was OFF
export BTSTATE=0
/usr/pandora/scripts/op_bluetooth.sh
# else
# exit
# fi
fi
# Get the devices list
#hcitool scan|grep -G ^[^Scanning] > temp1.tmp
# Get the paired devices list
bluetooth-properties -d | grep "^Device:" | gawk -F "Device:" '{print $2}' > temp1.tmp
# export vdevice=$(cat templist.tmp | zenity --list --text="BTDUN" --column="Paired devices:")
cat temp1.tmp | zenity --list --text="Select the device for the connection" --column="Paired devices:" --title="BTDUN" --width=400 --height=300 --window-icon="./icon.png" > temp2.tmp
if [ $? -ne 0 ]; then
# Find if the bluetooth is still ON
# and will turn it OFF if it was disabled before the connection
INTERFACE="`hciconfig | grep "^hci" | cut -d ':' -f 1`"
if hciconfig "$INTERFACE" | grep UP &>/dev/null; then
if [ ${BTSTATE} = 0 ]; then
/usr/pandora/scripts/op_bluetooth.sh
fi
fi
exit 0;
fi
# Getting only the device address, cause the bluetooth name
# can contain special characters, spaces and all...
export vdevice=$(cat temp2.tmp | grep -o '\([^ \s]*\)$' | gawk -F "(" '{print $2}' | gawk -F ")" '{print $1}')
export vchannel=$(sdptool search --bdaddr ${vdevice} DUN | grep Channel | gawk -F ":" '{print $2}')
# If the channel is not found, the device is not DUN capable or not responding
if [ -z "${vchannel}" ] ; then
zenity --error --text="The selected device does not have a DUN service or is not responding."
# Find if the bluetooth is still ON
# and will turn it OFF if it was disabled before the connection
INTERFACE="`hciconfig | grep "^hci" | cut -d ':' -f 1`"
if hciconfig "$INTERFACE" | grep UP &>/dev/null; then
if [ ${BTSTATE} = 0 ]; then
/usr/pandora/scripts/op_bluetooth.sh
fi
fi
exit ;
fi
#zenity --info --text="Selected device ${vdevice} use channel ${vchannel}"
notify-send -u normal "Selected device ${vdevice} use channel ${vchannel}" -t 10000
# Find if there's already a configuration for this device
# - if not it will be added
# - if found it will be used
# - if there are more than one it will make a selection window
export vfound=$(cat devices.conf | grep '^'${vdevice} | wc -l)
if (( vfound<1 )) ; then
export vdialnum=$(zenity --entry --text="Insert the dial number for\n${vdevice}" --entry-text="*99#")
# If the string is empty uses the default value
if [ -z "${vdialnum}" ] ; then vdialnum="*99#"; fi
export vstrapn=$(zenity --entry --text="Insert the APN" --entry-text="")
export vusername=$(zenity --entry --text="Insert the username\n\(Leave blank if not required\)" --entry-text="")
export vpassword=$(zenity --entry --text="Insert the password\n\(Leave blank if not required\)" --entry-text="")
echo "${vdevice} - ${vdialnum} - ${vstrapn} - ${vusername} - ${vpassword}" >> devices.conf
fi
if (( vfound==1 )) ; then
export vdialnum=$(cat devices.conf | grep '^'${vdevice} | gawk -F " - " '{print $2}')
export vstrapn=$(cat devices.conf | grep '^'${vdevice} | gawk -F " - " '{print $3}')
export vusername=$(cat devices.conf | grep '^'${vdevice} | gawk -F " - " '{print $4}')
export vpassword=$(cat devices.conf | grep '^'${vdevice} | gawk -F " - " '{print $5}') ;
fi
if (( vfound>1 )) ; then
cat devices.conf | grep '^'${vdevice} | gawk -F " - " '{print $2 " - " $3 " - " $4 " - " $5}' > temp1.tmp
cat temp1.tmp | zenity --list --text="BTDUN" --column="Select \"Dial number - APN - username - password\" to use:" > temp2.tmp
export vdialnum=$(cat temp2.tmp | gawk -F " - " '{print $1}')
export vstrapn=$(cat temp2.tmp | gawk -F " - " '{print $2}')
export vusername=$(cat temp2.tmp | gawk -F " - " '{print $3}')
export vpassword=$(cat temp2.tmp | gawk -F " - " '{print $4}') ;
fi
# Creation of the personalized BTconnection script
echo "ABORT BUSY ABORT 'NO CARRIER' ABORT VOICE ABORT 'NO DIALTONE' ABORT 'NO DIAL TONE' ABORT 'NO ANSWER' ABORT DELAYED ABORT ERROR" > ~/chatscripts/BTconnection
echo SAY \"Initializing\\n\" >> ~/chatscripts/BTconnection
echo "'' ATZ" >> ~/chatscripts/BTconnection
echo SAY \"ATE\\n\" >> ~/chatscripts/BTconnection
echo "OK 'AT+CGDCONT=1,\"IP\",\"${vstrapn}\"'" >> ~/chatscripts/BTconnection
echo "OK 'AT'" >> ~/chatscripts/BTconnection
echo "OK 'ATDT${vdialnum}'" >> ~/chatscripts/BTconnection
echo SAY \"Dialing\\n\" >> ~/chatscripts/BTconnection
echo "" >> ~/chatscripts/BTconnection
echo CONNECT \\d\\c >> ~/chatscripts/BTconnection
# Adding username and password into the peers script
sed -i 's/user `'.*'`/user `'${vusername}'`/g' ~/peers/BTconnection
sed -i 's/password `'.*'`/password `'${vpassword}'`/g' ~/peers/BTconnection
if ! [ -c /dev/ppp ]; then
mknod /dev/ppp c 108 0
fi
rfcomm bind 0 "${vdevice}" "${vchannel}"
pppd file ~/peers/BTconnection
rfcomm release 0
# Find if the bluetooth is still ON
# and will turn it OFF if it was disabled before the connection
INTERFACE="`hciconfig | grep "^hci" | cut -d ':' -f 1`"
if hciconfig "$INTERFACE" | grep UP &>/dev/null; then
if [ ${BTSTATE} = 0 ]; then
/usr/pandora/scripts/op_bluetooth.sh
fi
fi
#notify-send -u normal "Disconnected" -t 10000
zenity --info --text="Disconnected"
rm ~/temp*
# rm -r ~/chatscripts
# rm -r ~/peers
exit 0