You Are Here:

Community: Developer Discussion Boards

#1 Old Sending files autonomously - is this possible? - 2008-07-05, 23:19

Join Date: Jul 2008
Posts: 3
aubregine
Offline
Registered User
Hi,

I'm attempting to transfer files autonomously between two mobiles via Bluetooth, without the user controlling the process. Basically Mobile1 searches for Mobile2, and once Mobile2 is within range, Mobile1 will transfer the file to Mobile2, and Mobile2 will save it. The mobiles are paired Nokia 6630s.

As you can probably tell I haven't had much experience with PyS60, I'm following Jurgen Scheible's Mobile Python book and found an example for taking and sending photos with Bluetooth:
import camera, e32, socket, appuifw

PHOTO = u"e:\\Images\\bt_photo_send.jpg"

def send_photo():
try:
address, services = socket.bt_obex_discover()
except:
appuifw.note(u"OBEX Push not available", "error")
return

if u'OBEX Object Push' in services:
channel = services[u'OBEX Object Push']
socket.bt_obex_send_file(address, channel, PHOTO)
appuifw.note(u"photo sent", "info")
else:
appuifw.note(u"OBEX Push not available", "error")

def take_photo():
photo = camera.take_photo()
canvas.blit(photo, scale = 1)
photo.save(PHOTO)

def quit():
app_lock.signal()

canvas = appuifw.Canvas()
appuifw.app.body = canvas
appuifw.app.exit_key_handler = quit
appuifw.app.title = u"BT photo send"
appuifw.app.menu = [(u"Take photo", take_photo),
(u"Send photo", send_photo)]
app_lock = e32.Ao_lock()
app_lock.wait()
It works so far, but I just wanted to know if there's any way to adapt this code so that it sends a specific file stored on the phone, instead of taking a photo? And how can I make the receiving phone save this file automatically? Is this even possible? Any advice or examples you can give would be very much appreciated. Thank you!
Reply With Quote

#2 Old Re: Sending files autonomously - is this possible? - 2008-07-06, 04:19

Join Date: Feb 2008
Posts: 2,546
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
gaba88
Offline
Forum Nokia Champion
hi aubregine
first of all a warm welcome to the forum nokia PyS60 discussion board.
your code looks good but please note that from the next time you put some code use the quote marki.e. # provided in the tool bar. so we can observe the code more exactly


Gargi Das- http://gargidas.blogsot.com

Forum Nokia Python Wiki


Learn Python at http://mobapps.org/PyS60
Reply With Quote

#3 Old Smile Re: Sending files autonomously - is this possible? - 2008-07-06, 07:13

Join Date: May 2007
Posts: 2,739
Location: 21.46 N 72.11 E
croozeus's Avatar
croozeus
Offline
Forum Nokia Champion
Quote:
Originally Posted by aubregine View Post
It works so far, but I just wanted to know if there's any way to adapt this code so that it sends a specific file stored on the phone, instead of taking a photo?
Hi and Welcome to Pys60 Dibo !

In your code take_photo() is responsible for taking a photo.

If you just require to send the file (Already exisiting picture) on your phone just don't use or call the function,

