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 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();
        }
    
    }
    
    
    aha_1980A JonBJ J.HilkJ 3 Replies Last reply
    0
    • S SpaceToon

      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();
          }
      
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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

        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();
            }
        
        }
        
        
        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 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
        1
        • aha_1980A aha_1980

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

          aha_1980A 1 Reply Last reply
          0
          • S SpaceToon

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

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 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

              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();
                  }
              
              }
              
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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

                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.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on 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
                5
                • J.HilkJ J.Hilk

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

                  • Login

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