Using QBluetoothSocket() to connect to multiple Rfcomm devices failes for second device
-
I'm trying to use QBluetoothSocket to open (client-) connections to two BT Classic SPP Servers. The SPP Servers run on two separate ESP32 devices which are paired with the PC already.
I wrote a Class to handle a SPP connection to a device with a known Bluetooth address. This works fine as long as I'm only trying to connect to a single device. If I try to start a connection to a second device with QBluetoothSocket::connectToService() the PC connects to the device ( connection is reported correctly on ESP32 side ).
The problem is that the QBluetoothSocket stays in the "ConnectingState" and never calls the QBluetoothSocket::connected() signal for the second device.
I'm on a Linux system ( Ubuntu 20.04 ) running BlueZ ( 5.5.3 ) with QT v6.2.4
Has anyone experienced this problem before?
For reference, this is the implementation of the Class handling the connection:
#include <QtCore/qmetaobject.h> #include "bclassichandler.h" BTClassicHandler::BTClassicHandler( QObject *parent ) : QObject{ parent } {} BTClassicHandler::~BTClassicHandler() {} void BTClassicHandler::connect( const QBluetoothAddress &btAddr ) { if ( m_socket ) return; m_socket = new QBluetoothSocket( QBluetoothServiceInfo::RfcommProtocol ); qDebug() << "Created Socket for " << btAddr.toString(); qDebug() << "Connecting to SPP on " << btAddr.toString(); QObject::connect( m_socket, &QBluetoothSocket::readyRead, this, &BTClassicHandler::onSocketRead ); QObject::connect( m_socket, &QBluetoothSocket::connected, this, &BTClassicHandler::onSocketConnected ); QObject::connect( m_socket, &QBluetoothSocket::disconnected, this, &BTClassicHandler::onSocketDisconnected ); QObject::connect( m_socket, &QBluetoothSocket::errorOccurred, this, &BTClassicHandler::onSocketError ); QObject::connect( m_socket, &QBluetoothSocket::stateChanged, this, &BTClassicHandler::onStateChanged ); setAddress( btAddr ); m_socket->connectToService( btAddr, QBluetoothUuid::ServiceClassUuid::SerialPort ); } void BTClassicHandler::disconnect() { delete m_socket; m_socket = nullptr; } /* Private Slots */ void BTClassicHandler::onSocketConnected() // <<<<< THIS IS NEVER CALLED FOR THE SECOND DEVICE !!!! { qDebug() << "Connected to " << m_btAddr.toString(); emit sig_BTConnected( m_btAddr ); } void BTClassicHandler::onSocketDisconnected() { qDebug() << "Disconnected from " << m_btAddr.toString(); } void BTClassicHandler::onSocketRead() {} void BTClassicHandler::onSocketError( QBluetoothSocket::SocketError error ) { QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketError>(); QString errorString = metaEnum.valueToKey( static_cast<int>( error ) ); qDebug() << "Socket Error " << m_btAddr.toString() << " :" << errorString; } void BTClassicHandler::onStateChanged( QBluetoothSocket::SocketState state ) { QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketState>(); QString stateString = metaEnum.valueToKey( static_cast<int>( state ) ); qDebug() << "State Changed " << m_btAddr.toString() << " : " << stateString; }( I also tried to get help here: https://stackoverflow.com/questions/72965275/problem-connecting-qbluetoothsocket-to-multiple-devices )