You Are Here:

Community: Developer Discussion Boards

#1 Old how to accept bluetooth connections with socket module? - 2005-01-27, 23:23

Join Date: Nov 2004
Posts: 14
relbs
Offline
Registered User
are there any examples of accepting incoming bluetooth connections in python? All the bluetooth examples we've seen are of phones making
outgoing connections in python. The following code doesn't seem to
work:
Code:
from e32socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s)
s.bind(("", p))
bt_advertise_service( "asdf", s, True, RFCOMM )
s.listen(1)
c = s.accept()
No record is advertised via SDP (browsing the advertised services using
bluez shows nothing but what was there already). Attempts to connect to
the socket from a desktop machine with bluez fail with "connection
refused". We were able to successfully establish an outgoing bluetooth
connection from the phone to the PC.

Notes:
1. changing e32socket to socket results in errors with
bt_rfocomm_get_available_server_channel
TypeError: argument 1 must be e32socket.Socket, not instance
and also with
bt_advertise_service
TypeError: argument 2 must be e32socket.Socket, not instance
2. binding to "00:00:00:00:00" and the bluetooth address of the phone
has the same effect
3. abritrarily assigning p to an unused RFCOMM channel has no effect.

thanks!
Albert
Reply With Quote

#2 Old 2005-01-28, 15:35

Join Date: Aug 2004
Posts: 290
simo.salminen
Online
Regular Contributor
I think PDIS project has code that does this, check it out: http://pdis.hiit.fi/pdis/download/pdis/
Reply With Quote

#3 Old Re: how to accept bluetooth connections with socket module? - 2005-01-28, 16:41

Join Date: Dec 2004
Posts: 646
jplauril's Avatar
jplauril
Offline
Forum Nokia Expert
Quote:
Originally posted by relbs
are there any examples of accepting incoming bluetooth connections in python? All the bluetooth examples we've seen are of phones making
outgoing connections in python. The following code doesn't seem to
work:
Code:
from e32socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s)
s.bind(("", p))
bt_advertise_service( "asdf", s, True, RFCOMM )
s.listen(1)
c = s.accept()
No record is advertised via SDP (browsing the advertised services using
bluez shows nothing but what was there already). Attempts to connect to
the socket from a desktop machine with bluez fail with "connection
refused". We were able to successfully establish an outgoing bluetooth
connection from the phone to the PC.

Notes:
1. changing e32socket to socket results in errors with
bt_rfocomm_get_available_server_channel
TypeError: argument 1 must be e32socket.Socket, not instance
and also with
bt_advertise_service
TypeError: argument 2 must be e32socket.Socket, not instance
2. binding to "00:00:00:00:00" and the bluetooth address of the phone
has the same effect

3. abritrarily assigning p to an unused RFCOMM channel has no effect.

thanks!
Albert
The socket module has a bug related to what data types some of the
functions expect. Instead of the Python wrapper object implemented by
the "socket" module they expect to get the internal native
e32socket.Socket object. This bug (ref: 0154) was mentioned in the
Release Notes, but unfortunately the description there was too vague
to be of much use. The bug will be fixed in a future release, but in
the mean time you can use the following simple workaround: If a
function complains that some argument foo "must be e32socket.Socket,
not instance", then simply pass foo._sock instead of foo.

A few other comments:

- Use the "socket" module, not the "e32socket" module. The "socket"
module is the official, supported API. The "e32socket" module is an
internal module, and although its' API is not hard to guess from
reading the socket.py code, it is subject to change and
therefore it is not safe to build upon it.

- The service name should be given as a Unicode string to
bt_advertise_service.

Here's the corrected version of that code:

Code:
from socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s._sock)
s.bind(("", p))
bt_advertise_service( u"asdf", s._sock, True, RFCOMM )
s.listen(1)
c = s.accept()
Reply With Quote

#4 Old Re: Re: how to accept bluetooth connections with socket module? - 2005-01-28, 19:09

Join Date: Nov 2004
Posts: 14
relbs
Offline
Registered User
Quote:
Originally posted by jplauril

Here's the corrected version of that code:

Code:
from socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s._sock)
s.bind(("", p))
bt_advertise_service( u"asdf", s._sock, True, RFCOMM )
s.listen(1)
c = s.accept()
Hello,

thanks for the reply. I am still unable to get my phone (7610) to accept incoming connections from either a PC or another phone (also 7610). I tried using exactly the code above on one phone to listen, and the following code on another phone to connect:

