Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Bluetooth Low Energy: State 'ServiceDiscovered' is never reached



  • Hello guys,

    I am about to develop a console application with which I can search for Bluetooth Low Energy devices, connect to one, choose the services and show the characteristics.
    After successfull connection, I am able to discover the services and create a service with a given UUID. However, by calling discover details I expect the state() transitions from DiscoveryRequired via DiscoveringServices to its final ServiceDiscovered state like it is described here. But the final state "ServiceDiscovered" with which I am able to read or write characteristics, is never reached in my application it remains DiscoveringServices the whole time.
    When using the BLE Scanner example from Qt, the characteristics from my device are discovered, so everything's fine with my BLE device.

    Here is my code, after successfull connection:

    void Scanner::startServiceDiscovery(){
        connect(controller, &QLowEnergyController::discoveryFinished, this, &Scanner::discoveryFinished);
        controller->discoverServices();
    }
    
    
    void Scanner::discoveryFinished(){
        qDebug() << "\n Service Discovery finished.";
        QList<QBluetoothUuid> serviceList = controller->services();
    
        QLowEnergyService *uartService = controller->createServiceObject(adafruitServiceUuid, this);
    
        connect(uartService, &QLowEnergyService::stateChanged, this, &Scanner::printChars);
        uartService->discoverDetails();
    }
    
    
    
    void Scanner::printChars(QLowEnergyService::ServiceState newState){
    
        auto service = qobject_cast<QLowEnergyService *>(sender());
    
        switch(newState){
    
        case QLowEnergyService::DiscoveringServices:
            qDebug() << "\n Current ServiceState: Dsicovering services";
            break;
    
        case QLowEnergyService::ServiceDiscovered:
        {
            qDebug() << "Current ServiceState: ServiceDiscovered";
    
            const QList<QLowEnergyCharacteristic> chars = service->characteristics();
            qDebug()<< "Size in printChars Slot" << chars.size();
            for (const QLowEnergyCharacteristic &ch : chars) {
                qDebug() << &ch;
            }
            break;
        }
    
        default:
            qDebug()<<"Default";
            break;
        }
    }
    
    


  • I maybe missing it (thought read twice): which platform is it? And the version of Qt?



  • @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    I maybe missing it (thought read twice): which platform is it? And the version of Qt?

    Oh sorry, my fault.
    It is Windows 10 Home 64 bit.



  • @SpaceToon

    Please, consider creating bug report with a minimal but complete reproducer?



  • @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @SpaceToon

    Please, consider creating bug report with a minimal but complete reproducer?

    Well, as I said, with the Blueooth Low Energy Scanner example from Qt it works - the state "ServiceDiscovered" is reached and I cann retrieve the characteristics of a chosen service from my BLE device. Therefore I think that this might not be a bug but something wrong with my code or something like this..



  • @SpaceToon Well, but scanner example is not a console application, right? As an example on macOS, some parts of QtBluetooth (mostly Bluetooth Classic) will not work in a console application (and for Bluetooth LE you would need a special event dispatcher).


  • Qt Champions 2019

    @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    will not work in a console application

    Why should it not? You need to make sure Qt event loop is running.



  • @jsulm said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    will not work in a console application

    Why should it not? You need to make sure Qt event loop is running.

    So how to make sure Qt event loop is running? Cuase I am new to Qt.
    Because The first output of my application with the above code is "\n Current ServiceState: Dsicovering services" and that is right.
    When I turn off my Bluetooth then "Default" is printed to the console because the state must then be "InvalidService". So do you mean this by saying "Qt event loop is running"?


  • Qt Champions 2019

    @SpaceToon

    int main(int argc, char* argv[])
    {
        QCoreApplication app(argc, argv);
    
        return app->exec(); // This starts the event loop
    }
    


  • @jsulm you need a working run loop under the hood, which is not the case for qeventdispatcher_unix (I am talking about Apple-specific CFRunLoop/NSRunLoop, not sure about Windows, was just guessing)



  • @SpaceToon Could you, then, post the code here, to check for some simple errors/mistakes?



  • @jsulm said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @SpaceToon

    int main(int argc, char* argv[])
    {
        QCoreApplication app(argc, argv);
    
        return app->exec(); // This starts the event loop
    }
    

    Yes, I have exactly the same code in my main method.



  • @SpaceToon Could you also connect to the server's 'error' signal to check if any error happens?



  • @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @SpaceToon Could you also connect to the server's 'error' signal to check if any error happens?

    Hello, could you give me a piece of code for doing this? I do not really know how to do so thank you :)

    @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @SpaceToon Could you, then, post the code here, to check for some simple errors/mistakes?

    Here is the code. It is just for understanding how BLE works in Qt it is not my final code therefore everything is in one file (there are also header files in which the slots, signals etc. are declared):

    /*Uuid of the service from which I want to have the characteristics printed*/
    const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
    
    /*Discovering Bluetooth devices*/
    void Scanner::startDeviceDiscovery(){
    
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered);
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect);
    
        discoveryAgent->start();
    }
    
    /*Prints the name of the device that is discovered*/
    void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
        qDebug() << "Found new device:" << device.name();
    }
    
    
    void Scanner::doConnect(){
    
    /*All devices are listed in a list. When I see my BLE device, I enter the list element and then */
    /*create a connection to this certain ble device*/
        cout << "\nPlease enter the number of your device: ";
        int listNumber;
        cin >> listNumber;
    
        deviceList = discoveryAgent->discoveredDevices();
        adafruit = &deviceList[listNumber];
        qDebug() << "\nThe element that you chose is: " + adafruit->name();
    
        controller = QLowEnergyController::createCentral(*adafruit);
        connect(controller, &QLowEnergyController::connected, this, &Scanner::startServiceDiscovery);
        controller->connectToDevice();
    }
    
    
    void Scanner::startServiceDiscovery(){
        connect(controller, &QLowEnergyController::discoveryFinished, this, &Scanner::discoveryFinished);
        controller->discoverServices();
    }
    
    
    void Scanner::discoveryFinished(){
        qDebug() << "\n Service Discovery finished.";
        QList<QBluetoothUuid> serviceList = controller->services();
    
        QLowEnergyService *uartService = controller->createServiceObject(adafruitServiceUuid, this);
    
        connect(uartService, &QLowEnergyService::stateChanged, this, &Scanner::printChars);
        uartService->discoverDetails();
    
    }
    
    
    
    void Scanner::printChars(QLowEnergyService::ServiceState newState){
    
        auto service = qobject_cast<QLowEnergyService *>(sender());
    
        switch(newState){
    
        case QLowEnergyService::DiscoveringServices:
            qDebug() << "\n Current ServiceState: Dsicovering services";
            break;
    
        case QLowEnergyService::ServiceDiscovered:
        {
    
            qDebug() << "Current ServiceState: ServiceDiscovered";
    
            const QList<QLowEnergyCharacteristic> chars = service->characteristics();
            qDebug()<< "Size in printChars Slot" << chars.size();
            for (const QLowEnergyCharacteristic &ch : chars) {
                qDebug() << &ch;
            }
            break;
        }
    
        default:
            qDebug()<<"Default";
            break;
        }
    }
    
    

    This is the output:

    9777b5fc-8198-45cd-95ea-42d946d43914-image.png

    The connection is successfully created (a blue LED turns on on my BLE device which indicates a succerssfull connection) .



  • @SpaceToon I suggest you to create a bug report (please, attach a complete minimal project you are using). In JIRA we can assign it to Denis (who's the author of this back-end), so maybe at least he has an immediate idea of what's wrong. Thanks.



  • @Timur-Pocheptsov said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @SpaceToon I suggest you to create a bug report (please, attach a complete minimal project you are using). In JIRA we can assign it to Denis (who's the author of this back-end), so maybe at least he has an immediate idea of what's wrong. Thanks.

    Okay, thanks. I created the Bug report here, but I could not assign it to someone.



  • @SpaceToon Is this issue resolved? We are also facing the same issue in Windows 10. As you mentioned, the BLE scanner example is working perfectly fine.



  • @harip said in Bluetooth Low Energy: State 'ServiceDiscovered' is never reached:

    @SpaceToon Is this issue resolved? We are also facing the same issue in Windows 10. As you mentioned, the BLE scanner example is working perfectly fine.

    Hey,
    not it is not fixed yet, but a developer is investigating the issue. You can track the current status of the bug here in Jira.