Easytether Bluetooth Script

nurfitri
Easytether Bluetooth Script

Introduction

I've been using Easytether for quite some time to tether mobile data from my smartphone to my Linux computer. Usually, I just use Easytether-USB, but sometimes it is more convenient to use Bluetooth tethering so I don't need to constantly plug my phone into the computer using a USB cable.

If you are an Easytether user and a novice Linux user like me, you probably experience this every time you want to connect your phone via Bluetooth tethering:

You are required to find out the MAC address of your connected phone from the Bluetooth GUI interface:

You first need to open up a terminal and write "easytether-bluetooth <Your MAC address>".

I am too lazy to constantly write those commands, so I decided that maybe I could write a script for that...

The procedure

To use Easytether-Bluetooth, I usually follow these steps:

  1. Find my phone's 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 is already running and I need to kill it and re-run that command.

The script

I think my script is straightforward.

#!/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 involves finding my device's MAC address using hcitool. hcitool is a Bluetooth configuration tool, and you can read more about it here.

So, what I did was use hcitool con to display active connection info (my smartphone that is already connected to the PC) and use grep with regex to extract only the MAC address of my connected smartphone and store it 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 is 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
    }

The bottom part of the code simply checks if my phone's 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

The next part is just making the script executable and using it.

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

This script may not be perfect, but it gets the job done. I am not that experienced with Linux and writing bash scripts, but I learned a lot while researching how to write this script. I hope others can learn something from this too.