| Reply | « Previous Thread | Next Thread » |
|
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()
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 |
|
I think PDIS project has code that does this, check it out: http://pdis.hiit.fi/pdis/download/pdis/
|
| simo.salminen |
| View Public Profile |
| Find all posts by simo.salminen |
|
Quote:
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()
|
|
Quote:
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)) Is there something else I'm missing? Thanks, Albert |
|
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 |
|
Quote:
See page 29 of the API Reference. |
|
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()
|
|
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 |
|
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 ... |
|
Quote:
Regards, Feike |
| feikekramer |
| View Public Profile |
| Find all posts by feikekramer |
|
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 | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |