Command your shiny Android in Python

I am the happy owner of an Android phone. I love it. But what I love even more, is rolling some sweet Python on my precious.

In case you have a vanilla phone like I do, you will need to follow these steps:

  • download and install the Android SDK for your platform
  • add the SDK tools to your system path, for instance on Windows run this in a shell:
> set PATH=%PATH%;path_to_android_sdk\tools
  • activate USB debugging on your phone (Parameters > Applications > Development > USB Debugging)
  • setup your system to detect your device as explained here (on Windows, it means installing the usb driver from the SDK)
  • check that your phone is detected correctly by running adb get-state (if everything is ok, the result should be ‘device’)
  • check out the Android Scripting Environment svn repository on your computer (see this page)

Your system is almost configured, let’s install the necessary applications on the phone:

  • install the Android Scripting Environment on your phone
  • (optionally) install the Text-to-Speech library (by Charles Chen) on your phone if you want to use it in your scripts, via the Android Market
  • run ASE on your phone, type menu and start a Python shell, you will notice a line like ‘export AP_PORT = “49508”‘.
  • setup port forwarding on your system with the value you just noted:
> set AP_PORT=49508
> adb forward tcp:%AP_PORT% tcp:%AP_PORT%
  • go to the ASE directory on your computer containing the android.py module
> cd path_to_ASE\python\ase
  • run your favorite Python 2.6 interpreter and enjoy the magic:
> python
Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid = android.Android()
# now your phone is ready to acknowledge your awesomeness
>>> droid.speak('hello, my master')
{u'result': None, u'id': 2, u'error': None}
# or alternatively, if you don't have the TTS library installed:
>>> droid.makeToast('hello, my master')
{u'result': None, u'id': 1, u'error': None}

Now you have all the information you need to start exploring the API and roll  more interesting examples.

droid.makeToast

Advertisements

5 thoughts on “Command your shiny Android in Python

  1. Thank you for your guide.

    However, the command only works on Windows:
    set AP_PORT=XXXXX
    adb forward tcp:%AP_PORT% tcp:%AP_PORT%

    For Linux and perhaps OSX, it should be:
    export AP_PORT=XXXXX
    adb forward tcp:$AP_PORT tcp:$AP_PORT

  2. Good tips if you’re not familiar with the SDK. Thanks.

    It’s worth noting that you only need `android.py` on your system, rather than the entire ASE checkout. Be sure to grab it from the tag that reflects the version of ASE installed on your phone.

  3. 1. I see, that in Python, started from ASE, there also is a variablle AP_HANDSHAKE with some hashcode.
    2. When following this tutorial, seems, that the connection is established, by later not authenticated:
    >>> import android; droid = android.Android()
    java.lang.SecurityException: Authentication failed!

    I think 1 and 2 are connected. How do i get & escape need in AP_HANDSHAKE ?

    1. I am not a expert but this is how I deal with the problem
      Set 1 (on emulator/Phone)
      1) Run the python interpreter inside the Droid Emulator/Phone/PDA (SLA4 –> Menu –> Interpreters –> Python )
      2) Run following commands from the interpreter shell inside the emulator/Phone/PDA
      import os
      print “export AP_HANDSHAKE=%s\n” % os.environ.get(“AP_HANDSHAKE”)
      print “export AP_PORT=9999\n”
      print “adb forward tcp:9999 tcp:%s\n” % os.environ.get(“AP_PORT”)

      Set 2 (on Linux machine)
      1) Open a command line prompt on a Linux
      2) Cut paste the command generated in step (3) above into this command prompt
      3) Run python or eclipse from this command prompt. You should not get the authentication issue anymore

      Actually I have partly automated the above process

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s