Posts Tagged ‘ sl4a

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

Installing the Android Scripting Environment on the Android Emulator

Update:  I have a newer post here describing how I got the SL4A installed on an actual phone.  More work than I expected…

Based on my previous post, the thought of being able to run Python on an Android Device via the Android Scripting Environment seems… enticing.  However, I personally do not own an Android Device, nor have plans to get one anytime soon (ah, love those AT&T contracts…).  But there is the Android Emulator which I can run on my PC, why not use that in the meantime?

What I expected to be easy turned into quite a quest.  But I got it working, and the steps are listed below.  Specifically, this is a walkthrough on installing the Android SDK, creating an ‘Android Virtual Device’, launching the Android Emulator, installing the appropriate scripting language data on it, and running that language on the emulator.  This was done on a WinXP system.  I should also point out that this was my first foray into anything Android related, so I am quite a bit of a noob at this point.  My knowledge does not stretch far beyond this tutorial :-)

Finally, while my goal was to get Python running, you can use these steps to (presumably) get other languages (Lua, Perl, jRuby, BeanShell, Rhino) installed as well.  My tutorial below will be based around Python and its associated dependencies, but you should be able to mentally replace that with the language of your choice.

#1: Install the Android SDK:

Based on this post, I got the SDK installed.  I’d recommend you follow it as well.  You can stop at their “Step6: Create a new Android Virtual Device”, since that’s what we’ll cover below.  Note that this install process was done with version Android v2.0, can’t vouch for earlier versions.

  • It ended up being installed here:  C:\android-sdk-windows
  • Not sure of the importance, but I read online that you need to add a Windows System Variable named ‘SDK_ROOT’ with the value of the tools dir in the SDK install location, which for me is ‘C:\android-sdk-windows\tools’

#2: Create an Android Virtual Device (AVD):

  • I first created a shortcut on my desktop to this bat file which I use to launch the ‘Android SDK and AVD Manager’:
    • C:\android-sdk-windows\tools\android.bat
  • Under ‘Virtual Devices’, I created a new AVD, named it ‘ASE_test’.  Specifics in the image:
  • createNewAVD
  • After creation, pick the AVD in the ‘List of existing Android Virtual Devices”, and “Start…”
  • This will launch the Android Emulator:
  • emulatorScreen
  • Getting closer…

#3: Install the Android Scripting Environment (ASE):

It’s important that the Android Emulator is open (& unlocked) and running while the below steps are performed.

I read a lot of documentation about making a disk image of a memory card that the AVD could load in place of a real one.  I spent two hours trying to get this to work… it just didn’t happen.  Then I learned that you can install the ASE via the ‘Android Debug Bridge’ (adb) in a shell, and everything got a lot easier…

  • Download the ASE, and appropriate scripting language data.  Find them at the ‘Android Scripting’ download page here:
  • These files specifically: (note: since the time of authoring this post, the below apk and zip files have been updated… be sure to grab the latest versions)
  • After downloaded, for convenience, copy them to your SDK\tools dir:
    • C:\android-sdk-windows\tools
  • Install the ASE:
    • Make sure the Android Emulator is up and running (& unlocked).
    • In an OS shell, change directory to the SDK\tools directory.
    • Execute these commands to install the ASE on the emulated Android Device (running in the background):
      • C:\android-sdk-windows\tools>adb install ase_r14.apk
    • Then, use these commands to copy the Python data to the virual sdcard on the emulator:
      • C:\android-sdk-windows\tools>adb push python_scripts_r0.zip  /sdcard
        C:\android-sdk-windows\tools>adb push python_extras_r0.zip  /sdcard
        C:\android-sdk-windows\tools>adb push python_r0.zip  /sdcard
    • That should do it!  Your shell should look something like this when complete:
      • C:\android-sdk-windows\tools>adb install ase_r14.apk
        570 KB/s (127785 bytes in 0.218s)  pkg: /data/local/tmp/ase_r14.apk
        Success 
        
        C:\android-sdk-windows\tools>adb push python_scripts_r0.zip  /sdcard
        133 KB/s (4280 bytes in 0.031s)  
        
        C:\android-sdk-windows\tools>adb push python_extras_r0.zip  /sdcard
        502 KB/s (2460097 bytes in 4.781s)  
        
        C:\android-sdk-windows\tools>adb push python_r0.zip  /sdcard
        360 KB/s (1726792 bytes in 4.671s)

#4: Configure the ASE:

At this point, the ASE should be installed in the running Android Emulator, and the Python data (or whatever scripting language you’re using) copied to the virtual SD card.

  • Expand the Application Tab.  You should see the “ASE” icon.  This means a successfull .apk install!
  • aseInstalled
  • Select the ASE icon.  This will launch the Android Scripting Environment, and give you a screen saying “Start adding scripts and Interpreters by pressing the menu button”.  We need to install the Python interpreter, so, press “MENU”.
  • Along the bottom of the UI three buttons will pop up.  Press the middle one “Interpreters”.
  • This will clear the screen with only one option; “Shell”.  Press the “MENU” button again.
  • New buttons along the bottom, press “Add”
  • AH, a big list of stuff to install!  Scroll down to ‘Python 2.6.2’ and press it.
  • This will start an extraction process, and you should see it extract the various Python .zip files you had put on the virtual SD card earlier.
  • Now the UI has two options, “Shell” and “Python 2.6.2”.  Press Python…
  • This launches a Python shell.  Success!
  • pythonShell
  • Hitting the return button twice will take you to another menu that has all the default Python modules previously installed: notify_weather.py, saychat.py, etc.  Press ‘test.py’:
  • This will run that module, executing a variety of test suites on the emulated device.
  • Pressing menu can take you into a module edit mode as well:
  • testPy
  • That’s pretty cool… 😛

In Conclusion…

What to do now?  First step, will be to go over all this info on the ‘Android Scripting Groups Wiki’:  http://code.google.com/p/android-scripting/w/list

I have to say, interaction in the emulator itself  is really slow, with lots of lag.  I also had a weird issue where all the text I tried to type was being converted into Japanese.  I had to go into my emulator’s Settings -> Language Keyboard -> And uncheck the first two options under “Text settings”.  I don’t know what they are, since they’re in JAPANESE.  But that fixed it.

Thus, it begins.

Index of reference pages:

These are pages I pulled a lot of info from to figure all of this out: