Tuesday, March 9, 2010

Writing a mobile phone app



Update (July 17, 2010) - the applet is now up to date with the new bus schedule from RTL.

So for a while now, i'd been wanting to write an app for my cell. In particular, some time ago i had written a Java applet which shows a countdown to the next bus leaving my workplace, so i know when to sprint out of the office in hopes of catching it. This could be a useful phone app, for obvious reasons.

Here is the applet, web browser style:




I dont have a particularly smart phone, its a Nokia 6275i, one of their Series 40 models. However, it does run Java. Despite the fact that i had bought the phone outright, not on a plan, my phone company (who shall remain nameless) had kept it locked down. With a little help from a friend, i managed to free it from their evil clutches using roughly this procedure.

Nonetheless, this just means i had to find a way to compile java apps for it. Such applications will utilize one of many different APIs for accessing the mobile device screen, etc. Different phones support different APIs, and which phone supports what does seem a bit confusing. However, there seems to be a bit of a standardization around the J2ME (vs J2SE which is Standard Edition, ME is Mobile Edition). Disappointingly, the newest J2ME is windows only. Blech.

However, i eventually found this article, and was able to get the Java Wireless Toolkit from Sun. This is basically one version back on J2ME and it runs just fine under Linux. The first step was to write a typical HelloWorld app, which was easy, i just copied it from the article. More difficult was to figure out which mobile device profile to use. Each profile determines the set of API functions available to you. By trial and error, i found that the JTWI, with CLDC 1.0 worked for my phone. For the reference of others - i recommend you figure out first which profile to use, as this determines the API available to you. If you develop for the wrong API, you may have to rewrite lots if the functions you used are not available on your phone.

What remained was to port the code from a web browser applet to something that ran on the phone - a MidLet. Theres a lot less functions available on the phone. The user interface functions are all different -not really surprising as the user interface is all different. Oddly many common functions are missing. Most annoying was the lack of String.format... but in the end that is not a very serious problem.

The phone has a relatively slow processor. My original applet was updating the time at 1/10th of a second intervals. Useless of course, but flashy. On the phone, updating this fast would lock the phone into a spasm of calculating. I had to pop the battery to get it to snap out of it. Updating at a more sedate pace of once a second - more than adequate, its a bus after all - made the applet more controllable.



The jar file from the phone can be downloaded here and the source code downloaded from here. I'm very interested if it runs on other peoples phones - do let me know in the comments.

You can also svn the code from assembla if you would like to help in the development.

svn co http://subversion.assembla.com/svn/NextBus

No comments: