Posted by: Yanyan | December 9, 2013

SL4A working with Seattle Repy V2

In the past few weeks, we got Python and SL4A (scripting language for Android) working with Repy V2. Here are some steps and script provided by Albert. Our purpose is to use the SL4A API in Repy V2 to interface Android sensors. See this link for an API overview and help.

  1. On your shell, specify the AP_PORT for SL4A to listen on, e.g., export AP_PORT=45678
  2. Start SL4A in server mode using am, the Activity Manager, on that port (CAUTION: That’s one long line!) am start -a com.googlecode.android_scripting.action.LAUNCH_SERVER -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher –ei com.googlecode.android_scripting.extra.USE_SERVICE_PORT $AP_PORT
  3. Finally, run the readout program and supply it with the AP_PORT, e.g., python restrictionsfile test_sensors.repy $AP_PORT

Here’s a very basic example Repy V2 code you could run. It connects to the configured AP_PORT, sends a JSON request to vibrate the phone, and receives the sensor result. (No attempt is made here to construct/parse JSON in a reusable way YET.) Bear with me spaces don’t work with wordpress..

if callfunc==”initialize”:
ap_port = int(callargs[0])
# Not castable (-> ValueError) or missing at all (-> IndexError)
# That’s the user’s fault! Teach them!

# Connect to SL4A, do some random test.
sl4a_socket = openconnection(“”, ap_port, “”, 12345, 5)
jsonstring = ‘{“params”: [], “id”: 100000, “method”: “vibrate”}\n’

bytes_sent = 0
while bytes_sent<len(jsonstring):
bytes_sent += sl4a_socket.send(jsonstring[bytes_sent:])
log(“Sent”, bytes_sent, “bytes.\n”)

while True:
except SocketWouldBlockError:
except SocketClosedRemote:
log(“The remote side closed the connection. Exiting.\n”)


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s


%d bloggers like this: