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. Can't locate devices with QtBluetooth low energy
Qt 6.11 is out! See what's new in the release blog

Can't locate devices with QtBluetooth low energy

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 549 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
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm trying to scan for bluetooth le devices, but I have no luck. For my device I have a mi band 6. I can connect to it from windows settings and my phone (and read it with nRF connect).

    While I'm paired to the device, it can be found. I also tried this program, but still no luck.

    Here's my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    QBluetoothDeviceDiscoveryAgent *discoverAgent;
    
    QList<QBluetoothDeviceInfo> discoveredDevices;
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        discoverAgent = new QBluetoothDeviceDiscoveryAgent(this);
        discoverAgent->setLowEnergyDiscoveryTimeout(1000);
    
        connect(discoverAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &MainWindow::addDevice);
        connect(discoverAgent, static_cast<void (QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error),
                this, &MainWindow::scanError);
        connect(discoverAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &MainWindow::scanFinished);
        connect(discoverAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &MainWindow::scanCanceled);
    
        discoverAgent->start();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::addDevice(const QBluetoothDeviceInfo &device)
    {
        discoveredDevices.append(device);
        qDebug() << "Found BLE device" << device.name();
    }
    
    void MainWindow::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
    {
        qDebug() << "Failed to scan" << error;
    }
    
    void MainWindow::scanFinished()
    {
        qDebug() << "scan finished";
    }
    
    void MainWindow::scanCanceled()
    {
        qDebug() << "scan canceled";
    }
    

    Could it be a windows issue or is my code wrong?

    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      Hi, I'm trying to scan for bluetooth le devices, but I have no luck. For my device I have a mi band 6. I can connect to it from windows settings and my phone (and read it with nRF connect).

      While I'm paired to the device, it can be found. I also tried this program, but still no luck.

      Here's my code:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      QBluetoothDeviceDiscoveryAgent *discoverAgent;
      
      QList<QBluetoothDeviceInfo> discoveredDevices;
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          discoverAgent = new QBluetoothDeviceDiscoveryAgent(this);
          discoverAgent->setLowEnergyDiscoveryTimeout(1000);
      
          connect(discoverAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &MainWindow::addDevice);
          connect(discoverAgent, static_cast<void (QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error),
                  this, &MainWindow::scanError);
          connect(discoverAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &MainWindow::scanFinished);
          connect(discoverAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &MainWindow::scanCanceled);
      
          discoverAgent->start();
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::addDevice(const QBluetoothDeviceInfo &device)
      {
          discoveredDevices.append(device);
          qDebug() << "Found BLE device" << device.name();
      }
      
      void MainWindow::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
      {
          qDebug() << "Failed to scan" << error;
      }
      
      void MainWindow::scanFinished()
      {
          qDebug() << "scan finished";
      }
      
      void MainWindow::scanCanceled()
      {
          qDebug() << "scan canceled";
      }
      

      Could it be a windows issue or is my code wrong?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Sucharek
      Not at all my area, but why set a discovery timeout as low as 1 second? https://doc.qt.io/qt-6/qbluetoothdevicediscoveryagent.html#setLowEnergyDiscoveryTimeout advises

      For a reliable Bluetooth Low Energy discovery, use at least 40000 milliseconds.

      Doubtless it won't help, but worth a try...?

      But what compiler are you using under Windows? MinGW? https://stackoverflow.com/questions/61753002/invisible-ble-device-when-qt-scanning says you have to use MSVC?

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @Sucharek
        Not at all my area, but why set a discovery timeout as low as 1 second? https://doc.qt.io/qt-6/qbluetoothdevicediscoveryagent.html#setLowEnergyDiscoveryTimeout advises

        For a reliable Bluetooth Low Energy discovery, use at least 40000 milliseconds.

        Doubtless it won't help, but worth a try...?

        But what compiler are you using under Windows? MinGW? https://stackoverflow.com/questions/61753002/invisible-ble-device-when-qt-scanning says you have to use MSVC?

        S Offline
        S Offline
        Sucharek
        wrote on last edited by Sucharek
        #3

        Hi @JonB, so I've tried the 40 second timeout, but the scan seems to finish after 13 seconds, no matter the timeout. Even if I put -1, 200, 1000 or 40000, it's always 13 seconds.

        My compiler is MinGW, but after changing it to MSVC (had to install visual basic build tools to get a working compiler), it works now! All the bluetooth devices I need have been found and the timeout works too. Thank you!
        Edit: My Qt version is 5.15.2

        1 Reply Last reply
        0
        • S Sucharek has marked this topic as solved on

        • Login

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