Easytether Bluetooth Script

nurfitri
Easytether Bluetooth Script

Introduction

I've been using easytether for quite some times, to tether mobile data from my smartphone to my linux computer. Usually I just use the easytether-usb but sometimes it is more handy to use the bluetooth tethering and I don't need to constantly plug in my phone to the computer using usb.

If you are an easytether user and a noob linux user like me, you probably experience this everytime you want to connect your phone via bluetooth tethering,

you are requires to find out the Mac Address of your connected phone from the bluetooth GUI interface:

you first need to > open up terminal > write "easytether-bluetooth <-Your Mac address->" .

I am too lazy to constantly writing those command so I decided that maybe I can write a script for that ....

The procedure

To use easytether-bluetooth, I usually follow these steps:

  1. find my phone bluetooth mac address
  2. connect to it using easytether-bluetooth xx:xx:xx:xx:xx:xx where xx:xx:xx:xx:xx:xx is my mac address
  3. sometimes easytether already running and I need to kill it and re run that command.

The script

I think my script is straight foward.

#!/bin/bash

# myMac=$(hcitool dev | grep -o "[[:xdigit:]:]\{11,17\}")
devMac=$(hcitool con | grep -o "[[:xdigit:]:]\{11,17\}")

echo "PC addr- $myMac"

function blt_connect {
    #check if easy-tether already running 
    if grep -q "easytet+" <<< $(ps -aux) ; then
        echo "easytether already running in background"
        echo "Killing existing process"
        sudo kill $(pgrep easytet+)
        echo "reloading connection.."
        sudo easytether-bluetooth $devMac
    else
        echo "Trying to connnect"
        sudo easytether-bluetooth $devMac
        exit
    fi
}


if grep -q "[[:xdigit:]:]\{11,17\}" <<< "$devMac"; then
    echo "Connected Device- $devMac .."
    echo "connecting tether using bluetooth..."
    blt_connect
else
    echo "Bluetooth Device Not Connected"
fi

The script breakdown

The first part me finding my device mac address using hcitool. hcitool is bluetooth configure connection tools and you can read more here.

So, what I did was I use hcitool con to display active connection info (my smartphone that already connected to the pc) and use grep regex to take only the mac address of my connected smartphone and store in a variable $devMac.

    #!/bin/bash

    # myMac=$(hcitool dev | grep -o "[[:xdigit:]:]\{11,17\}")
    devMac=$(hcitool con | grep -o "[[:xdigit:]:]\{11,17\}")

Then I use grep and ps -aux to check if easytether previously running in the background, if true, I simply kill it and then run easytether-bluetooth,

    function blt_connect {
        #check if easy-tether already running 
        if grep -q "easytet+" <<< $(ps -aux) ; then
            echo "easytether already running in background"
            echo "Killing existing process"
            sudo kill $(pgrep easytet+)
            echo "reloading connection.."
            sudo easytether-bluetooth $devMac
        else
            echo "Trying to connnect"
            sudo easytether-bluetooth $devMac
            exit
        fi
    }

Bottom part of the code is just me simply checking if my phone bluetooth is actually connected to my pc before running easytether-bluetooth

if grep -q "[[:xdigit:]:]\{11,17\}" <<< "$devMac"; then
    echo "Connected Device- $devMac .."
    echo "connecting tether using bluetooth..."
    blt_connect
else
    echo "Bluetooth Device Not Connected"
fi

next part is just making the script executable and use it.

nurbxfit@jun~: chmod +x easytether-bluetooth.sh

This is actually not a great script, but I am able to get the job done with it.

I am not that experience with linux and writing bash script, but I am able to learn a lot while researching on how to write this script. I hope others can learn something from this too.