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. Scanning BLE beacons
Forum Updated to NodeBB v4.3 + New Features

Scanning BLE beacons

Scheduled Pinned Locked Moved Solved General and Desktop
42 Posts 4 Posters 14.5k Views 1 Watching
  • 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 Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #1

    I start a scan

    BT_DEVICE::BT_DEVICE()
    {
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
        discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
    
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BT_DEVICE::AddDevice);
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BT_DEVICE::ScanFinished);
    }
    
    void BT_DEVICE::BLE_Scan()
    {
        qDeleteAll(devices);
        devices.clear();
    
        discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    
        if (discoveryAgent->isActive())
        {
            m_deviceScanState = true;
        }
    }
    

    Then on deviceDiscovered event I add my remote device to a list

    void BT_DEVICE::AddDevice(const QBluetoothDeviceInfo &info)
    {
        if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) 
        {
            auto d = new DeviceInfo(info);
    
            if (d->getName() == "MyDeviceName")
                devices.append(d);
        }
    }
    

    Finally on finished event

    void BT_DEVICE::ScanFinished()
    {
        QVariant dev_list = QVariant::fromValue(devices);
    }
    

    But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.

    sierdzioS 1 Reply Last reply
    0
    • J jenya7

      I start a scan

      BT_DEVICE::BT_DEVICE()
      {
          discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
          discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
      
          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BT_DEVICE::AddDevice);
          connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BT_DEVICE::ScanFinished);
      }
      
      void BT_DEVICE::BLE_Scan()
      {
          qDeleteAll(devices);
          devices.clear();
      
          discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
      
          if (discoveryAgent->isActive())
          {
              m_deviceScanState = true;
          }
      }
      

      Then on deviceDiscovered event I add my remote device to a list

      void BT_DEVICE::AddDevice(const QBluetoothDeviceInfo &info)
      {
          if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) 
          {
              auto d = new DeviceInfo(info);
      
              if (d->getName() == "MyDeviceName")
                  devices.append(d);
          }
      }
      

      Finally on finished event

      void BT_DEVICE::ScanFinished()
      {
          QVariant dev_list = QVariant::fromValue(devices);
      }
      

      But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @jenya7 said in Scanning BLE beacons:

      But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.

      The scan is done. If you want to scan constantly, run the scan again, with a timer.

      (Z(:^

      J 1 Reply Last reply
      2
      • sierdzioS sierdzio

        @jenya7 said in Scanning BLE beacons:

        But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.

        The scan is done. If you want to scan constantly, run the scan again, with a timer.

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

        @sierdzio said in Scanning BLE beacons:

        @jenya7 said in Scanning BLE beacons:

        But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.

        The scan is done. If you want to scan constantly, run the scan again, with a timer.

        Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

        J.HilkJ 1 Reply Last reply
        0
        • J jenya7

          @sierdzio said in Scanning BLE beacons:

          @jenya7 said in Scanning BLE beacons:

          But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.

          The scan is done. If you want to scan constantly, run the scan again, with a timer.

          Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @jenya7 said in Scanning BLE beacons:

          Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

          no guarantee, the beacons (and other le devices) have to advertise during your scan phase.

          The trick is, storing a list of found devices and checking if a new one was added or not


          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.

          J 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @jenya7 said in Scanning BLE beacons:

            Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

            no guarantee, the beacons (and other le devices) have to advertise during your scan phase.

            The trick is, storing a list of found devices and checking if a new one was added or not

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

            @J-Hilk said in Scanning BLE beacons:

            @jenya7 said in Scanning BLE beacons:

            Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

            no guarantee, the beacons (and other le devices) have to advertise during your scan phase.

            The trick is, storing a list of found devices and checking if a new one was added or not

            I see. So I have some pain to get all data from all devices. Have to figure it out how to do it.
            Another question - how can I iterate all devices

            for (int i = 0; i < devices.length(); i++)
            {
                   devices[i]. //??? it has no name and data field
            }
            
            jsulmJ J.HilkJ 2 Replies Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by sierdzio
              #6

              I don't know what your variable devices is, so I can't say.

              But you can use discoveredDevices() which returns a QList, so:

              for (const auto &device : discoveryAgent->discoveredDevices()) {
                // do something with `device`
              }
              

              (Z(:^

              1 Reply Last reply
              1
              • J jenya7

                @J-Hilk said in Scanning BLE beacons:

                @jenya7 said in Scanning BLE beacons:

                Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

                no guarantee, the beacons (and other le devices) have to advertise during your scan phase.

                The trick is, storing a list of found devices and checking if a new one was added or not

                I see. So I have some pain to get all data from all devices. Have to figure it out how to do it.
                Another question - how can I iterate all devices

                for (int i = 0; i < devices.length(); i++)
                {
                       devices[i]. //??? it has no name and data field
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
                What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...

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

                J 1 Reply Last reply
                1
                • J jenya7

                  @J-Hilk said in Scanning BLE beacons:

                  @jenya7 said in Scanning BLE beacons:

                  Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?

                  no guarantee, the beacons (and other le devices) have to advertise during your scan phase.

                  The trick is, storing a list of found devices and checking if a new one was added or not

                  I see. So I have some pain to get all data from all devices. Have to figure it out how to do it.
                  Another question - how can I iterate all devices

                  for (int i = 0; i < devices.length(); i++)
                  {
                         devices[i]. //??? it has no name and data field
                  }
                  
                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @jenya7
                  usually, you react to the deviceDiscovered signal.
                  https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html#deviceDiscovered

                  that has a QBluetoothDeviceInfo argument, make a slot that accepts that as argument.
                  https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html

                  I personally have a class an interface class that takes a QBluetoothDeviceInfo as constructor argument, and I store that in a list to iterate over.


                  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.

                  1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
                    What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...

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

                    @jsulm said in Scanning BLE beacons:

                    @jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
                    What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...

                    I added it to a list

                     if (d->getName() == "MyDeviceName")
                                devices.append(d);
                    

                    How do I retrieve the device data?
                    No fields like
                    devices[i].Name
                    devices[i].Data

                    jsulmJ 2 Replies Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      It's your custom class of some sort! We can't know how to use a class we don't know :-)

                      (Z(:^

                      1 Reply Last reply
                      0
                      • J jenya7

                        @jsulm said in Scanning BLE beacons:

                        @jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
                        What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...

                        I added it to a list

                         if (d->getName() == "MyDeviceName")
                                    devices.append(d);
                        

                        How do I retrieve the device data?
                        No fields like
                        devices[i].Name
                        devices[i].Data

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

                        @jenya7 said in Scanning BLE beacons:

                        How do I retrieve the device data?

                        Why do you ask us?
                        Device is your class, you did not even show its code - how should we know?!

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

                        J 1 Reply Last reply
                        0
                        • J jenya7

                          @jsulm said in Scanning BLE beacons:

                          @jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
                          What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...

                          I added it to a list

                           if (d->getName() == "MyDeviceName")
                                      devices.append(d);
                          

                          How do I retrieve the device data?
                          No fields like
                          devices[i].Name
                          devices[i].Data

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

                          @jenya7 said in Scanning BLE beacons:

                          if (d->getName() == "MyDeviceName")
                          devices.append(d);

                          According to this code you do it this way:

                          devices[i]->getName();
                          

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

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @jenya7 said in Scanning BLE beacons:

                            How do I retrieve the device data?

                            Why do you ask us?
                            Device is your class, you did not even show its code - how should we know?!

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

                            @jsulm said in Scanning BLE beacons:

                            @jenya7 said in Scanning BLE beacons:

                            How do I retrieve the device data?

                            Why do you ask us?
                            Device is your class, you did not even show its code - how should we know?!

                            Why? It's Qt class

                            QBluetoothDeviceInfo &info
                            

                            Here

                            void BT_DEVICE::AddDevice(const QBluetoothDeviceInfo &info)
                            {
                                if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) 
                                {
                                    auto d = new DeviceInfo(info);
                            
                                    if (d->getName() == "MyDeviceName")
                                        devices.append(d);
                                }
                            }
                            

                            I see my device name (d->getName() ) so it probably should hold the beacon raw data (like d->data())

                            1 Reply Last reply
                            0
                            • sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #14

                              @jenya7 said in Scanning BLE beacons:

                              auto d = new DeviceInfo(info);

                              DeviceInfo is NOT QBluetoothDeviceInfo. It's some custom class you created. We don't know how to access it.

                              If you are asking how to get data from info (which is an instance of QBluetoothDeviceInfo), then just read the docs we have already linked: https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html

                              (Z(:^

                              J 1 Reply Last reply
                              0
                              • sierdzioS sierdzio

                                @jenya7 said in Scanning BLE beacons:

                                auto d = new DeviceInfo(info);

                                DeviceInfo is NOT QBluetoothDeviceInfo. It's some custom class you created. We don't know how to access it.

                                If you are asking how to get data from info (which is an instance of QBluetoothDeviceInfo), then just read the docs we have already linked: https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html

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

                                @sierdzio said in Scanning BLE beacons:

                                @jenya7 said in Scanning BLE beacons:

                                auto d = new DeviceInfo(info);

                                DeviceInfo is NOT QBluetoothDeviceInfo. It's some custom class you created. We don't know how to access it.

                                If you are asking how to get data from info (which is an instance of QBluetoothDeviceInfo), then just read the docs we have already linked: https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html

                                I see. Yes I use this class (from an example - C:\Qt\Examples\Qt-5.12.0\bluetooth\lowenergyscanner)

                                class DeviceInfo: public QObject
                                {
                                    Q_OBJECT
                                    Q_PROPERTY(QString deviceName READ getName NOTIFY deviceChanged)
                                    Q_PROPERTY(QString deviceAddress READ getAddress NOTIFY deviceChanged)
                                public:
                                    DeviceInfo() = default;
                                    DeviceInfo(const QBluetoothDeviceInfo &d);
                                    QString getAddress() const;
                                    QString getName() const;
                                    QBluetoothDeviceInfo getDevice();
                                    void setDevice(const QBluetoothDeviceInfo &dev);
                                
                                Q_SIGNALS:
                                    void deviceChanged();
                                
                                private:
                                    QBluetoothDeviceInfo device;
                                };
                                

                                So it has

                                QString DeviceInfo::getName() const
                                {
                                    return device.name();
                                }
                                

                                What if I want to add

                                 DeviceInfo::getData() const
                                {
                                    return device. //???? - no data field
                                }
                                
                                1 Reply Last reply
                                0
                                • sierdzioS Offline
                                  sierdzioS Offline
                                  sierdzio
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  But what data do you want to read? Manufacturer data? Device UUID? Address?

                                  Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services

                                  (Z(:^

                                  J J.HilkJ 2 Replies Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    jenya7
                                    wrote on last edited by
                                    #17
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • sierdzioS sierdzio

                                      But what data do you want to read? Manufacturer data? Device UUID? Address?

                                      Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services

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

                                      @sierdzio said in Scanning BLE beacons:

                                      But what data do you want to read? Manufacturer data? Device UUID? Address?

                                      Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services

                                      OK. I understand. I can use info directly like info.name, but how do I get data (no info.data).
                                      Yes I want to se all beacon raw data - payload. I need no services. It's a remote sensor sending me temperature, humidity, pressure, etc.

                                      1 Reply Last reply
                                      0
                                      • sierdzioS sierdzio

                                        But what data do you want to read? Manufacturer data? Device UUID? Address?

                                        Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #19

                                        @sierdzio said in Scanning BLE beacons:

                                        Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services

                                        not with beacon, beacons are not connectable. And the Qt Api gives no way to identify that.

                                        what the op is looking for should be in
                                        https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html#manufacturerData-1


                                        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.

                                        J 1 Reply Last reply
                                        0
                                        • sierdzioS Offline
                                          sierdzioS Offline
                                          sierdzio
                                          Moderators
                                          wrote on last edited by
                                          #20

                                          It's been a long time since I've done it, sorry I don't remember the details. Check the example docs above, it explains how to connect to various services and characteristics, then you can start receiving the data.

                                          (Z(:^

                                          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