Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bluetooth Low Energy: State 'ServiceDiscovered' is never reached

Bluetooth Low Energy: State 'ServiceDiscovered' is never reached

Scheduled Pinned Locked Moved Unsolved General and Desktop
bluetoothbluetooth low eblesignal & slot
19 Posts 5 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SpaceToon
    wrote on last edited by SpaceToon
    #1

    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;
        }
    }
    
    
    T 1 Reply Last reply
    0
    • T Offline
      T Offline
      Timur Pocheptsov
      wrote on last edited by Timur Pocheptsov
      #2

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

      S 1 Reply Last reply
      1
      • T Timur Pocheptsov

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

        S Offline
        S Offline
        SpaceToon
        wrote on last edited by
        #3

        @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.

        T 1 Reply Last reply
        0
        • S SpaceToon

          @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.

          T Offline
          T Offline
          Timur Pocheptsov
          wrote on last edited by
          #4

          @SpaceToon

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

          S 1 Reply Last reply
          0
          • T Timur Pocheptsov

            @SpaceToon

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

            S Offline
            S Offline
            SpaceToon
            wrote on last edited by
            #5

            @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..

            T 1 Reply Last reply
            0
            • S SpaceToon

              @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..

              T Offline
              T Offline
              Timur Pocheptsov
              wrote on last edited by
              #6

              @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).

              jsulmJ 1 Reply Last reply
              1
              • T Timur Pocheptsov

                @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).

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S T 2 Replies Last reply
                0
                • jsulmJ jsulm

                  @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.

                  S Offline
                  S Offline
                  SpaceToon
                  wrote on last edited by
                  #8

                  @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"?

                  jsulmJ T 2 Replies Last reply
                  0
                  • S SpaceToon

                    @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"?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #9

                    @SpaceToon

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @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.

                      T Offline
                      T Offline
                      Timur Pocheptsov
                      wrote on last edited by
                      #10

                      @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)

                      1 Reply Last reply
                      1
                      • S SpaceToon

                        @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"?

                        T Offline
                        T Offline
                        Timur Pocheptsov
                        wrote on last edited by Timur Pocheptsov
                        #11

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

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @SpaceToon

                          int main(int argc, char* argv[])
                          {
                              QCoreApplication app(argc, argv);
                          
                              return app->exec(); // This starts the event loop
                          }
                          
                          S Offline
                          S Offline
                          SpaceToon
                          wrote on last edited by
                          #12

                          @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.

                          1 Reply Last reply
                          0
                          • S SpaceToon

                            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;
                                }
                            }
                            
                            
                            T Offline
                            T Offline
                            Timur Pocheptsov
                            wrote on last edited by
                            #13

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

                            S 1 Reply Last reply
                            0
                            • T Timur Pocheptsov

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

                              S Offline
                              S Offline
                              SpaceToon
                              wrote on last edited by
                              #14

                              @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) .

                              T 1 Reply Last reply
                              0
                              • S SpaceToon

                                @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) .

                                T Offline
                                T Offline
                                Timur Pocheptsov
                                wrote on last edited by
                                #15

                                @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.

                                S 1 Reply Last reply
                                2
                                • T Timur Pocheptsov

                                  @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.

                                  S Offline
                                  S Offline
                                  SpaceToon
                                  wrote on last edited by
                                  #16

                                  @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.

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    harip
                                    wrote on last edited by
                                    #17

                                    @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.

                                    S 1 Reply Last reply
                                    0
                                    • H harip

                                      @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.

                                      S Offline
                                      S Offline
                                      SpaceToon
                                      wrote on last edited by
                                      #18

                                      @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.

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        Dihambo
                                        wrote on last edited by
                                        #19

                                        The QTBluetooth Central API is not supported on any platform.
                                        see the first table in Qt Bluetooth

                                        1 Reply Last reply
                                        1

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved