Android Adventures, part 2

Go to part 1, part3…

Based on the success of my previous post covering how I got my Android phone (Samsung Captivate) rooted, the “Scripting Language For Android” installed with Python, it was time to get Processing working on it.

Of course, it wouldn’t go easy or smoothly…

A.  Install Processing For Android & Dependencies

Following the ‘Instructions‘ sections on the “Processing For Android” page, I installed:

B.  Try Out Some Code

I started with these two tutorials, trying to walk through them to get the code working:

I immediately ran into problems…

C. Troubleshooting:

First problem:

Couldn’t get the Android emulator to run any of the example code.  The first  error I got  started like this:

Importing rules file: tools\ant\ant_rules_r3.xml
BUILD FAILED
G:\android-sd\tools\ant\ant_rules_r3.xml:336: Error running javac.exe compiler

Websearching, I found someone else with the exact problem here.  On that thread a fix was posted which linked here.  Looking at that suggestion, I ended up doing these actions, which fixed the emulator problem:

  • I added all of these dirs to my PATH system variable (Windows XP):
    • C:\android-sdk-windows;
    • C:\android-sdk-windows\tools;
    • C:\Program Files\Java\jdk1.6.0_21\bin;
  • I added a ‘ANDROID_SDK’ system variable, pointing it to ‘C:\android-sdk-windows’ dir.
  • I had also previously added a ‘SDK_ROOT’ system variable pointing to ‘C:\android-sdk-windows\tools’ dir.

I should point out that maybe not all of those steps needed to be done, but after I did it all, I could finally get my sketches to load on the Android Emulator.

Second Problem:

When I tried to load the sketches on the phone itself via “Presentation Mode” in Processing, I got a new error:

“Device time out”
“Device killed or disconnected”

The computer wasn’t seeing the phone.  In a nutshell:  I had to switch the phone’s USB mode to “Enable USB Debugging”.  By doing that, the computer once again failed to recognize the phone  since  the phone seemed to be in a ‘new usb mode’.  Unplugging it and reconnecting it asked for me to reinstall the USB drivers again, twice in a row.  But once the drivers updated, I was able to get the sketches loaded on the phone.  Success!

D.  Try Out Some Code (Again)

I was then able to go through both the above tutorials getting the code running on both the emulator, and on my phone itself.  The examples are simple, but they look amazing on that screen.

I can’t wait to start writing some real code and getting it up on the phone.  And I still need to figure out how to actually save it on the phone, and run it later… 😉

Update:

So, the next day I found out that Processing installs the sketches on the phone when you run them.  I found them sitting in the Applications menu, named after the sketch that made them.  I just hadn’t noticed when I initially loaded them.   Nice!

Go to part 1, part3…

Speech 2 Text 2 Speech

Hot off the heels of my previous post on getting Python scripting on my Android phone via the “Scripting Layer For Android“, I wrote my first (silly) Python module directly on the phone:  I’ve coined it ‘Speech 2 Text 2 Speech‘:  You speak into the phone, it converts it to text, then says it back.  And, it’s sooo easy to do:

# speech2text2speech.py
import android
droid = android.Android()
speech = droid.recognizeSpeech("Talk Now", None, None)
print speech[1]
droid.ttsSpeak(speech[1])

Sometimes the “Talk Now” window pops up too fast and you have to reset it, but when it works it’s pretty funny to hear what you said repeated back in ‘Androidish’ 😉

Android Adventures

Go to part 2…

I finally got a smartphone:  Samsung Captivate.  Why?  Mainly since the bar for making apps on them seems pretty low, which is something I’ve wanted to try for some time.  Plus after seeing these two posts…

And their reference to Processing For Android

…and considering my love for Processing
…it just all seemed to make sense.

The first thing I wanted to do though had nothing to do with Processing:  I wanted to get the ‘Scripting Layer For Android‘ (SL4A) installed on it so I could run Python (which I love more than Processing), in a shell, on my phone.  I documented how to do this on the Android Emulator in a previous post (and waxed lyrical on how I’d like to program on an Android phone in a post before that). However, to get all this working on a real phone, I had to jump through some hoops.

Disclaimer:    I’m a total smartphone\Android noob, and this is the first time I’ve ever written words like  ‘sideload’ or ‘root’.  I did this a day after getting the phone, never having owned a smartphone before.  So for most of  it I was just fumbling in the dark.  There may be easier ways, but I had a terrible time finding any kind of documentation on anything.  Any documentation I did use is linked to below.

A.  Time To Root:

AT&T in its infinite wisdom has disallowed ‘sideloading’ of apps onto the phone:  You can’t copy something onto the internal SD card and ‘install’.  If you try, you get a nice “You can only install apps from the Marketplace” window pop up.  Meaning, I can’t install SL4A  (or if you can, I just went through a lot more steps than needed…).   Talking with my buddies who’s smartphone kung-fu is much more powerful than my own, they told me to look into ‘rooting’ the phone:  Give me ‘root access’ on the phone I just bought (shouldn’t everyone have that?  I must have missed the politics class on that one).  This will allow me to be able to ‘sideload’ apps.