Code:
from socket import *
a, s = bt_discover()
p = s[u"asdf"]
c = socket(AF_BT, SOCK_STREAM)
c.connect((a, p))
The bt_discover() works fine, and the service is discovered (usually on RFCOMM port 3), but the connect() invariably fails with a socket.error: (54, 'Connection refused by remote host') I get more or less the same results when trying to connect from a linux PC.

Is there something else I'm missing?

Thanks,
Albert
Reply With Quote

#5 Old 2005-02-01, 20:19

Join Date: Nov 2004
Posts: 14
relbs
Offline
Registered User
I have discovered the reason incoming connections are not accepted is the security settings are too high by default. If I write a python extension that lowers or disables security on the bound RFCOMM channel, then the accept() works as expected.

Interestingly enough, running a C++ application beforehand that lowers security (e.g. btpointtopoint example) will also do the trick as long as you happen to re-use the same RFCOMM channel. Is that really such a good idea, to allow bluetooth channels to retain state across processes? I guess it doesn't matter if you're always proactive about setting security, but it's not a design choice I would make.

So here's the question: Is there an undocumented socket command in python that allows us to set the security of a socket? socket.setsockopt seems the obvious for this kind of thing...

Regards,
Albert
Reply With Quote

#6 Old 2005-02-09, 18:49

Join Date: Dec 2004
Posts: 646
jplauril's Avatar
jplauril
Offline
Forum Nokia Expert
Quote:
Originally posted by relbs

So here's the question: Is there an undocumented socket command in python that allows us to set the security of a socket?
Would a documented one do? See page 29 of the API Reference.
Reply With Quote

#7 Old 2005-02-09, 20:01

Join Date: Nov 2004
Posts: 14
relbs
Offline
Registered User
wow, I feel silly. Thank you for pointing that out to me. Allow me to post code that finally (!!) works.

Code:
from socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s._sock)
s.bind(("", p))
bt_advertise_service( u"asdf", s._sock, True, RFCOMM )
set_security(s._sock, AUTHOR)
s.listen(1)
c = s.accept()
Reply With Quote

#8 Old serverbtconsole.py - 2005-03-08, 01:28

Join Date: Jan 2005
Posts: 8
rjstone
Offline
Registered User
Thanks guys! I found this information and the sample code very helpful. I'm glad I found your thread before I started working on this or I probably would have wasted an hour or two trying to figure out what was going on.

Also, if anyone is interested, here's the simplebtconsole.py modified so that it behaves as a server: http://otaku.org/files/serverbtconsole.py
Reply With Quote

#9 Old Re: how to accept bluetooth connections with socket module? - 2006-04-03, 14:58

Join Date: Mar 2006
Posts: 29
MrKikkeli's Avatar
MrKikkeli
Offline
Registered User
rjstone, have you tried to handle multiple client connections with your bluetooth server ?

I am interested in knowing how to do that, the use of threads seems impossible since threads in pys60 can't manage objects such as sockets when they're not created within the thread... And basically, you would have to create a new thread handling an incoming connection as soon as the new socket is created ...
Reply With Quote

#10 Old Re: how to accept bluetooth connections with socket module? - 2006-08-08, 13:51

Join Date: Dec 2005
Posts: 7
feikekramer
Offline
Registered User
Quote:
Originally Posted by relbs
wow, I feel silly. Thank you for pointing that out to me. Allow me to post code that finally (!!) works.

Code:
from socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s._sock)
s.bind(("", p))
bt_advertise_service( u"asdf", s._sock, True, RFCOMM )
set_security(s._sock, AUTHOR)
s.listen(1)
c = s.accept()
I have this code running on a phone and I'm trying to find out on which port the service is running, by scanning for devices and requesting the SDP records (using PyBluez or BlueSweep). The code should put an entry in the phone's SDP records, isn't it? However, I can't find any services that look like the one above... What is wrong?

Regards,
Feike
Reply With Quote

#11 Old Re: how to accept bluetooth connections with socket module? - 2006-08-08, 18:08

Join Date: Nov 2004
Posts: 14
relbs
Offline
Registered User
s60 python does not appear to advertise under the PublicBrowseGroup, which is what most SDP browsing applications look for. Instruct your SDP searching/browsing application to search for either the L2CAP UUID (0x0100) or the Serial Pot Service Class ID (0x1101)
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

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 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d81449X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