Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Issues searching bluetooth devices

Issues searching bluetooth devices

Scheduled Pinned Locked Moved Solved Installation and Deployment
6 Posts 2 Posters 1.2k 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.
  • J Offline
    J Offline
    juanfraska
    wrote on last edited by
    #1

    Hi, I've been trying to use the bluetooth API that Qt has implemented in order to develop an application to search for nearby devices.
    Once I got back the values of nearby devices, I realized that if one of them went off I was going to keep showing and if I turned on or approached a new one it would not be detected. It will only show me the devices that have been previously discovered by the Ubuntu tool itself (I am working on Linux) for the search of bluetooth devices. Here I leave the code:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QBluetoothLocalDevice>
    #include <QDebug>
    #include <QBluetoothDeviceDiscoveryAgent>
    #include <QtBluetooth/QBluetoothLocalDevice>
    //! [include]
    #include <QtCore/QCoreApplication>
    #include <QtCore/QDebug>
    #include <QtCore/QFile>
    #include <QtCore/QObject>
    #include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
    #include <QtBluetooth/QBluetoothServiceDiscoveryAgent>
    #include <QtBluetooth/QBluetoothTransferManager>
    #include <QtBluetooth/QBluetoothTransferRequest>
    #include <QtBluetooth/QBluetoothTransferReply>

    #include <QtBluetooth/QLowEnergyController>
    #include <QtBluetooth/QLowEnergyService>
    #include <QtBluetooth/QLowEnergyCharacteristic>

    int main(int argc, char *argv[])
    {

    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();
        qDebug() << remotes;
    }
    MyClass hola;
    hola.startDeviceDiscovery();
    
    //while( 1);
    

    }

    void MyClass::startDeviceDiscovery()
    {

    // Create a discovery agent and connect to its signals
    QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
            this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
    
    // Start a discovery
    discoveryAgent->start();
    
    //...
    

    }

    // In your local slot, read information about the found devices
    void MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
    qDebug() << "Found new device:" << device.name() << '(' << device.address().toString()<<' ' << device.minorDeviceClass()<< ')';
    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You seem to be missing a QCoreApplication/QApplication object which is needed in order to initialise Qt's internal state. Therefore you also don't call the exec method of the application object thus you don't have any event loop running.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • J Offline
        J Offline
        juanfraska
        wrote on last edited by
        #3

        I add this in the main app.

        int main(int argc, char *argv[])
        {

        QBluetoothLocalDevice localDevice;
        QString localDeviceName;
        QApplication a(argc, argv);
        
        
        
        // 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();
            qDebug() << remotes;
        }
        MyClass hola;
        hola.startDeviceDiscovery();
        
        return a.exec();
        

        }

        The process still running but doesn't find nearby devices which not are discovered by linux bluetooth tool before.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          As stated in the documentation, the first thing you have to do is create the QApplication object. It initialises its subsystem.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          2
          • SGaistS SGaist

            As stated in the documentation, the first thing you have to do is create the QApplication object. It initialises its subsystem.

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

            @SGaist said in Issues searching bluetooth devices:

            e first thing you have to do is create the QApplication object. It initialises its subsystem.

            I added
            QApplication a(argc, argv);
            return a.exec();

            The same result, only discover when I find devices in Linux Bluetooth tool.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What version of Qt ?
              What distribution ?
              What device ?

              What is your current version of the code ?

              Did you check with Qt's examples ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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