And accordingly have only one option to send photo (You can add more functions if you require ofcourse.
Quote:
appuifw.app.menu = [(u"Send photo", send_photo)]
Quote:
Originally Posted by aubregine View Post
And how can I make the receiving phone save this file automatically? Is this even possible? Any advice or examples you can give would be very much appreciated. Thank you!
The receiving phone and the sending phones should be paired with each other. Also the receiving phone should set the Recceiving Authorization granted for the sending device. If this is done, then the file would be saved to the inbox of the receiving device, without asking any kind of "Receiving data" confirmation.

Hope that helps,

Br,
Croozeus
Reply With Quote

#4 Old Re: Sending files autonomously - is this possible? - 2008-07-06, 18:50

Join Date: Jul 2008
Posts: 3
aubregine
Offline
Registered User
Hi, thanks very much for the welcome! Btw croozeus, it's such a coincidence that you replied because I was searching online yesterday for related tutorials and found your socket module examples. So I've modified your "Transfer File through OBEX" example very slightly so that it sends a photo instead. I also paired and authorized the phones as you recommended, and that works, so thanks for that. Here's the code:

Server File
Code:
# This script lets 2 phones exchange a file via OBEX.
# 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"e:\\Photo 1.jpg"
    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
Code:
# this file lets 2 phones exchange a file via OBEX
# this file is the client side
# 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"e:\\Photo 1.jpg"

# 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."
My question is, if I know the server phone's address and the port (OBEX Object Push), how do I change the code so that it bypasses the "Choose device" and "Choose port" selection process? Basically, so that it connects and sends the photo automatically. I tried storing the address in a variable and addr,services=bt_obex_discover(variable), but for some reason that doesn't work. Sorry, it's just I'm completely new at this and not really sure what I'm doing wrong here!

Again, thanks in advance for any advice and help you can give...
Reply With Quote

#5 Old Re: Sending files autonomously - is this possible? - 2008-07-06, 19:36

Join Date: Feb 2008
Posts: 2,546
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
gaba88
Offline
Forum Nokia Champion
hi aubregine
i think its really easy.
change the code in the client side to this in place of
Code:
address=(addr,port)
write your known address and port number.
hope this will work for you.


Gargi Das- http://gargidas.blogsot.com

Forum Nokia Python Wiki


Learn Python at http://mobapps.org/PyS60
Reply With Quote

#6 Old Re: Sending files autonomously - is this possible? - 2008-07-08, 23:37

Join Date: Jul 2008
Posts: 3
aubregine
Offline
Registered User
Thanks so much to everyone for your help, finally managed to get it working! So now the files transfer and save automatically once you run the script. Just need to specify the target address and port. I know it's quite basic for most of you, but thought some other beginners out there might find it helpful, so here's the code...

Client
Code:
# this script lets 2 phones exchange a file via OBEX
# this file is the client side

from socket import *

# define target device
target = "00:00:00:00:00:00"

# specify file to be sent
send_path = u"e:\\Images\Photo 1.jpg"

# send file via OBEX
print "Sending file %s to host %s port %s"%(send_path, str(target), 4)
bt_obex_send_file(str(target), 4, send_path)
print "File sent."
Server
Code:
# this script lets 2 phones exchange a file via OBEX
# this file is the server

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"e:\\Images\Photo 1.jpg"
    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."
Reply With Quote

#7 Old Re: Sending files autonomously - is this possible? - 2008-07-09, 05:22

Join Date: Feb 2008
Posts: 2,546
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
gaba88
Offline
Forum Nokia Champion
hi aubregine
its great that you have completed it. for further reference of everybody i have put the code in a wiki article here.
have a look at that and dont forget you can edit that article also.


Gargi Das- http://gargidas.blogsot.com

Forum Nokia Python Wiki


Learn Python at http://mobapps.org/PyS60
Reply With Quote

#8 Old Re: Sending files autonomously - is this possible? - 2009-09-13, 14:30

Join Date: Mar 2009
Posts: 3
runnerxia
Offline
Registered User
hi, I cannot get it working. I was using N73 as server and E71 as client, log on client shows the server and service was discovered correctly, but the bt_obex_send_file raised an exception: 32,'broken pipe'. I checked both the address and port used on the client side are compliant with the server side.

Any idea?
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
sending vcal files to Nokia N90 tomhodder General Messaging 1 2006-07-27 13:44
Sending multiple files over bluetooth from phone to PC dguldbrandsen Symbian Networking & Messaging 4 2005-01-30 10:55
Problems in sending files to the server yuava Other Programming Discussion 关于其他编程技术的讨论 4 2005-01-08 14:53
Sending several files without using SIS stopin General Symbian C++ 0 2003-07-10 17:32
Sending Files via Bluetooth mike456 Bluetooth Technology 1 2002-09-05 08:48

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia