[PyQt] QBluetooth support for a PyQT Bluetooth Server

José Jácome josejacomeb at gmail.com
Tue May 28 22:31:07 BST 2019


Hi to all,

I've read the official tutorial of QBluetooth C++ on
https://doc.qt.io/qt-5/qtbluetooth-btchat-example.html, I tested it on my
PC and it works very. I would like to adapt that code to PyQT to test a
Bluetooth Server, after digging in the PyQT Docs I have figure out that
class *QBluetoothServiceInfo::Sequence* doesn't exist. This class is
defined for configure the PublicBrowseGroup and protocolDescriptorList
parameters.

While there isn't enough info about BT Server on PyQT, I've tried to adapt
the code in my way but I haven't got success to connect my Phone to my PC
like the C++ code does while I haven't had a Python or qt.bluetooth.bluez
error. If I can find my error, I will implement this code inside a Thread
on a GUI.

I wonder if PyQT has full support for QBluetooth or Am I doing something
wrong?

Thanks for your time for reading this, any reply is appreciated.

Sincerely,

José Jácome

There is the class I'm testing

[CODE]
#!/usr/bin/env python3

from PyQt5.QtBluetooth import *
from PyQt5.QtCore import *

class BTServer():
    '''
        Class based on:
https://doc.qt.io/qt-5/qtbluetooth-btchat-example.html
    '''
    def __init__(self):
        self.startServer()
    def startServer(self, localAdapter=QBluetoothAddress()):
        self.rfcommServer =
QBluetoothServer(QBluetoothServiceInfo.RfcommProtocol)
        self.rfcommServer.newConnection.connect(self.clientConnected)
        self.result = self.rfcommServer.listen(localAdapter)
        self.clientSockets = []
        if not self.result:
            print("Cannot bind chat server to: " + localAdapter.toString())
        self.serviceInfo = QBluetoothServiceInfo()
        self.serviceInfo.setAttribute(QBluetoothServiceInfo.ServiceName,
"Bt Chat Server")

self.serviceInfo.setAttribute(QBluetoothServiceInfo.ServiceDescription,
                         "Example bluetooth chat server")

self.serviceInfo.setAttribute(QBluetoothServiceInfo.ServiceProvider, "
qt-project.org")
        self.serviceUuid =
QBluetoothUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8")
        self.serviceInfo.setServiceUuid(QBluetoothUuid(self.serviceUuid))
        self.PublicBrowseGroup =
[QVariant(QBluetoothUuid(QBluetoothUuid.PublicBrowseGroup))]

self.serviceInfo.setAttribute(QBluetoothServiceInfo.BrowseGroupList,
self.PublicBrowseGroup)
        self.protocolDescriptorList = [QBluetoothUuid(QBluetoothUuid.L2cap),

 QBluetoothUuid(QBluetoothUuid.Rfcomm),
                                       int(self.rfcommServer.serverPort())]
        print(self.protocolDescriptorList)

self.serviceInfo.setAttribute(QBluetoothServiceInfo.ProtocolDescriptorList,
                         self.protocolDescriptorList);
        self.serviceInfo.registerService(localAdapter)

    def clientConnected(self):
        self.socket = self.rfcommServer.nextPendingConnection()
        if not self.socket:
            return
        self.rfcommServer.readyRead.connect(self.readSocket)
        self.rfcommServer.disconnected.connect(self.clientDisconnected)
        self.clientSockets.append(self.socketBT)
        print(self.socketBT.peerName())
    def readSocket(self):
        print("Socket data")
    def clientDisconnected(self):
        print("Client Disconected")
[/CODE]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190528/e872f8fd/attachment.html>


More information about the PyQt mailing list