Wiley.Mobile.Python.Rapid.prototyping.of.applications.on.the.mobile.platform.Dec.2007 (779889), страница 7
Текст из файла (страница 7)
Right-click on the file to send. Select ‘Send To’ and then ‘Bluetoothdevice’ (see Figure 2.6). A window asks you to select where you wantto send the file. Select ‘Browse’ and wait a moment. Your computer willsearch for nearby Bluetooth devices.A list (see Figure 2.7) will show the nicknames of all Bluetooth devicesfound. Select the nickname of your phone and select ‘OK’ and then‘Next’. A popup note should be displayed on your phone asking ‘Receivemessage by way of Bluetooth .
. . ?’ and you should select ‘Yes’. A messageFigure 2.6 File ExplorerINSTALLING PYTHON FOR S60 ON 2ND EDITION DEVICES23Figure 2.7 Bluetooth devices foundshould then arrive in your inbox. Repeat these steps for the secondinstallation file.In different Windows versions, the menus might look slightly different,but the process is basically the same. Also you might have to do Bluetoothpairing between your phone and your computer before you can send thefile. You can find instructions on how to do this in Section 7.1.Mac OS X UsersSelect the Bluetooth icon on the Mac window and select ‘Send file’ (seeFigure 2.8). Then select one of the installation files and select ‘Send’.This opens a window where you need to select ‘Search’ to make yourcomputer search for nearby Bluetooth devices.A list (see Figure 2.9) will show the nicknames of all Bluetooth devicesfound.
Select the nickname of your phone and press ‘Send’. A popupnote should be displayed on your phone asking ‘Receive message byway of Bluetooth . . . ?’ and you should select ‘Yes’. A message shouldthen arrive in your inbox. Repeat these steps for the second installationfile.In different Mac OS versions, the menus might look slightly different,but the process is basically the same. Also you might have to do Bluetoothpairing between your phone and your computer before you can send thefile.
You can find instructions on how to do this in Section 7.1.24GETTING STARTEDFigure 2.8 Initiating Bluetooth transfer on a MacFigure 2.9 Bluetooth devices foundLinux UsersYou need the ussp-push and hcitool programs installed. In manydistributions, they can be installed from packages ussp-push andbluez-utils.INSTALLING PYTHON FOR S60 ON 2ND EDITION DEVICES25Perform Bluetooth scanning to find the Bluetooth address of yourphone:hcitool scanYou should see the name of your phone in the list, with its Bluetoothaddress (e.g., 00:17:ED:AC:56:FE). Next, you can upload the SIS files toyour phone with the following commands:ussp-push 00:17:ED:AC:56:FE@ PythonForS60...SIS PyS60.SISussp-push 00:17:ED:AC:56:FE@ PythonShellScript...SIS Shell.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.After each file has been uploaded, a popup note should be displayedon your phone asking ‘Receive message by way of Bluetooth .
. . ?’ andyou should select ‘Yes’.2.2.3 Install the Files to the PhoneOpen the received messages by selecting them in order. First, installthe PyS60 interpreter (PythonForS60) and then the user interface for thePyS60 interpreter (PythonScriptShell). Follow the on-screen instructionson the phone – for instance, select ‘Yes’ if a security warning appearson the phone screen.
If you are asked to install the files on the phonememory (Ph. Mem) or the memory card (M. card), it is up to you whichone to choose.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.2.2.4 Writing and Running a Python Script1. Write a Python ScriptYou can write a Python script on your computer with any text editor.Useful editors for Windows are, for example, ConTEXT or PyWin whichare freely downloadable on the Internet, but Notepad also works. Possibleeditors for Mac 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.
In Linux, you can use Vim or Emacs.26GETTING STARTEDWrite 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.2.
Install a Python Script on a PhoneYou send a Python script to the phone in the same way that you sent theinstallation files in Section 2.2.3.Open the received hello.py file in the message inbox by selecting it.The message inbox handler will recognize it as a PyS60 file and displaya popup note. When asked to install your file as a ‘Python script’ or a‘Python lib module’ (Figure 2.10), choose ‘Python script’. Your Pythonscript is automatically installed to the correct place. Now you can test thescript.Figure 2.10 Installing a Python script3.
Test a Python ScriptStart the PyS60 interpreter by clicking on the Python icon (Figure 2.1)on the desktop or inside the appropriate subfolder on your phone. Oncethe PyS60 interpreter has started up, select ‘Options’ (Figure 2.4(a)) andWRITING A PROGRAM IN PYTHON FOR S6027‘Run script’ (Figure 2.4(b)). Choose your script name, 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 1–3 for each new script. Have fun with it!2.3 Writing a Program in Python for S60Example 1: First PyS60 programWe write a simple program consisting of three lines of code as our firstreal example.
It should do the following:1. Display a text input field on the screen; the instruction in the textfield should say ‘Type your name’ (see Figure 2.11(a)).2. Display a popup note stating ‘Greetings from:’ followed by whateverthe user typed into the text input field (see Figure 2.11(b)).(a)(b)Figure 2.11 Our first Python script example: (a) a text input field and (b) a popup noteThe code to do this is as follows:import appuifwname = appuifw.query(u"Type your name:", "text")appuifw.note(u"Hello world! Greetings from: " + str(name), "info")28GETTING STARTEDIn the first line of code, we import the appuifw module, whichhandles user interface elements such as text input fields and popup notes.In the second line of code, we create a single-field dialog (Figure2.11(a)) using the query() function of the appuifw module withtwo parameters: label and type.
For the first parameter, label, we putthe text u"Type your name:". The u is required because the phoneunderstands only text declared as Unicode and the quotation marks areneeded because the label parameter must be given as a string. As thesecond parameter, type, we put "text", to declare the input field asa text dialog. Other possible types are "number", "date", "time","query" and "code". The two parameters are separated by a comma.This line of code also creates a variable called name which will receivethe result of the text input field after the user has typed something intothe dialog.In the third line of code, we create a popup note (Figure 2.11(b))using the note() function of the appuifw module with two parameters:label and type.
For the first parameter, label, we put the text u"Helloworld! Greetings from: "+ str(name). This is the text that willappear inside the popup note. Again, the string must be given in quotationmarks but the popup note will also include the result that the user hastyped in, so we add the contents of the variable name to the string.As the second parameter, type, we put "info", to declare the popupnote as information; that causes a green exclamation mark to appearinside the popup note. Other possible types are "error", showing ared exclamation mark, and "conf"(confirmation), showing an animatedcheck mark.
The two parameters are separated with a comma.Type this example into a file and upload it to your phone for testing,by following the instructions in Sections 2.1 or 2.2.2.4 White Space in Python CodePython does not use curly brackets or special begin and end statementsto structure code. Instead, the indentation level of a line determines towhich code block the line belongs.
A code block is a group of statementsthat belongs to an if clause, a for-loop, a function or a similar structure.Indentation starts a code block and it ends when the indentation stops.In the following example, the first two print statements form a codeblock that belongs to the if clause. The last print statement does notbelong to the code block and it is executed regardless of the if condition.if x > 5:print "X is greater than 5"print "Also this line belongs to the if-clause"print "This line does not belong to the if-clause"TROUBLESHOOTING29You can use spaces or tabs for indentation but you must be consistent.In this book, we use four spaces for indentation.
You can follow thesame style in your own code. Many text editors that support Pythonprogramming, such as Emacs or PythonWin, automatically make surethat the indentation is consistent. Using an editor like this is highlyrecommended.In some cases you must split a long expression over several lines. Aswhite space is significant only at the beginning of a statement, you cansplit a long expression freely over several lines. However, in some casesthe Python interpreter cannot know whether the subsequent lines belongto one statement or several statements. To make your intention clear, youcan put a backslash character ‘\’ at the end of a line to specify that thenext line continues the same statement.2.5 TroubleshootingTo avoid an installation error, make sure the date is set correctly on thephone, since ‘signed’ installation files have a date that specifies fromwhen they work.
With some phone models, you might encounter an errorsuch as ‘Certificate error. Contact the application supplier’. You can fixthis by selecting ‘Tools, App. mgr, Options, Settings’. Change ‘Softwareinstallation’ from ‘Signed Only’ to ‘All’ and ‘Online certif. check’ to ‘Off’.2.5.1Bluetooth as an Alternative to a USB CableInstead of using a USB cable to connect your phone to a computer, youcan use Bluetooth, if your PC hardware has built-in Bluetooth functionalityor you have a USB Bluetooth dongle available. Check the manual of theNokia PC Suite for instructions, as well as your phone’s manual for howto set up the connection.2.5.2 Bluetooth FailsYou might need to pair your phone with your computer.
See the phonemanual to find out how to do this. Typically, pairing can be performedby way of the phone’s ‘Connectivity, Bluetooth’ settings.2.5.3No Nokia PC Suite AvailableIf you do not have Nokia PC Suite installed, you can connect your phoneto the computer as an external hard drive. Please check Section 2.1.3 forinstruction on how to get this to work (it works in a very similar way onboth Windows and Mac).30GETTING STARTED2.5.4 Using an EmulatorTesting scripts with an emulator on a PC is possible.