Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannnot find blutetooth device



  • Hello, I am trying to find a hc05-bluetooth 2.0 module with the following code, which I have from the QtDocu (https://doc.qt.io/qt-5/qtbluetooth-overview.html#scanning-for-bluetooth-devices). My problem is that my program does not find/scan my bluetooth device

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        agent  = new QBluetoothDeviceDiscoveryAgent(this);
        ui->setupUi(this);
        connect(agent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
        agent->start(QBluetoothDeviceDiscoveryAgent::ClassicMethod);
    
        socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_btn_find_clicked()
    {
        ui->listwdg_devices->clear();
        agent->stop();
        agent->start();
    }
    
    void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
        qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
        ui->listwdg_devices->addItem(device.address().toString());
    }
    
    
    void MainWindow::on_listwdg_devices_itemClicked(QListWidgetItem *item)
    {
        string = item->text();
        static const QString serviceUuid(QStringLiteral("00001101-0000-1000-8000-00805F9B34FB"));
        socket->connectToService(QBluetoothAddress(string), QBluetoothUuid(serviceUuid), QIODevice::ReadWrite);
    }
    
    

    MainWindow.h

    private slots:
        void on_btn_find_clicked();
        void deviceDiscovered(const QBluetoothDeviceInfo &device);
        void on_listwdg_devices_itemClicked(QListWidgetItem *item);
    
    private:
        Ui::MainWindow *ui;
        QBluetoothDeviceDiscoveryAgent *agent;/
        QBluetoothSocket *socket;
        QString string;
    

  • Lifetime Qt Champion

    @Chaki Which OS and which Qt version?
    Is Bluetooth enabled on the host?
    Are any other devices found?



  • @jsulm I have Windows 10 and qt version 5.15.0
    I have enabled bluetooth via the Windows settings and I am not able to find any other devices



  • Edit: I solved the problem with changing:
    agent->start(QBluetoothDeviceDiscoveryAgent::ClassicMethod);
    to
    agent->start();


Log in to reply