Can't locate devices with QtBluetooth low energy
-
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?
-
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?
@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 advisesFor 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?
-
@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 advisesFor 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?
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 -
S Sucharek has marked this topic as solved on