I am developing on Windows 7 machine but final product will be linux and I am also using Ubuntu virtual machine.
I need to search and connect to a bluetooth device and ran this example but from research it appears Qt Bluetooth APIs doesn't really support windows - which is ok, i need it for linux anyways. The bluetooth device discovery code for reference is:
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() << ')';
}
Now I am using blueZ version 4.x which Qt does support but my app doesn't discover anything in linux as well. I have installed blueZ bluetooth in my virtual machine with:
sudo apt-get install libbluetooth-dev
But how do I tell Qt/Qt-Creator to use the blueZ bluetooth stack? How does Qt builds against the blueZ library?
Update
I posted this question nearly 3 years ago and I believe the version was Qt 5.4 but if anyone wants to post a solution, please post it to the latest version of Qt so it can benefit others. As far as I recall, I believe I had found out that Qt supported bluetooth only on linux but not windows. Its implementation on Windows was just a stub.
Is the posted code the actual used code? (next time provide a MCVE ). Which version of QT are you running?
If yes, the problem is that discoveryAgent
becomes null
at the end of startDeviceDiscovery
. For the compiler it's legit, but it's actually a logic error.
Possible solutions could be:
discoveryAgent
a class member A faster way to try it out is a Qt console application
btdiscover.pro
QT -= gui
QT += bluetooth # Add it in your .pro file
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp
main.cpp
#include <QCoreApplication>
#include <QBluetoothServiceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QDebug>
#include <QObject>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
// Connect the signal to a lambda function
// The 3rd param is a dummy one, in real life application it will be an instance that point to the slot (4th param) owner
QObject::connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, new QObject(),
[](const QBluetoothDeviceInfo &device){
qInfo() << QString("Device found!! Its name is %1 and its MAC is %2").arg(device.name(), device.address().toString());
});
// Stop after 5000 mS
discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
// Start the discovery process
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
return a.exec();
}
In my case the program outputs the following lines:
"Device found!! Its name is IO_EXP and its MAC is 00:XX:XX:XX:XX:A1"
"Device found!! Its name is IO_EXP and its MAC is 00:XX:XX:XX:XX:57"
I compiled the code with Qt 5.11.1
Here there is a step-by-step guide by QT to getting started.
Moreover, as cited here , on Linux, QT uses Bluez.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.