Wiley.Mobile.Python.Rapid.prototyping.of.applications.on.the.mobile.platform.Dec.2007 (779889), страница 6
Текст из файла (страница 6)
Have fun with it!INSTALLING PYTHON FOR S60 ON 3RD EDITION DEVICESFigure 2.3 File manager(a)(b)Figure 2.4 Python running on a phone1718GETTING STARTEDFigure 2.5Our script running on the phone2.1.3 Installation for Mac OS X Users1. Install the downloaded .SIS files to your phoneConnect your phone to your computer using the USB cable, then select‘Data transfer’ or ‘Mass storage’ mode on the phone screen. The memorycard of the phone is mounted as an external ‘hard drive’ and its contentcan be accessed by the Finder application on the Mac. You must havethe memory card inserted into your phone.Copy the downloaded installation files to any folder on the memorycard of your phone, for example, to the root.
Safely remove (un-mount)the phone’s drive from your computer in the same way as with anyexternal hard drive.On the phone, open the File Manager application (usually found onthe desktop of the phone or inside a subfolder e.g. ‘Tools’) and go tothe directory of the memory card where you have stored the installationfiles. Click on the installation files. First, install the PyS60 interpreter(PythonForS60) and then the user interface for the PyS60 interpreter(PythonScriptShell). Follow the on-screen instructions on the phone – forinstance, select ‘Yes’ if a security warning appears on the phone screen.Also, make sure the date is set correctly on the phone.After the installation is complete, your phone shows the Python iconon the desktop or inside one of the desktop folders (Figure 2.1).
Yourphone is now ready to execute Python scripts. Let’s prepare a ‘Helloworld’ script.INSTALLING PYTHON FOR S60 ON 3RD EDITION DEVICES192. Write a Python ScriptYou can write a Python script on your computer with any text editor.Useful editors are, for example, SubEthaEdit, TextMate or BBEdit. We donot recommend using Mac’s TextEdit application, as it might place someinvisible characters in your code which throw an error when executingthe script.Write the following line in your text editor:print "Hello world!"After the code is typed, save the file under the name hello.py. Makesure that the file ending is .py and not .txt.The file is now ready to be executed on your phone! You do not haveto build or compile it any way.
However, it must be copied to the phonefirst.3. Upload a Python Script to a PhoneAgain, connect the phone to your computer as an external ‘hard drive’.Create a folder called Python on the phone’s memory card (drive E:)using the Mac’s Finder application. Copy the hello.py file from yourcomputer to the E:\Python folder on the phone.Safely remove (un-mount) the phone as an external hard drive fromthe computer.
You can now test the script.If your Mac has built-in Bluetooth, you can use it to upload thehello.py file to your phone. Click on the Bluetooth icon at the topof the Mac screen (see Figure 2.8), select ‘Browse device’ and chooseyour phone’s nickname from the list (Bluetooth must be switched onon the phone). Then select ‘Browse’ to open a window showing thephone’s memory card E:\. Drag and drop hello.py from the locationin your Finder application to the E:\Python folder in the newly openedwindow. This saves you having to use the USB cable and mount andun-mount the phone as an external ‘hard drive’.4.
Test a Python ScriptStart the PyS60 interpreter by clicking on the Python icon (Figure 2.1) onthe desktop or inside the appropriate subfolder on your phone. Once thePyS60 interpreter has started up, select ‘Options’ (Figure 2.4(a)) and ‘Runscript’ (Figure 2.4(b)). Choose your script name, e:hello.py, from thelist and select ‘OK’.
Your script should now start up and you should seea green line stating Hello world! (Figure 2.5).Congratulations! You have successfully written and executed yourscript with PyS60! To go through all the examples in the book, just repeatsteps 2–4 for each new script. Have fun with it!20GETTING STARTED2.1.4 Installation for Linux UsersWe assume that your computer has either a built-in Bluetooth capabilityor an external Bluetooth dongle. Also, we assume that Bluetooth isdetected correctly by your distribution and you have the obexftp andhcitool programs installed.
In many distributions, they can be installedfrom packages obexftp and bluez-utils.1. Install the downloaded .SIS files to your phoneFirst, enable Bluetooth on your phone. Then, on the command line,execute the following command:hcitool scanThis performs Bluetooth scanning. You should see the name of yourphone in the list, with its Bluetooth address (e.g., 00:17:ED:AC:56:FE).Next, you can upload the SIS files to your phone with the followingcommands:obexftp -b 00:17:ED:AC:56:FE -c E: -p PythonForS60...SISobexftp -b 00:17:ED:AC:56:FE -c E: -p PythonScriptShell...SISReplace the Bluetooth address above with your phone’s actual Bluetooth address.
Also, replace the file names with the actual names of thefiles that you downloaded.On the phone, open the File Manager application (usually found onthe desktop of the phone or inside a subfolder e.g. ‘Tools’) and go tothe root of the memory card. You should see the two files there. Clickon the installation files. First, install the PyS60 interpreter (PythonForS60)and then the user interface for the PyS60 interpreter (PythonScriptShell).Follow the on-screen instructions on the phone – for instance, select ‘Yes’if a security warning appears on the phone screen.
Also, make sure thedate is set correctly on the phone.2. Write a Python ScriptYou can write a Python script on your computer with any text editor.Useful editors are, for example, Vim or Emacs.Write the following line in your text editor:print "Hello world!"After the code is typed, save the file under the name hello.py. Makesure that the file ending is .py and not .txt.The file is now ready to be executed on your phone! You do not haveto build or compile it any way. However, it must be copied to the phonefirst.INSTALLING PYTHON FOR S60 ON 2ND EDITION DEVICES213. Upload a Python Script to a PhoneWe use the same obexftp tool for this as in Step 1. Execute the followingcommand on the command line, replacing the Bluetooth address withyour phone’s actual address:obexftp -b 00:17:ED:AC:56:FE -c E: -C Python -p hello.py4.
Test a Python ScriptStart the PyS60 interpreter by clicking on the Python icon (Figure 2.1) onthe desktop or inside the appropriate subfolder on your phone. Once thePyS60 interpreter has started up, select ‘Options’ (Figure 2.4(a)) and then‘Run script’ (Figure 2.4(b)). Choose your script name, e:hello.py, fromthe list and select ‘OK’.
Your script should now start up and you shouldsee a green line stating Hello world! (Figure 2.5).Congratulations! You have successfully written and executed yourscript with PyS60! To go through all the examples in the book, just repeatsteps 2–4 for each new script. Have fun with it!2.2 Installing Python for S60 on 2nd Edition DevicesWe assume that your computer has either a built-in Bluetooth capabilityor an external Bluetooth dongle. You should have Bluetooth working onyour computer.2.2.1 Download the Installation FilesYou need to download two installation files from the Internet to yourcomputer. You find them at SourceForge’s PyS60 project page, http://sourceforge.net/projects/pys60.
There are many versions of files available on that website. All you need are two files: the PyS60 interpreter andthe user interface for the PyS60 interpreter.The file names consist of four components:• the name• the version number, for example, 1.4.0 (make sure you choose thelatest version, that is, the largest number)• the edition number (in this case, 2ndEd)• the choice of Feature Pack; depending on your phone model, youshould choose files ending with FP2 or FP3 (Table D.1 shows whichis suitable for your phone).22GETTING STARTEDAssuming that version 1.4.0 is the latest version and your phone usesFeature Pack 2, you download PythonForS60 1 4 0 2ndEdFP2.SIS(the PyS60 interpreter) and PythonScriptShell 1 4 0 2ndEdFP2.SIS (the user interface).2.2.2Send the Installation Files to the PhoneYou use Bluetooth to send the files to the phone’s message inbox. Makesure you have switched on Bluetooth on your phone (settings can befound in the ‘Connect’ panel).Windows UsersIn the File Explorer application, go to the location of the installationfiles.