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

How to scanning for bluetooth devices?



  • Hi All,
    I'm using qt5.5 on my computer and using Linux+QT wayland for my OS system.
    I wanna scanning for my blutooth devices and I do it with below link.
    Document

    This is what I write in my program.

    void MainWindow::on_play_clicked()
    {
    QBluetoothLocalDevice localDevice;
    QString localDeviceName;
    
    // Check if Bluetooth is available on this device
    if (localDevice.isValid()) {
    
           // Turn Bluetooth on
            localDevice.powerOn();
    
           // Read local device name
           localDeviceName = localDevice.name();
    
           // Make it visible to others
           localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
    
           // Get connected devices
           QList<QBluetoothAddress> remotes;
           remotes = localDevice.connectedDevices();
     }
    else
    {
          qDebug("local Device is not available");
    }
    
    }
    

    But It always show "local Device is not available".
    Why is this happening?
    How can I solve this question?
    Thanks in Advanced!


  • Lifetime Qt Champion

    Hi,

    I'd go with the static QBluetoothLocalDevice::allDevices function to check the list of devices and see if anything is available.

    Did you also check that you have the bluez libraries installed on your system ?



  • @SGaist
    Yes, I had try to connect bluetooth directly from my system last week.
    And I'm using bluez5 to do it.
    I'm sure it connect perfectly.

    But yesterday I found that something going wrong with my system.
    I can't "lsusb".
    I doubt that it is the problem which caused my device invalid in QT progrm.

    I will try again and the advise you gave then that you know the result.
    Thanks for the reply.



  • @SGaist
    Here I'm using QBluetoothLocalDevice::allDevices() to check the available device.
    This is how I designed.

    QList<QBluetoothHostInfo> infos = QBluetoothLocalDevice::allDevices();
    if(infos.isempty)
    {
          qDebug("It is empty");
    }
    

    I can't understand why it will show empty.
    I'm sure that I can connect my phone by running commands in terminal directly.

    Why is infos empty?


  • Lifetime Qt Champion

    In that case, you should try to run your application once you connected your phone.



  • @SGaist
    Is that you mean I have to connect my phone first then run my QT application?


  • Lifetime Qt Champion

    Yes, so you ensure that you have at least one active Bluetooth device on your computer.


Log in to reply