O'Reilly Book Excerpts: Nokia Smartphone Hacks

Editor's note: If you enjoy this sample hack from Nokia Smartphone Hacks, be sure to check out the book. There are many more such hacks that will show you how to take full advantage of Nokia's powerful smartphones. Among the cool hacks the book covers are how to unlock your phone so that you can use it with any carrier, how to watch DVD movies on your phone, how to check your email and browse the Web, how to post to your weblog from your phone; how to transfer files between the phone and your computer, and much more.

Use the Python scripting language to develop small hacks for your Series 60 device.

While Java and Symbian C++ are powerful programming languages, their learning curves are too steep for most smartphone users. Most users do not need a full-blown programming platform to develop small hacks for their phone. A scripting language is the perfect tool to automate simple tasks and perform simple logical processing.

Python is a widely used scripting language in the computer world. It is easy to learn and supports object-oriented program construction. Nokia provides support for Python on most of its Series 60 devices.

TIP: Speaking of running scripting languages on Nokia Series 60 smartphones, there is an unsupported Symbian port for Perl 5.8x and 5.9.x. You can find the installation package and usage instructions from this mailing list posting from a Nokia engineer.

Install the Python Environment

The current Nokia phones do not come with the Python runtime environment preinstalled. You have to download and install Python yourself.

You can download the Python for Series 60 package from the Forum Nokia web site under the Series 60 Platform → Tools and SDKs category. The download package is a zip file with the .sis installation files, documentation, and example code. Make sure you read the Getting Started document in the download bundle to choose the correct .sis file for your phone. Then, you need to install the extracted .sis file to the phone, following the instructions in "Run Symbian Applications" [Hack #17]. The .sis file installs the following components to the phone:

Once the Python runtime is successfully installed, a Python icon appears in the Main menu.

Run Python Scripts

Click the Python icon to open it and then select Options → Run script. You should see a list of installed Python scripts and applications (see Figure 3-10). If this is the first time you've run Python, the scripts that appear will be the demo scripts from Nokia. You can try any of them. The filebrowser.py script is a good start. It allows you to browse the filesystem on the Series 60 smartphone, similar to what FExplorer does [Hack #20]).

Install Python Scripts

Of course, ultimately you want to install and run your own Python scripts on the device. To do that, you can put the Python script (with the .py filename suffix) on a web page and download it using the Services browser, or you can simply send the script to the phone via Bluetooth. The phone recognizes the Python script and prompts you to install it. Once the script is installed, it becomes available under the Python script list and you can run it from there. Figure 3-11 shows the process.

Figure 3-10
Figure 3-10. Running an installed Python script

Figure 3-11
Figure 3-11. Installing and running a Python script from a file sent via Bluetooth

The user-installed scripts are placed in the C:\System\Apps\Python\my\ directory (or in E:\System\Apps\Python\my\ if Python for Series 60 is installed on the MMC card; see Figure 3-12). You can delete those files to delete Python scripts.

Figure 3-12
Figure 3-12. The path to an installed Python script

Write Your Own Python Scripts

You can write your own Python scripts in any PC text editor and deploy them to the phone using the web or Bluetooth methods described earlier. But the key benefit of a scripting language is the ability to quickly prototype scripts. In fact, Python for Series 60 allows you to prototype Python scripts right there on the phone!

You can use the interactive_console.py script that comes with the Python runtime to open a text console for the Python interpreter (or, you can simply use the Options → Interactive console soft-key menu). Then, you can type Python statements from the phone keypad and see them run! Figure 3-13 shows the console executing several simple Python statements. The print statement and math formula statement generate output in the console. The statements at the bottom of the last image in Figure 3-13 display a Series 60–style UI dialog box.

Figure 3-13
Figure 3-13. Running scripts directly in the Python interactive console

Unlike full-blown programming languages such as Java and C++, which require setup code and noncode resources for even the simplest application, a scripting language such as Python actually allows you to write useful programs with only several lines of code. A great way to learn Python is through examples. The BigBold web community has a page for user-posted Series 60 Python examples (http://www.bigbold.com/snippets/tags/series60). There, developer Korakot Chaovavanich posted several useful snippets covering topics such as camera operation, Short Message Service (SMS) messaging, file exchange, and SQL database queries. Check them out, and share your own!

Write Series 60 Python Scripts on a Computer

Programming Python with the mobile-phone keypad is not pleasant. Luckily, the bt_console.py script bundled with Python for Series 60 opens a Bluetooth console on the device, which can be connected to a console program on a computer. The computer console sends everything it receives from the keyboard to the device via a serial port emulated over the Bluetooth connection. In turn, the phone console sends all the response data back to the computer console. This allows you to type Python commands on a regular computer keyboard and have them executed on the phone in real time. Very cool!

TIP: Series 60 Python scripts do not run in the regular Python environment on a desktop computer, since they use Nokia-specific Python libraries for UI, messaging, and multimedia operations. The Series 60 Compatibility Library (http://pdis.hiit.fi/pdis/download/) ports the phone library to computers and allows you to run Series 60 Python scripts on Windows, Mac OS X, and Linux computers. This library is developed by the Personal Distributed Information Store project at the Helsinki Institute for Information Technology.

On a Windows PC, you can use the console program bundled in the Nokia Python SDK to work with the Bluetooth Python console on the phone. Read the Nokia Python SDK documentation to see how it works.

The Nokia documentation does not cover how to run the serial console on Mac or Linux computers. But it is a straightforward process once you set up the Bluetooth serial port on the computer. Here are brief instructions for Mac computers, adapted from Erik Smartt's blog at http://www.eriksmartt.com/blog/:

  1. Set up an incoming RS-232 port over Bluetooth using the Bluetooth Serial Utility program in the Applications → Utilities folder. Give the serial port a name (e.g., PythonConsole) and remember it.

  2. From a Terminal window (Applications → Utilities → Terminal), you can use the screen /dev/tty.portname command to redirect that port to the terminal. portname is the name of the port you assigned in the last step (i.e., PythonConsole).

  3. On the phone, use the BTConsole application to search for available Bluetooth devices and choose your Mac from the list. You should see a Connected message in your Mac Terminal window, followed by some directions and a Python prompt.

After you are finished with console programming, you can press Ctrl-D on the Mac to exit the BTConsole and shut down the process on the phone.

View catalog information for Nokia Smartphone Hacks

Return to Python DevCenter