Qt 6.11 is out! See what's new in the release
blog
Bluetooth never fires connected signal
-
I am connecting to a BLE device. Scanning works well but I go to connect and the device seems to think it's connected to the computer (shows connection and disconnected events) but QT never seems to fire the connected signal so I can act on this. I have tested this code against 5.14 and 5.15.
#include "token.h" Token::Token(QObject *parent) : QObject(parent) { m_init = false; m_pBleController = nullptr; m_connected = false; } void Token::Connected() { qDebug("Connected"); setConnected(true); } void Token::Disconnected() { qDebug("Disconnected"); setConnected(false); } void Token::ConnectDevice(const QBluetoothDeviceInfo &device) { if (m_pBleController != nullptr) { if (m_pBleController->state() == QLowEnergyController::ConnectedState) { m_pBleController->disconnectFromDevice(); setConnected(false); } } qDebug("CreateController for %s", device.address().toString().toStdString().c_str()); m_pBleController = QLowEnergyController::createCentral(device, this); connect(m_pBleController, &QLowEnergyController::connected, this, &Token::connected); connect(m_pBleController, &QLowEnergyController::disconnected, this, &Token::Disconnected); connect(m_pBleController, static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error), this, [=](QLowEnergyController::Error error) { qDebug("Cannot connect to remote device: %d", error); }); m_pBleController->connectToDevice(); qDebug("Connect to %s started", device.address().toString().toStdString().c_str()); } void Token::FoundDevice(const QBluetoothDeviceInfo &device) { std::string deviceName = device.name().toStdString(); qDebug("Found BLE device: %s", deviceName.c_str()); if (deviceName == "TokenDevice") { m_blFinder.stop(); ConnectDevice(device); } } void Token::InitDisoveryAgent() { connect(&m_blFinder, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Token::FoundDevice); m_blFinder.setLowEnergyDiscoveryTimeout(5000); m_init = true; } bool Token::scanForToken() { if (m_init == false) { InitDisoveryAgent(); } m_blFinder.start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethod::LowEnergyMethod); return true; } bool Token::connected() const { return m_connected; } void Token::setConnected(bool connected) { if (m_connected == connected) return; m_connected = connected; emit connectedChanged(m_connected); }