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.3k 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.
  • J jsulm
    11 Dec 2019, 08:53

    @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 11 Dec 2019, 09:12 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
      11 Dec 2019, 09:00

      @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 11 Dec 2019, 09:18 last edited by Timur Pocheptsov 12 Nov 2019, 09:19
      #11

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

      1 Reply Last reply
      0
      • J jsulm
        11 Dec 2019, 09:03

        @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 11 Dec 2019, 09:22 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
          10 Dec 2019, 10:58

          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 11 Dec 2019, 13:21 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 11 Dec 2019, 19:09
          0
          • T Timur Pocheptsov
            11 Dec 2019, 13:21

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

            S Offline
            S Offline
            SpaceToon
            wrote on 11 Dec 2019, 19:09 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 12 Dec 2019, 18:52
            0
            • S SpaceToon
              11 Dec 2019, 19:09

              @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 12 Dec 2019, 18:52 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 13 Dec 2019, 12:58
              2
              • T Timur Pocheptsov
                12 Dec 2019, 18:52

                @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 13 Dec 2019, 12:58 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 23 Jan 2020, 13:52 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 7 Feb 2020, 11:16
                  0
                  • H harip
                    23 Jan 2020, 13:52

                    @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 7 Feb 2020, 11:16 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 11 Nov 2021, 13:43 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