12/29/2011

Remote Android Scripting with Python

In a previous post, Android Scripting was described. Once you have established the ability to use the Android Scripting Layer for Android (SL4A) on your device, you may be frustrated trying to type your scripts on your device, especially if you have a soft keyboard.

Fortunately, if you have installed the Android Software Development Kit (SDK) on your computer, there are several options. Below is my summary of some of the techniques described at http://code.google.com/p/android-scripting/wiki/RemoteControl. Being a big fan of Python, the interpreter I chose to use was python. This required this android.py file to be downloaded either into my current directory, or added to my PYTHONPATH.


1.  You can type up your scripts on your computer and then upload them, or push them to your device. The default directory to place SL4A scripts on your device is /sdcard/sl4a/scripts. If you place your scripts there, then they will show up in your device's SL4A app when you start it. An example of the command to upload a script is: adb push /bin/sl4a/speak.py /sdcard/sl4a/

2. You can connect to your device with USB, and start a private server on a port on your device. You can then forward a port from your computer to that port on your device. This allows you to connect from an interpreter running in an interactive shell on your computer to the SL4A server running on your device.
  • Connect the USB cable to your device
  • Enable USB debugging on your device (Menu, Settings, Application Settings, Development)
  •  Start the SL4A server
    • adb shell  'am start -a com.googlecode.android_scripting.action.LAUNCH_SERVER -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher'
    • Make note of  the port number in the SL4A device notification. Unfortunately, the method of specifying USE_SERVICE_PORT to set the port failed to work for me and others
  • Forward a local open port to the port that was found. Assuming 9999 is an available port on your computer and port 43917 was displayed in the device notification, you would use: adb forward tcp:9999 tcp:43917
  • Set or export the AP_PORT variable to your computer port: export AP_PORT=9999
  • Start an interpreter compatible with one installed in the SL4A environment in your device like Python.
  • python
Python 2.7 (r27:82500, Aug 07 2010, 16:54:59) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import android
droid = android.Android()
name = droid.dialogGetInput('What is your name?')
print name.result
droid.makeToast('Hello ' + name.result + '!')

3. You can also connect your device to a network shared by your computer and start a public server on a port on your device. You can then forward a port from your computer over the network to that port on your device. This allows you to do something very similar to previous example.

  • Connect your device to a network shared by your computer. Assuming that it is a wireless network, use Menu, Settings, Wi-Fi Settings and enable Wi-Fi. Now, connect to one of the networks by tapping on it, and authenticating if necessary. After it has connected, tap on it again to check the IP address. This example assumes the address was found to be 192.168.1.83.
  • Start the SL4A server and make note of  the port number in the SL4A device notification.:
    • If you are connected using adbWireless Tether or something similar run: adb shell 'am start -a com.googlecode.android_scripting.action.LAUNCH_SERVER -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher --ez com.googlecode.android_scripting.extra.USE_PUBLIC_IP true'
    • If you are not connected, then on the device, type in a terminal or  execute a script which contains:
    • am start -a com.googlecode.android_scripting.action.LAUNCH_SERVER -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher --ez com.googlecode.android_scripting.extra.USE_PUBLIC_IP true
  • Set or export the AP_PORT variable to the device port: export AP_PORT=52177
  • Set or export the AP_HOST variable to the device IP: export AP_HOST=192.168.1.83
  • Start an interpreter compatible with one installed in the SL4A environment in your device like Python.
  • python
Python 2.7 (r27:82500, Aug 07 2010, 16:54:59) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import android
droid = android.Android()
# if AP_PORT and AP_HOST not set use "droid = android.Android(('192.168.1.83', 52177))"
name = droid.dialogGetInput('What is your name?')
print name.result
droid.makeToast('Hello ' + name.result + '!')

12/26/2011

Android Scripting

The Scripting Layer for Android (SL4A) Project is not in Google's Android Market but it can be downloaded to your Android device to allow you to use multiple scripting languages. Warning, a quote from the SL4A site: "SL4A is designed for developers and is alpha quality software." 

An impressive list of languages are available: LuaBeanShellPythonPerlJRubyTclJavaScriptRubyShell.

Whatever code you write can automatically be executed with TaskBomb using the SL4A Script Launcher.

For support, there's a google group for that: http://groups.google.com/group/android-scripting/.

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.