After a lot of websearching, I came across this form post, which describes how to install the “Unleash the Beast” root tool.  It seemed to do everything I needed it to in one step:  Clean off all the bloatware that came with the phone, disable the annoying on\off sound fx, plus a bunch of other stuff.  Most importantly though it gives you root access and enable sideloading.  I followed the directions on the site, and it worked flawlessly.  Not to say it didn’t stress me out during the process :)

Excellent, root complete!

B.  What happened to my USB?

Loooong story short:  When I first got the phone and before the root, my XP laptop wouldn’t detect it.  Went online to the Samsung web site to get drivers:  Site claimed it had drivers, but when I went to the download page:  “This product has no downloads”.  I contacted customer support explaining the issue, and their reply was to do the exact thing I had just done.  Robot.  More websearching:  Based on this post, I got a link to the drivers, and installed:  the  machine could now see my phone.  Win.

After the root, when I connected phone to computer, the computer again failed to detect it.  Tried plugging it into a different machine:  Same result.   The phone came with no directions, and the web page for downloads claiming it had the manual didn’t work (like mentioned above).  More websearching:

  • Settings -> Applications -> USB Settings -> Set to “Ask on connection” (was ‘Kies’).

Now when I plug the phone into the computer, the phone asks me ‘how it should be connected’:  Set to “Mass storage” (not Kies).  Then access the notifications menu (that one that scrolls down from the top), click on the “USB connected” item, and then “Mount”.  Suddenly the computer detects it.  What a pain…

C.  Get Astro File Manager

Part of  executing “Unleash The Beast” was the removal of the built-in “My Files” app.  To install the new tools I’d be getting I needed a new file manager:  Recommended in several places was “Astro File Manager“.  Successfully installed from the marketplace.

D.  Install SL4A with Python:

From the Android Scripting download page, I downloaded both sl4a_r2.apk and python_for_android_r1.apk.  After connecting the phone to computer and jumping through all the USB hoops, I copied them both to the \Android dir (I have no idea if that’s the right place).  Unmounting the the usb drive from the phone’s menu, I then accessed Astro:  Browsed to the /sdcard/Android dir, selected ‘sl4a_r2’ -> Open App Manager -> Install.  I then repeated this process on ‘python_for_android_r1’.  While python was installing, it also downloaded and installed all the other Python .zip files found in the SL4A download page as well.

When it was complete, I had the nice ‘SL4A’ app  ready for usage

Note: In hind-site I found the official doc for “Installing Interpreters” which saves you from having to install the Python shell by hand.  Main problem is the menu for his app on the phone isn’t that intuitive, and I totally missed how to do it the first time around.

Update: A buddy of mine did this, his install was even easier:  On his phone he simply browsed to the SL4A page, and clicked on the bar-code:  It installed automatically.  Magic.

E.  Do something with it:

Launch it ->  Settings Menu -> View -> Interpreters -> Python 2.6.2 ->  the shell opens:

>>> import this

Continue to Part 2

Some blog cleaning…

Still wrapping my head around the whole WordPress blog thing:  I’d got hundreds of ‘spam users’ signed up on the blog, and finally found a plugin that will go through a user confirmation process. And honestly, there’s really no reason (that I’m aware of) to be a ‘user’ of this blog anyway, since there’s no actions you can perform, and you don’t need to be a user to post a comment or get the RSS feed. So anyway, all users have been deleted.

I wasn’t able to go through the entire list and see if each one was actually a valid user.  So if you were a user and you still want to be, just register again, and I can approve you.

Basic Pygame Program

Whenever I begin a new Pygame application, I always start with a ‘basic program’ to begin things. Saves me from having to re-type all the simple stuff. Obviously a Pygame program can get far more complex than this, but everything has to start somewhere. Thought I’d post it up for reference.

"""
default.py
www.akeric.com - 2010-09-07
Default, base, initial setup for a pygame program.
In this case, black background, white circle follows mouse position, and
framerate is shown in the title-bar.
"""

#-------------------------------------------------------------------------------
# Imports & Inits
import sys
import pygame
from pygame.locals import *
pygame.init()

#-------------------------------------------------------------------------------
# Constants
VERSION = '1.0'
WIDTH = 512
HEIGHT = 512
FRAMERATE = 60

#-------------------------------------------------------------------------------
# Screen Setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
bgCol = Color('black')
clock = pygame.time.Clock()
moduleName = __file__.split('\\')[-1]

#-------------------------------------------------------------------------------
# Define helper functions, classes, etc...
def spam():
    pos = pygame.mouse.get_pos()
    pygame.draw.circle(screen, Color('white'), pos, 32)

#-------------------------------------------------------------------------------
# Main Program
def main():
    print "Running Python version: %s"%sys.version
    print "Running PyGame version: %s"%pygame.ver
    print "Running %s version: %s"%(moduleName, VERSION)
    looping = True

    # Main Loop-----------------------
    while looping:
        # Maintain our framerate, set caption, clear background:
        clock.tick(FRAMERATE)
        pygame.display.set_caption("%s - FPS: %.2f" %(moduleName,clock.get_fps()) )
        screen.fill(bgCol)

        # Detect for events-----------
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                looping = False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    looping = False

        # Do stuff!-------------------
        spam()

        # Update our display:---------
        pygame.display.flip()

#-------------------------------------------------------------------------------
# Execution from shell\icon:
if __name__ == "__main__":
    # Make running from IDE work better:
    sys.exit(main())