Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Issues searching bluetooth devices

    Installation and Deployment
    2
    6
    877
    Loading More Posts
    • 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
      juanfraska last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 2
        • J
          juanfraska last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 2
            • J
              juanfraska @SGaist last edited by

              @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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 1
                • First post
                  Last post