Issues searching bluetooth devices
-
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()<< ')';
} -
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.
-
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.
-
As stated in the documentation, the first thing you have to do is create the QApplication object. It initialises its subsystem.
-
As stated in the documentation, the first thing you have to do is create the QApplication object. It initialises its subsystem.
@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.
-
What version of Qt ?
What distribution ?
What device ?What is your current version of the code ?
Did you check with Qt's examples ?