×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    14

    how to accept bluetooth connections with socket module?

    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

  2. #2
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    295
    I think PDIS project has code that does this, check it out: http://pdis.hiit.fi/pdis/download/pdis/

  3. #3
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643

    Re: how to accept bluetooth connections with socket module?

    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()

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    14

    Re: Re: how to accept bluetooth connections with socket module?

    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

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    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

  6. #6
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643
    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.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    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()

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    8

    serverbtconsole.py

    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

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    29

    Re: how to accept bluetooth connections with socket module?

    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 ...

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Re: how to accept bluetooth connections with socket module?

    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

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    14

    Re: how to accept bluetooth connections with socket module?

    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)

Posting Permissions

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