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. SLOT is not executed after successfully contected to BLE-Device

SLOT is not executed after successfully contected to BLE-Device

Scheduled Pinned Locked Moved Solved General and Desktop
blebluetoothsignal & slotsignals slots
7 Posts 4 Posters 1.1k 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 24 Nov 2019, 20:20 last edited by SpaceToon
    #1

    Hello,

    I am writing a simple desktop application to find and connect to a bluetooth low energy device.
    With the code you see below I could discover and connect to a blueetooth low energy device. After the succesfull connection I would like to print all the services of my device. Therefore I wrote a Slot connectionSuccessfull. However it seems that this function is not executed at all, but the connect Function returns "true", when I print the return type.
    It only worked once when I started the code in debug mode. But then it did not work in debug mode either.
    Bzw: I know that the connection is successful because a LED turn on at my ble device.

    const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
    
    void Scanner::startDeviceDiscovery(){
    
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
        discoveryAgent->start();
    
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered);
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect);
    
    }
    
    
    void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
        qDebug() << "Found new device:" << device.name();
    }
    
    void Scanner::doConnect(){
        cout << "\nPlease enter the number of your device: ";
        int listNumber;
        cin >> listNumber;
    
        deviceList = discoveryAgent->discoveredDevices();
        adafruit = &deviceList[listNumber];
        qDebug() << "The element that you chose is: " + adafruit->name();
    
    
        controller = QLowEnergyController::createCentral(*adafruit);
        controller->connectToDevice();
        connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
    
    }
    
    
    void Scanner::connectionSuccessful(){
    
        qDebug()<<"succesfully connected";
        controller->discoverServices();
        QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
        const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
        for (const QLowEnergyCharacteristic &ch : characteristics) {
            qDebug() << ch.name();
        }
    
    }
    
    
    A J J 3 Replies Last reply 24 Nov 2019, 20:42
    0
    • S SpaceToon
      24 Nov 2019, 20:20

      Hello,

      I am writing a simple desktop application to find and connect to a bluetooth low energy device.
      With the code you see below I could discover and connect to a blueetooth low energy device. After the succesfull connection I would like to print all the services of my device. Therefore I wrote a Slot connectionSuccessfull. However it seems that this function is not executed at all, but the connect Function returns "true", when I print the return type.
      It only worked once when I started the code in debug mode. But then it did not work in debug mode either.
      Bzw: I know that the connection is successful because a LED turn on at my ble device.

      const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
      
      void Scanner::startDeviceDiscovery(){
      
          discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
          discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
          discoveryAgent->start();
      
          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered);
          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect);
      
      }
      
      
      void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device)
      {
          qDebug() << "Found new device:" << device.name();
      }
      
      void Scanner::doConnect(){
          cout << "\nPlease enter the number of your device: ";
          int listNumber;
          cin >> listNumber;
      
          deviceList = discoveryAgent->discoveredDevices();
          adafruit = &deviceList[listNumber];
          qDebug() << "The element that you chose is: " + adafruit->name();
      
      
          controller = QLowEnergyController::createCentral(*adafruit);
          controller->connectToDevice();
          connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
      
      }
      
      
      void Scanner::connectionSuccessful(){
      
          qDebug()<<"succesfully connected";
          controller->discoverServices();
          QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
          const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
          for (const QLowEnergyCharacteristic &ch : characteristics) {
              qDebug() << ch.name();
          }
      
      }
      
      
      J Offline
      J Offline
      JonB
      wrote on 25 Nov 2019, 08:36 last edited by JonB
      #5

      @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

      controller->connectToDevice();
      connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
      

      I don't really understand what it is you see or don't see. (Presumably you know whether it does indeed read an element number from stdin.) However, assuming controller->connectToDevice(); does the device connection, then you must not set your connected signal-->slot handler after the line where it connects, that's too late! You must always connect signals/slots on an object before any call which could raise a signal. Swap the order of these two lines, does your slot get called?

      1 Reply Last reply
      3
      • S SpaceToon
        24 Nov 2019, 20:20

        Hello,

        I am writing a simple desktop application to find and connect to a bluetooth low energy device.
        With the code you see below I could discover and connect to a blueetooth low energy device. After the succesfull connection I would like to print all the services of my device. Therefore I wrote a Slot connectionSuccessfull. However it seems that this function is not executed at all, but the connect Function returns "true", when I print the return type.
        It only worked once when I started the code in debug mode. But then it did not work in debug mode either.
        Bzw: I know that the connection is successful because a LED turn on at my ble device.

        const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
        
        void Scanner::startDeviceDiscovery(){
        
            discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
            discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
            discoveryAgent->start();
        
            connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered);
            connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect);
        
        }
        
        
        void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device)
        {
            qDebug() << "Found new device:" << device.name();
        }
        
        void Scanner::doConnect(){
            cout << "\nPlease enter the number of your device: ";
            int listNumber;
            cin >> listNumber;
        
            deviceList = discoveryAgent->discoveredDevices();
            adafruit = &deviceList[listNumber];
            qDebug() << "The element that you chose is: " + adafruit->name();
        
        
            controller = QLowEnergyController::createCentral(*adafruit);
            controller->connectToDevice();
            connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
        
        }
        
        
        void Scanner::connectionSuccessful(){
        
            qDebug()<<"succesfully connected";
            controller->discoverServices();
            QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
            const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
            for (const QLowEnergyCharacteristic &ch : characteristics) {
                qDebug() << ch.name();
            }
        
        }
        
        
        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 24 Nov 2019, 20:42 last edited by
        #2

        @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

        int listNumber;
        cin >> listNumber;
        

        What do you expect these lines to do in an UI program?

        Regards

        Qt has to stay free or it will die.

        S 1 Reply Last reply 25 Nov 2019, 08:19
        1
        • A aha_1980
          24 Nov 2019, 20:42

          @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

          int listNumber;
          cin >> listNumber;
          

          What do you expect these lines to do in an UI program?

          Regards

          S Offline
          S Offline
          SpaceToon
          wrote on 25 Nov 2019, 08:19 last edited by
          #3

          @aha_1980 said in SLOT is not executed after successfully contected to BLE-Device:

          @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

          int listNumber;
          cin >> listNumber;
          

          What do you expect these lines to do in an UI program?

          Regards

          Hello aha,

          this is just a console application. The devices are printed from a list to the console, and the user can enter a number (the list element of the device he/she wants to connect to), then this paritcular device is saved in the adafruit variable

          adafruit = &deviceList[listNumber];
          

          It's just a temporary solution to see how Bluetooth Low Energy works in Qt. After I understand all this I will create a real application with GUI. As I mentioned, the connection is established successfully, however the slot connectionSuccessful() is not executed.

          A 1 Reply Last reply 25 Nov 2019, 08:29
          0
          • S SpaceToon
            25 Nov 2019, 08:19

            @aha_1980 said in SLOT is not executed after successfully contected to BLE-Device:

            @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

            int listNumber;
            cin >> listNumber;
            

            What do you expect these lines to do in an UI program?

            Regards

            Hello aha,

            this is just a console application. The devices are printed from a list to the console, and the user can enter a number (the list element of the device he/she wants to connect to), then this paritcular device is saved in the adafruit variable

            adafruit = &deviceList[listNumber];
            

            It's just a temporary solution to see how Bluetooth Low Energy works in Qt. After I understand all this I will create a real application with GUI. As I mentioned, the connection is established successfully, however the slot connectionSuccessful() is not executed.

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 25 Nov 2019, 08:29 last edited by
            #4

            Hi @SpaceToon,

            then make sure this program runs in a terminal, not in Creators Application Output, which has no input capabilities.

            If you still encounter problems, you will need to share more code, otherwise we cannot tell you where the problem is.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            5
            • S SpaceToon
              24 Nov 2019, 20:20

              Hello,

              I am writing a simple desktop application to find and connect to a bluetooth low energy device.
              With the code you see below I could discover and connect to a blueetooth low energy device. After the succesfull connection I would like to print all the services of my device. Therefore I wrote a Slot connectionSuccessfull. However it seems that this function is not executed at all, but the connect Function returns "true", when I print the return type.
              It only worked once when I started the code in debug mode. But then it did not work in debug mode either.
              Bzw: I know that the connection is successful because a LED turn on at my ble device.

              const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
              
              void Scanner::startDeviceDiscovery(){
              
                  discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
                  discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
                  discoveryAgent->start();
              
                  connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered);
                  connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect);
              
              }
              
              
              void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device)
              {
                  qDebug() << "Found new device:" << device.name();
              }
              
              void Scanner::doConnect(){
                  cout << "\nPlease enter the number of your device: ";
                  int listNumber;
                  cin >> listNumber;
              
                  deviceList = discoveryAgent->discoveredDevices();
                  adafruit = &deviceList[listNumber];
                  qDebug() << "The element that you chose is: " + adafruit->name();
              
              
                  controller = QLowEnergyController::createCentral(*adafruit);
                  controller->connectToDevice();
                  connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
              
              }
              
              
              void Scanner::connectionSuccessful(){
              
                  qDebug()<<"succesfully connected";
                  controller->discoverServices();
                  QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
                  const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
                  for (const QLowEnergyCharacteristic &ch : characteristics) {
                      qDebug() << ch.name();
                  }
              
              }
              
              
              J Offline
              J Offline
              JonB
              wrote on 25 Nov 2019, 08:36 last edited by JonB
              #5

              @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

              controller->connectToDevice();
              connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
              

              I don't really understand what it is you see or don't see. (Presumably you know whether it does indeed read an element number from stdin.) However, assuming controller->connectToDevice(); does the device connection, then you must not set your connected signal-->slot handler after the line where it connects, that's too late! You must always connect signals/slots on an object before any call which could raise a signal. Swap the order of these two lines, does your slot get called?

              1 Reply Last reply
              3
              • S SpaceToon
                24 Nov 2019, 20:20

                Hello,

                I am writing a simple desktop application to find and connect to a bluetooth low energy device.
                With the code you see below I could discover and connect to a blueetooth low energy device. After the succesfull connection I would like to print all the services of my device. Therefore I wrote a Slot connectionSuccessfull. However it seems that this function is not executed at all, but the connect Function returns "true", when I print the return type.
                It only worked once when I started the code in debug mode. But then it did not work in debug mode either.
                Bzw: I know that the connection is successful because a LED turn on at my ble device.

                const QBluetoothUuid adafruitServiceUuid = (QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
                
                void Scanner::startDeviceDiscovery(){
                
                    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
                    discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
                    discoveryAgent->start();
                
                    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Scanner::deviceDiscovered);
                    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Scanner::doConnect);
                
                }
                
                
                void Scanner::deviceDiscovered(const QBluetoothDeviceInfo &device)
                {
                    qDebug() << "Found new device:" << device.name();
                }
                
                void Scanner::doConnect(){
                    cout << "\nPlease enter the number of your device: ";
                    int listNumber;
                    cin >> listNumber;
                
                    deviceList = discoveryAgent->discoveredDevices();
                    adafruit = &deviceList[listNumber];
                    qDebug() << "The element that you chose is: " + adafruit->name();
                
                
                    controller = QLowEnergyController::createCentral(*adafruit);
                    controller->connectToDevice();
                    connect(controller, &QLowEnergyController::connected, this, &Scanner::connectionSuccessful);
                
                }
                
                
                void Scanner::connectionSuccessful(){
                
                    qDebug()<<"succesfully connected";
                    controller->discoverServices();
                    QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
                    const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
                    for (const QLowEnergyCharacteristic &ch : characteristics) {
                        qDebug() << ch.name();
                    }
                
                }
                
                
                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 25 Nov 2019, 10:11 last edited by J.Hilk
                #6

                @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

                ontroller->discoverServices();

                additionally to what @aha_1980 and @JonB said, the discoverServices call is asynchronous and you'll have to listen to the QLowEnergyController::serviceDiscovered signal before you can print them.

                This can't work:

                void Scanner::connectionSuccessful(){
                
                    qDebug()<<"succesfully connected";
                    controller->discoverServices();
                    QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
                    const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
                    for (const QLowEnergyCharacteristic &ch : characteristics) {
                        qDebug() << ch.name();
                    }
                
                }
                

                That said, you do know, that Qt offers a ready to compile BTLE Scanner example, that prints out (Gui form) all services & characteristics of BTLE devices ?

                https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                S 1 Reply Last reply 29 Nov 2019, 14:14
                5
                • J J.Hilk
                  25 Nov 2019, 10:11

                  @SpaceToon said in SLOT is not executed after successfully contected to BLE-Device:

                  ontroller->discoverServices();

                  additionally to what @aha_1980 and @JonB said, the discoverServices call is asynchronous and you'll have to listen to the QLowEnergyController::serviceDiscovered signal before you can print them.

                  This can't work:

                  void Scanner::connectionSuccessful(){
                  
                      qDebug()<<"succesfully connected";
                      controller->discoverServices();
                      QLowEnergyService *service = controller->createServiceObject(adafruitServiceUuid);
                      const QList<QLowEnergyCharacteristic> characteristics = service->characteristics();
                      for (const QLowEnergyCharacteristic &ch : characteristics) {
                          qDebug() << ch.name();
                      }
                  
                  }
                  

                  That said, you do know, that Qt offers a ready to compile BTLE Scanner example, that prints out (Gui form) all services & characteristics of BTLE devices ?

                  https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html

                  S Offline
                  S Offline
                  SpaceToon
                  wrote on 29 Nov 2019, 14:14 last edited by SpaceToon
                  #7

                  @J-Hilk Thank you, JonB and you were right. For me it actually makes more sense that e.g. at first you connect to the device and then you say "okay, if the signal "connected" is emitted then call the function x.y.". So after changing it, it works now. However, after getting all services and calling "service->discoverDetails" and then "service->characteristics" the characteristics List is empty.

                  After connection is successfull:

                  void Scanner::startServiceDiscovery(){
                      connect(controller, &QLowEnergyController::discoveryFinished, this, &Scanner::discoveryFinished);
                      controller->discoverServices();
                  }
                  
                  
                  void Scanner::discoveryFinished(){
                      qDebug() << "\nService Discovery finished. Following Services found:\n";
                      QList<QBluetoothUuid> serviceList = controller->services();
                  
                      for(QBluetoothUuid &sl : serviceList){
                          qDebug() <<  controller->createServiceObject(sl)->serviceName() << "Uuid: " << sl;
                      }
                  
                      uartService = controller->createServiceObject(adafruitServiceUuid);
                  
                      qDebug() <<"\nChose the following service: " << (*uartService).serviceName();
                      
                      connect(uartService, &QLowEnergyService::stateChanged, this, &Scanner::printChars);
                      qDebug() << uartService->state(); //here the state is QLowEnergyService::DiscoveryRequired
                      uartService->discoverDetails(); 
                  }
                  
                  
                  void Scanner::printChars(){
                  
                      qDebug() << uartService->state(); // here the state is now QLowEnergyService::DiscoveringServices
                      
                  
                      const QList<QLowEnergyCharacteristic> chars = uartService->characteristics();
                      qDebug()<< chars.size(); //however the list size is 0
                      for (const QLowEnergyCharacteristic &ch : chars) {
                          qDebug() << &ch;
                      }
                  }
                  
                  

                  btw: I know that there is an example for BLE Scanner. But for a project, I want to create an own desktop application which connnects to a peripheral and live plots the data which are received from the sensor with qcustomplot. So I need a QWidget Application because QCustomPlot is a widget...

                  1 Reply Last reply
                  0

                  7/7

                  29 Nov 2019, 14:14

                  • Login

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