Run Python Scripts on Your Nokia Smartphone
Pages: 1, 2
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
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/:
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.
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).
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