Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Bluetooth - no devices discovered.
Qt 6.12 LTS is out! See what's new in the release blog

Qt Bluetooth - no devices discovered.

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 4 Posters 29.6k 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.
  • jsulmJ jsulm

    This post is deleted!

    J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #14

    @jsulm said in Qt Bluetooth - no devices discovered.:

    @jenya7 And why is deviceScanFinished private?
    It is actually apparent that it should be public...

    it's an official example in the examples folder (C:\Qt\Examples\Qt-5.12.0\bluetooth\lowenergyscanner).
    I wouldn't dare to change it, only if you say so.

    jsulmJ 1 Reply Last reply
    0
    • J jenya7

      @jsulm said in Qt Bluetooth - no devices discovered.:

      @jenya7 And why is deviceScanFinished private?
      It is actually apparent that it should be public...

      it's an official example in the examples folder (C:\Qt\Examples\Qt-5.12.0\bluetooth\lowenergyscanner).
      I wouldn't dare to change it, only if you say so.

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

      @jenya7 Please read my post above your last post...

      J 1 Reply Last reply
      0
      • jsulmJ jsulm

        @jenya7 Please read my post above your last post...

        J Offline
        J Offline
        jenya7
        wrote on last edited by jenya7
        #16

        @jsulm
        What if I do deviceScanFinished public?

        OK. I see what's going on. Just a strong encapsulation.

        The signal already set

        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);
        

        and it sets m_deviceScanState

        void Device::deviceScanFinished()
        {
            emit devicesUpdated();
            m_deviceScanState = false;
            emit stateChanged();
            if (devices.isEmpty())
                setUpdate("No Low Energy devices found...");
            else
                setUpdate("Done! Scan Again!");
        }
        

        so my

         while (bt_device.state()) ;
        

        actually tests the memeber

        bool Device::state()
        {
            return m_deviceScanState;
        }
        

        so deviceScanFinished never occurs.

        jsulmJ 1 Reply Last reply
        0
        • J jenya7

          @jsulm
          What if I do deviceScanFinished public?

          OK. I see what's going on. Just a strong encapsulation.

          The signal already set

          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);
          

          and it sets m_deviceScanState

          void Device::deviceScanFinished()
          {
              emit devicesUpdated();
              m_deviceScanState = false;
              emit stateChanged();
              if (devices.isEmpty())
                  setUpdate("No Low Energy devices found...");
              else
                  setUpdate("Done! Scan Again!");
          }
          

          so my

           while (bt_device.state()) ;
          

          actually tests the memeber

          bool Device::state()
          {
              return m_deviceScanState;
          }
          

          so deviceScanFinished never occurs.

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

          @jenya7 said in Qt Bluetooth - no devices discovered.:

          What if I do deviceScanFinished public?

          To achieve what exactly? deviceScanFinished is a slot not signal in Device - how will making it public help other classes?

          "The signal already set" - the signal from private member is "set", this does not help other classes as you already discovered.

          Why don't you try my approach? Signals/slots are one of the most important features in Qt and it is really not much work:

          class Device...
          {
          signals:
              void scanFinished();
          }
          
          // In Device constructor:
          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);
          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::scanFinished);
          
          // Now in some other class you simply do:
          connect(&bt_device, &Device::scanFinished, this, &SomeOtherClass::someSlot);
          

          Isn't this easy and clear? And you do not expose any private members from Device to outer world (which would be bad design).

          J 1 Reply Last reply
          1
          • jsulmJ jsulm

            @jenya7 said in Qt Bluetooth - no devices discovered.:

            What if I do deviceScanFinished public?

            To achieve what exactly? deviceScanFinished is a slot not signal in Device - how will making it public help other classes?

            "The signal already set" - the signal from private member is "set", this does not help other classes as you already discovered.

            Why don't you try my approach? Signals/slots are one of the most important features in Qt and it is really not much work:

            class Device...
            {
            signals:
                void scanFinished();
            }
            
            // In Device constructor:
            connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);
            connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::scanFinished);
            
            // Now in some other class you simply do:
            connect(&bt_device, &Device::scanFinished, this, &SomeOtherClass::someSlot);
            

            Isn't this easy and clear? And you do not expose any private members from Device to outer world (which would be bad design).

            J Offline
            J Offline
            jenya7
            wrote on last edited by jenya7
            #18

            @jsulm
            It seems OK. But here in the constructor

            BT_DEVICE::BT_DEVICE(QObject *parent) : QObject(parent)
            {
                connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
            }
            

            I get - "undefined reference to `vtable for BT_DEVICE' ". And I don't have any virtual function.

            jsulmJ 1 Reply Last reply
            0
            • J jenya7

              @jsulm
              It seems OK. But here in the constructor

              BT_DEVICE::BT_DEVICE(QObject *parent) : QObject(parent)
              {
                  connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
              }
              

              I get - "undefined reference to `vtable for BT_DEVICE' ". And I don't have any virtual function.

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

              @jenya7 Please read https://doc.qt.io/qt-5/signalsandslots.html
              If you want to have signals and/or slots in a class you have to derive this class from QObject and add Q_OBJECT macro right at the beginning of your class.

              #include <QObject>
              
              class BT_DEVICE : public QObject
              {
              Q_OBJECT
              
              signals:
                  void scanFinished();
              ...
              

              Do a complete rebuild of your project after these changes.

              J 1 Reply Last reply
              0
              • jsulmJ jsulm

                @jenya7 Please read https://doc.qt.io/qt-5/signalsandslots.html
                If you want to have signals and/or slots in a class you have to derive this class from QObject and add Q_OBJECT macro right at the beginning of your class.

                #include <QObject>
                
                class BT_DEVICE : public QObject
                {
                Q_OBJECT
                
                signals:
                    void scanFinished();
                ...
                

                Do a complete rebuild of your project after these changes.

                J Offline
                J Offline
                jenya7
                wrote on last edited by
                #20

                @jsulm
                this is h file

                class BT_DEVICE : public QObject
                {
                    Q_OBJECT
                
                    public:
                    BT_DEVICE(QObject *parent = nullptr);
                    void BLE_Scan();
                
                signals:
                    void ScanFinished();
                };
                

                and cpp file

                BT_DEVICE::BT_DEVICE(QObject *parent) : QObject(parent)
                {
                    connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
                }
                
                void BT_DEVICE::BLE_Scan()
                {
                   scanning = 1;
                
                   bt_device.startDeviceDiscovery();
                
                   while (scanning) ;
                
                   QVariant devices = bt_device.getDevices();
                }
                
                void BT_DEVICE::ScanFinished()
                {
                    scanning = 0;
                }
                
                jsulmJ 1 Reply Last reply
                0
                • J jenya7

                  @jsulm
                  this is h file

                  class BT_DEVICE : public QObject
                  {
                      Q_OBJECT
                  
                      public:
                      BT_DEVICE(QObject *parent = nullptr);
                      void BLE_Scan();
                  
                  signals:
                      void ScanFinished();
                  };
                  

                  and cpp file

                  BT_DEVICE::BT_DEVICE(QObject *parent) : QObject(parent)
                  {
                      connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
                  }
                  
                  void BT_DEVICE::BLE_Scan()
                  {
                     scanning = 1;
                  
                     bt_device.startDeviceDiscovery();
                  
                     while (scanning) ;
                  
                     QVariant devices = bt_device.getDevices();
                  }
                  
                  void BT_DEVICE::ScanFinished()
                  {
                      scanning = 0;
                  }
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #21

                  @jenya7 said in Qt Bluetooth - no devices discovered.:

                  while (scanning) ;

                  Not related to your current problem: this is a no go in an event driven application! You are blocking the event loop! This means that no signals will be handled. Redesign this part of your app.

                  Back to your current problem: I did a mistake above. BT_DEVICE::ScanFinished should be a slot of course.

                  J 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @jenya7 said in Qt Bluetooth - no devices discovered.:

                    while (scanning) ;

                    Not related to your current problem: this is a no go in an event driven application! You are blocking the event loop! This means that no signals will be handled. Redesign this part of your app.

                    Back to your current problem: I did a mistake above. BT_DEVICE::ScanFinished should be a slot of course.

                    J Offline
                    J Offline
                    jenya7
                    wrote on last edited by
                    #22

                    @jsulm
                    yes. it was stupid of me to block. for now
                    h

                    class BT_DEVICE : public QObject
                    {
                        Q_OBJECT
                    
                        public:
                        BT_DEVICE(QObject *parent = nullptr);
                        void BLE_Scan();
                    
                    public slots:
                        void ScanFinished();
                    };
                    

                    cpp

                    BT_DEVICE::BT_DEVICE(QObject *parent) : QObject(parent)
                    {
                        //for debug
                        //connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
                    }
                    
                    void BT_DEVICE::BLE_Scan()
                    {
                       scanning = 1;
                    
                       bt_device.startDeviceDiscovery();
                    }
                    
                    void BT_DEVICE::ScanFinished()
                    {
                        QVariant devices = bt_device.getDevices();
                    }
                    

                    Is it OK?

                    jsulmJ 1 Reply Last reply
                    0
                    • J jenya7

                      @jsulm
                      yes. it was stupid of me to block. for now
                      h

                      class BT_DEVICE : public QObject
                      {
                          Q_OBJECT
                      
                          public:
                          BT_DEVICE(QObject *parent = nullptr);
                          void BLE_Scan();
                      
                      public slots:
                          void ScanFinished();
                      };
                      

                      cpp

                      BT_DEVICE::BT_DEVICE(QObject *parent) : QObject(parent)
                      {
                          //for debug
                          //connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
                      }
                      
                      void BT_DEVICE::BLE_Scan()
                      {
                         scanning = 1;
                      
                         bt_device.startDeviceDiscovery();
                      }
                      
                      void BT_DEVICE::ScanFinished()
                      {
                          QVariant devices = bt_device.getDevices();
                      }
                      

                      Is it OK?

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

                      @jenya7 said in Qt Bluetooth - no devices discovered.:

                      Is it OK?

                      Why did you comment out the connect?

                      J 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @jenya7 said in Qt Bluetooth - no devices discovered.:

                        Is it OK?

                        Why did you comment out the connect?

                        J Offline
                        J Offline
                        jenya7
                        wrote on last edited by jenya7
                        #24

                        @jsulm said in Qt Bluetooth - no devices discovered.:

                        @jenya7 said in Qt Bluetooth - no devices discovered.:

                        Is it OK?

                        Why did you comment out the connect?

                        for debug. I still get the error.
                        On Windows I get a different error - error: multiple definition of `Device::scanFinished()' in moc_device.cpp.
                        first defined here device.cpp.
                        I wonder how can it be - moc file is a generated file after build.

                        jsulmJ 2 Replies Last reply
                        0
                        • J jenya7

                          @jsulm said in Qt Bluetooth - no devices discovered.:

                          @jenya7 said in Qt Bluetooth - no devices discovered.:

                          Is it OK?

                          Why did you comment out the connect?

                          for debug. I still get the error.
                          On Windows I get a different error - error: multiple definition of `Device::scanFinished()' in moc_device.cpp.
                          first defined here device.cpp.
                          I wonder how can it be - moc file is a generated file after build.

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

                          @jenya7 said in Qt Bluetooth - no devices discovered.:

                          I wonder how can it be - moc file is a generated file after build

                          It is probably a remnant from previos version when scanFinished was declared as signal.
                          Do a complete rebuild: delete build folder, run qmake and build.

                          J 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @jenya7 said in Qt Bluetooth - no devices discovered.:

                            I wonder how can it be - moc file is a generated file after build

                            It is probably a remnant from previos version when scanFinished was declared as signal.
                            Do a complete rebuild: delete build folder, run qmake and build.

                            J Offline
                            J Offline
                            jenya7
                            wrote on last edited by
                            #26

                            @jsulm said in Qt Bluetooth - no devices discovered.:

                            @jenya7 said in Qt Bluetooth - no devices discovered.:

                            I wonder how can it be - moc file is a generated file after build

                            It is probably a remnant from previos version when scanFinished was declared as signal.
                            Do a complete rebuild: delete build folder, run qmake and build.

                            Deleted all from the build folder. Clean. Rebuild. I see new files created. The same problem.
                            Renamed scanFinished for scanCompleted. Still the same problem.

                            jsulmJ 1 Reply Last reply
                            0
                            • J jenya7

                              @jsulm said in Qt Bluetooth - no devices discovered.:

                              @jenya7 said in Qt Bluetooth - no devices discovered.:

                              I wonder how can it be - moc file is a generated file after build

                              It is probably a remnant from previos version when scanFinished was declared as signal.
                              Do a complete rebuild: delete build folder, run qmake and build.

                              Deleted all from the build folder. Clean. Rebuild. I see new files created. The same problem.
                              Renamed scanFinished for scanCompleted. Still the same problem.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #27
                              This post is deleted!
                              1 Reply Last reply
                              0
                              • J jenya7

                                @jsulm said in Qt Bluetooth - no devices discovered.:

                                @jenya7 said in Qt Bluetooth - no devices discovered.:

                                Is it OK?

                                Why did you comment out the connect?

                                for debug. I still get the error.
                                On Windows I get a different error - error: multiple definition of `Device::scanFinished()' in moc_device.cpp.
                                first defined here device.cpp.
                                I wonder how can it be - moc file is a generated file after build.

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

                                @jenya7 said in Qt Bluetooth - no devices discovered.:

                                On Windows I get a different error - error: multiple definition of `Device::scanFinished()' in moc_device.cpp.

                                Just realised: this is in Device class.
                                Please show Device code (header and cpp).
                                You probably defined scanFinished() in cpp file, you should not do that for signals!

                                J 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @jenya7 said in Qt Bluetooth - no devices discovered.:

                                  On Windows I get a different error - error: multiple definition of `Device::scanFinished()' in moc_device.cpp.

                                  Just realised: this is in Device class.
                                  Please show Device code (header and cpp).
                                  You probably defined scanFinished() in cpp file, you should not do that for signals!

                                  J Offline
                                  J Offline
                                  jenya7
                                  wrote on last edited by
                                  #29

                                  @jsulm
                                  I see. Now on Windows it compiles OK. On Linux the same 'vtable' problem.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • J jenya7

                                    @jsulm
                                    I see. Now on Windows it compiles OK. On Linux the same 'vtable' problem.

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

                                    @jenya7 said in Qt Bluetooth - no devices discovered.:

                                    On Linux the same 'vtable' problem

                                    Exact same code?
                                    Do a full rebuild.

                                    J 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @jenya7 said in Qt Bluetooth - no devices discovered.:

                                      On Linux the same 'vtable' problem

                                      Exact same code?
                                      Do a full rebuild.

                                      J Offline
                                      J Offline
                                      jenya7
                                      wrote on last edited by jenya7
                                      #31

                                      @jsulm
                                      yes.
                                      I've noticed in device.h at the beginning

                                      class Device: public QObject
                                      {
                                          Q_OBJECT
                                          Q_PROPERTY(QVariant devicesList READ getDevices NOTIFY devicesUpdated)
                                          Q_PROPERTY(QVariant servicesList READ getServices NOTIFY servicesUpdated)
                                          Q_PROPERTY(QVariant characteristicList READ getCharacteristics NOTIFY characteristicsUpdated)
                                          Q_PROPERTY(QString update READ getUpdate WRITE setUpdate NOTIFY updateChanged)
                                          Q_PROPERTY(bool useRandomAddress READ isRandomAddress WRITE setRandomAddress NOTIFY randomAddressChanged)
                                          Q_PROPERTY(bool state READ state NOTIFY stateChanged)
                                          Q_PROPERTY(bool controllerError READ hasControllerError)
                                      
                                      //and so on
                                      

                                      those Q_PROPERTY - do I need it? it's a QML related stuff.
                                      I took the class from a QML project.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • J jenya7

                                        @jsulm
                                        yes.
                                        I've noticed in device.h at the beginning

                                        class Device: public QObject
                                        {
                                            Q_OBJECT
                                            Q_PROPERTY(QVariant devicesList READ getDevices NOTIFY devicesUpdated)
                                            Q_PROPERTY(QVariant servicesList READ getServices NOTIFY servicesUpdated)
                                            Q_PROPERTY(QVariant characteristicList READ getCharacteristics NOTIFY characteristicsUpdated)
                                            Q_PROPERTY(QString update READ getUpdate WRITE setUpdate NOTIFY updateChanged)
                                            Q_PROPERTY(bool useRandomAddress READ isRandomAddress WRITE setRandomAddress NOTIFY randomAddressChanged)
                                            Q_PROPERTY(bool state READ state NOTIFY stateChanged)
                                            Q_PROPERTY(bool controllerError READ hasControllerError)
                                        
                                        //and so on
                                        

                                        those Q_PROPERTY - do I need it? it's a QML related stuff.
                                        I took the class from a QML project.

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

                                        @jenya7 said in Qt Bluetooth - no devices discovered.:

                                        those Q_PROPERTY - do I need it?

                                        If you don't use them then you don't need them.

                                        J 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @jenya7 said in Qt Bluetooth - no devices discovered.:

                                          those Q_PROPERTY - do I need it?

                                          If you don't use them then you don't need them.

                                          J Offline
                                          J Offline
                                          jenya7
                                          wrote on last edited by jenya7
                                          #33

                                          @jsulm
                                          oops. I did "Run qmake" then "Build" - and it's compiles, no errors.

                                          now if I uncomment
                                          connect(&bt_device, &Device::scanFinished, this, &BT_DEVICE::ScanFinished);
                                          I get a run time exception

                                          The inferior stopped because it received a signal from the operating system.

                                          in devise.h

                                          signals:
                                              void scanFinished();
                                          

                                          in my h

                                          public slots:
                                              void ScanFinished();
                                          

                                          seems OK.

                                          So this is wrong

                                          Device::Device()
                                          {
                                              connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::scanFinished);
                                          }
                                          

                                          I connect a signal to a signal.

                                          jsulmJ 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
                                          • Unsolved