Hemanth's Scribes

javascript

Android Remote Control Setup

Author Photo

Hemanth HM

Thumbnail

Control your Android devices while running scripts on your computer!

Wonder can be done with ASE and an android device along with scripting languages like Python, Perl, JRuby, Lua, BeanShell, JavaScript, Tcl, and shell.

Would it not be wonderful to run code on your box and the same being reflected on your android devices ?

The same is achieved with help of Scripting Layer for Android’s remote control.

Before one uses the script make sure that you have a GNU/Linux box with sudo privileges and an Android device with ASE installed on it.

The below is the simple setup script I wrote to make the process easier. #!/usr/bin/bash

# License : GPLv3

# Author  : hemanth.hm 

# Site    : h3manth.com

# Purpose : Remote control android setup process 
function check_sudo(){

    # Check if the user has sudo privilages 

    sudo -v >/dev/null 2>&1 || { echo $(whoami) has no sudo privileges ; exit 1; }

}

function setup(){

    # Check if the use have sudo privileges, if fine, move on.

    check_sudo

    # Get the latest version of android SDK.

    echo -e "Download and extracting the latest version of android sdk to /opt dir\
"

    (

        cd /opt

    

        curl -s $(curl -s "http://developer.android.com/sdk/index.html" | grep -o -E 'href="([^"#]+).tgz"' | cut -d'"' -f2) | tar xz && echo "Completed download of sdk" || echo "Unable to download the SDK!" 

        # exit has the download did not happen

        exit 1

    

        cd android-sdk-linux

    

        echo -e "\
Updating SDK and fetching tools..."

        tools/android update sdk -o --no-ui \\

        --filter platform,platform-tool,tool,system-image

        echo -e "\
Done updating!"

    

        echo -e "\
Appending PATH to have android sdk tools"

        echo "PATH=\"\\${PATH}:~/android-sdk-linux/tools\"" >> ~/.bashrc

    

        # Source ~/.bahrc so that the path is updated

        source ~/.bashrc

        # Start server

        sudo ./adb kill-server && sudo ./adb start-server

        echo -e "\
 Almost done, now start a public/private server on your phone. Note : If private server phone must be connected via USB."

        

        # As of now, not worrying about validating IP, cause the users who will be using this script will be sane enough to give the right IP :)

        read -p "Enter IP:PORT of the server which is running on your phone" FIP

        

        # Extract IP and PORT from the value read above.

        local IP   = $( echo FIP | cut -f1 -d":") 

        local PORT = $( echo FIP | cut -f2 -d":")

        

        # Port forwarding

        sudo ./adb forward tcp:9999 tcp:$PORT

       

        # Export host and port  

        [[ ! -z $IP ]] && export AP_HOST = $IP

        [[ ! -z $PORT ]] && export AP_PORT = $PORT

 

        # Get the site_package location

        site_package_path = $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")

        

        # Get SLA4's android.py

        echo -e "\
 Getting SLA4's android.py to $site_package_path"

        curl -s "http://android-scripting.googlecode.com/hg/python/ase/android.py" > $site_package_path/android.py

    )

}

echo -e “Starting the setup…

setup

echo -e “Done with the setup! import android and explore!”

  To setup your machine just do the below : sudo bash ( curl h3manth.com/rc4a )


** Now you can control your Android device from your terminal, using ASE API **

For example, the below code would sent a toast to your Android device from your terminal : 

```javascript
$ python
Python 2.6
Type "help", "copyright", "credits" or "license" for more information.
>>> import android  # The SL4A android.py module should be on your sys.path.
>>> droid = android.Android()  # Or android.Android(('Public IP', PORT)) 
>>> droid.makeToast("Hello from my GNU!")

FORK it from https://github.com/hemanth/android-remote-control-setup

** Enjoy the power of free and open software! **

#javascript#python#ruby
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.