Qt World Summit: Register Today!

Debugging Bluetooth

  • I have cut the heartlistener example down to a minimal version that does nothing more than search for devices...and as much as I have tried to clone the code that I need from the example....it does not find any devices (yet the example works fine).

    I can post the code but was wondering if anyone could suggest things for me to look at in hopes that I can solve it myself.

    On a related question, I am having trouble getting logging out. I have the following in code:


    but no messages. I am new to Qt so am sure that I am missing something obvious.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Your filter line is wrong, the separator should be a semicolon.

  • Thanks, not sure where I copied that from but even with just one filter I am still not getting output. I think my problem must be more fundamental...as in how to turn on logging in the first place? I would have thought that a google would have answered my question of how to do this but alas I am clearly missing something!

  • Lifetime Qt Champion

    Then maybe a silly question but are you sure you are initialising everything like the example ?

  • @SGaist I have tried to cut the example down to bare minimums so I could focus on the bluetooth flow...I think that I am doing everything the same...but based on the results I am absolutely sure that I am NOT! Hence my hope of seeing the messages!!!! I have attached the code in case you or anything else can take a look :-o


    QT += bluetooth quick
    CONFIG += c++11
    TARGET = TestBt
    CONFIG += console
    TEMPLATE = app
    SOURCES += main.cpp \
        discoveryAgent.cpp \
    HEADERS += \
        main.h \
        discoveryAgent.h \


    /#include <qbluetoothaddress.h>
    #include <qbluetoothdevicediscoveryagent.h>
    #include <qbluetoothlocaldevice.h>
    #include <qbluetoothdeviceinfo.h>
    #include <qbluetoothservicediscoveryagent.h>
    #include <QDebug>
    #include <QList>
    #include <QTimer>
    #include <QCoreApplication>
    #include <QtCore/QLoggingCategory>
    #include <QMessageLogger>
    #include <discoveryAgent.h>
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        QBluetoothLocalDevice localDevice;
        QString localDeviceName;
        const QBluetoothAddress adapter = QBluetoothAddress() ;
        discoveryAgent bT;
        return a.exec();

    main.h is effectively empty


    #include <QBluetoothDeviceDiscoveryAgent>
    #include <discoveryAgent.h>
    #include <QtCore/QDebug>
    #include <QtEndian>
    discoveryAgent::discoveryAgent(): m_currentDevice(QBluetoothDeviceInfo()),
         connected(false), controller(0), m_deviceScanState(false), randomAddress(false)
        m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        connect(m_deviceDiscoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
                this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
        connect(m_deviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
                this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
        connect(m_deviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
        if (m_deviceDiscoveryAgent->isActive()) {
            m_deviceScanState = true;
            qDebug() << "Active";
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(waitForUpdate()));
    // In your local slot, read information about the found devices
    void discoveryAgent::deviceDiscovered(const QBluetoothDeviceInfo &device)
        qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
    void discoveryAgent::waitForUpdate()
        qDebug() << "----";
        if (m_deviceDiscoveryAgent->isActive()) {
            m_deviceScanState = true;
            qDebug() << "Active";
    void discoveryAgent::addDevice(const QBluetoothDeviceInfo &device)
        qDebug() << "addDevice";
        if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
            qWarning() << "Discovered LE Device name: " << device.name() << " Address: "
                       << device.address().toString();
            DeviceInfo *dev = new DeviceInfo(device);
            qDebug() << "Low Energy device found. Scanning for more...";
    void discoveryAgent::scanFinished()
        qDebug() << "scanFinished";
        if (m_devices.size() == 0)
            qDebug() << "No Low Energy devices found";
    void discoveryAgent::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
        if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
            qDebug() << "The Bluetooth adaptor is powered off, power it on before doing discovery.";
        else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
            qDebug() << "Writing or reading from the device resulted in an error.";
            qDebug() <<"An unknown error has occurred.";


    #include <QString>
    #include <QDebug>
    #include <QDateTime>
    #include <QVector>
    #include <QTimer>
    #include <QBluetoothDeviceDiscoveryAgent>
    #include <QBluetoothDeviceInfo>
    #include <QLowEnergyController>
    #include <QLowEnergyService>
    #include "deviceinfo.h"
    QT_FORWARD_DECLARE_CLASS (QBluetoothDeviceInfo)
    QT_FORWARD_DECLARE_CLASS (QBluetoothServiceInfo)
    class discoveryAgent: public QObject
        QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent;
        DeviceInfo m_currentDevice;
        QList<QObject*> m_devices;
        bool connected;
        QLowEnergyController *controller;
        bool m_deviceScanState;
        bool randomAddress;
    private slots:
        void waitForUpdate();
        void deviceDiscovered(const QBluetoothDeviceInfo &device);
        void addDevice(const QBluetoothDeviceInfo&);
        void scanFinished();
        void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
    #endif // DISCOVERYAGENT_H

  • This post is deleted!

Log in to reply