Phase 2

(This Website is created and managed by Pankaj Nathani) 

Home   Installation   Phase 1    Phase2   Phase4   Module-Wise Examples    Modules 

 Extensions    C++Py60 Extensions    Forum Nokia(Python)    Google Group    Contact Me   

 

Getting Started With Python S60

Phase 2

Sound Recorder Example

import appuifw, e32
# import the audio module
import audio


#This defines a name of the file to be the sound file, incl. its full path
filename = 'e:\\chime.wav'

#This defines the recording part:
def recording():
 global S
 # open the sound file to be ready for recording and set an instance (S) of it
 S=audio.Sound.open(filename)
 # do the recording (has to be stopped by closing() function below)
 S.record()
 print "Recording .Select Stop to end"

# define the playing part:
def playing():
 global S
 try:
 # open the sound file to be ready for playing by setting an instance (S) of it
 S=audio.Sound.open(filename)
 # play the sound file
 S.play()
 print "Playing"
 except:
 print "Record a sound first!"

# stopping of recording / playing and closing of the sound file
def closing():
 global S
 S.stop()
 S.close()
 print "Stopped"

def quit():
 script_lock.signal()
 appuifw.app.set_exit()

# define the application menu
appuifw.app.menu = [(u"play", playing),
 (u"record", recording),
 (u"stop", closing)]

appuifw.app.title = u"Sound recorder"

appuifw.app.exit_key_handler = quit
script_lock = e32.Ao_lock()
script_lock.wait()

Midi Player Example

import appuifw
import e32
import audio


def playsound1():
 S = audio.Sound.open("E:\\sound1.mid")
 S.play()
 menu()
 
def playsound2():
 S = audio.Sound.open("E:\\sound2.mid")
 S.play()
 menu()


def exit_key_handler():
 appuifw.app.set_exit()
 
def quit():
 appuifw.app.set_exit()

L = [u"sound 1", u"sound 2", u"exit"]

def menu():
 index = appuifw.popup_menu(L,u'select')
 if index == 0:
 playsound1()
 if index == 1:
 playsound2()
 if index == 2:
 quit()
 
appuifw.app.title = u"Midi player"

appuifw.app.exit_key_handler = exit_key_handler
menu()

Download and View Video

import appuifw, e32
# import urllib
import urllib

print "press options"

# function that handles the fetching and the playing of the video
def fetching():
 # define the url where the video file is located on the server
 url = "YOU URL HERE"
 # define the loction on the phone where the fetched video file shall be stored
 tempfile = "e:\\video.3gp"
 try:
 print "Retrieving information..."
 # fetch down the video and store it to you hard drive
 urllib.urlretrieve(url, tempfile)
 # create an active object before playin the video
 lock=e32.Ao_lock()
 # a content handler handles the playing of the video
 # load the content handler and tell to release the active object after the video has finnished playing (lock.signal)
 content_handler = appuifw.Content_handler(lock.signal)
 # open the video via the content handler. It will start playing automatically
 content_handler.open(tempfile)
 # Wait for the user to exit the image viewer.
 lock.wait()
 print "Video Plating finished."
 except:
 print "Errors!"


def quit():
 app_lock.signal()

# define the application menu with one choice "get video" and call the fetching video
appuifw.app.menu = [(u"get video", fetching)]

appuifw.app.title = u"Get video"

appuifw.app.exit_key_handler = quit
app_lock = e32.Ao_lock()
app_lock.wait()


Transfer Text from One Mobile to Other Mobile

(Server File)

(Should Be running on the server device when client file is to be running on other device)
# this file is the server side
# the corresponding client side file is called rfcomm_client.py

from socket import *
import appuifw

server_socket = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(server_socket)
server_socket.bind(("", p))
print "bind done"
server_socket.listen(1)
bt_advertise_service( u"port", server_socket, True, RFCOMM)
set_security(server_socket, AUTH)
print "I am listening"

# Note: Don't call .send or .recv on the server_socket!
# Use the sock object returned by server_socket.accept().
(sock,peer_addr) = server_socket.accept()
print "Connection from %s"%peer_addr
test = appuifw.query(u"Type words", "text", u"")
sock.send(test+'\n')
print "sending done"
import e32
# Allow time for data to be sent to work around a bug in the socket
# module.
e32.ao_sleep(1)
sock.close()

(Client File)

# this file is the client side
# the corresponding server side file is called rfcomm_server.py

import socket
import appuifw
import e32

class BTReader:
 def connect(self):
 self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
 addr,services=socket.bt_discover()
 print "Discovered: %s, %s"%(addr,services)
 if len(services)>0:
 import appuifw
 choices=services.keys()
 choices.sort()
 choice=appuifw.popup_menu([unicode(services[x])+": "+x
 for x in choices],u'Choose port:')
 port=services[choices[choice]]
 else:
 port=services[services.keys()[0]]
 address=(addr,port)
 print "Connecting to "+str(address)+"...",
 self.sock.connect(address)
 print "Connected..." 
 def readline(self):
 line=[]
 while 1:
 ch=self.sock.recv(1)
 if(ch=='\n'):
 break
 line.append(ch)
 return ''.join(line)
 def close(self):
 self.sock.close()

bt=BTReader()
bt.connect()
print "Received Result: "+bt.readline()
bt.close()

Transfer File through OBEX

(Server File)

# This is the server, the corresponding client is obex_client.py
from socket import *
import appuifw

# Create a bluetooth socket in waiting state to be connected to
s = socket(AF_BT, SOCK_STREAM)
port = bt_rfcomm_get_available_server_channel(s)
print "Binding service to port %s"%port
s.bind(("", port))
print "Service bound."

# Advertise the OBEX service, so it can be seen by other phones
service_name=u"Test OBEX service"

print "Advertising service as %s"%repr(service_name)
bt_advertise_service(service_name, s, True, OBEX)

try: 
 print "Setting security to AUTH."
 set_security(s, AUTH)

 receive_path = u"c:\\obex.txt"
 print "Receiving file."
 bt_obex_receive(s, receive_path)
 print "File received."

 import e32
 e32.ao_sleep(1)
finally:
 print "Stopping service advertising."
 bt_advertise_service(service_name, s, False, OBEX)

print "Closing socket."
s.close()
print "Socket closed."
print "Finished."

(Client File)

# the corresponding server side file is called obex_server.py

from socket import *
import appuifw
import e32

# JL: you don't need a socket for this!
## create socket
#s=socket(AF_BT,SOCK_STREAM)

# scan for other phones offering OBEX service
addr,services=bt_obex_discover()
print "Discovered: %s, %s"%(addr,services)
if len(services)>0:
 choices=services.keys()
 choices.sort()
 choice=appuifw.popup_menu([unicode(services[x])+": "+x
 for x in choices],u'Choose port:')
 port=services[choices[choice]]
else:
 port=services[services.keys()[0]]
address=(addr,port)

# create file to be sent
send_path = u"c:\\test.txt"
f=open(send_path, 'w')
f.write("hello")
f.close() # NOTE: parens were missing here before!

# send file via OBEX
print "Sending file %s to host %s port %s"%(send_path, address[0], address[1])
bt_obex_send_file(address[0], address[1], send_path)
print "File sent."