Bluetooth server with Python 3.3

前端 未结 1 2026
迷失自我
迷失自我 2020-12-31 08:29

Python 3.3 came with native support for bluetooth sockets. Unfortunately, it\'s not too well documented yet (there is only one mention of it in the documentation).

G

1条回答
  •  一整个雨季
    2020-12-31 08:47

    Bad news: Python doesn't appear to support what you want to do out of the box. (At least not in socketmodule.c).

    Most of the python/bluetooth users I've seen use pybluez although it hasn't been updated since 2009.

    Good news: I went through their source (for Linux connections), and found the relevant bits for advertising services. Most of the code is essentially copy-pasted from the python 2.2 version of socketmodule.c.

    pybluez does define some additional functionality for a socket object to implement all those bluetooth goodies. It doesn't get too low-level, and instead depends on BlueZ for that. From what I can tell, it basically takes python objects and creates the data structures expected by BlueZ and just calls that. If you don't want to/can't use pybluez, you'll have to somehow implement this missing functionality. I think you may be able to do it with c-types. The relevant parts for advertising the service are in btmodule.c, lines 2562-2642.

    There is a python-3 branch in the source for pybluez, although I don't know if it works or not.

    If you do decide to use pybluez, an example taken from their source

    server_sock=BluetoothSocket( RFCOMM )
    server_sock.bind(("",PORT_ANY))
    server_sock.listen(1)
    
    port = server_sock.getsockname()[1]
    
    uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
    
    advertise_service(server_sock, "SampleServer",
                      service_id = uuid,
                      service_classes = [ uuid, SERIAL_PORT_CLASS ],
                      profiles = [ SERIAL_PORT_PROFILE ], 
                      )
    

    As google code is closing, the code can also be found on github here.

    0 讨论(0)
提交回复
热议问题