我正在Windows 7机器上开发,但最终产品将是linux,我也使用Ubuntu虚拟机。
我需要搜索并连接到一个蓝牙设备并运行这示例,但是从研究中看,QT蓝牙API并不真正支持windows --这是好的,我无论如何都需要它。供参考的蓝牙设备发现代码是:
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() << ')';
}现在我使用的是Qt支持的blueZ版本4.x,但是我的应用程序在linux中也没有发现任何东西。我已经在虚拟机中安装了blueZ蓝牙,其中包括:
sudo apt安装lib蓝牙-dev
但是如何告诉Qt/Qt-Creator使用blueZ蓝牙堆栈呢?Qt是如何针对blueZ库构建的?
更新
我在近3年前发布了这个问题,我相信版本是QT5.4,但是如果有人想发布解决方案,请将其发布到Qt的最新版本,这样就可以使其他人受益。据我回忆,我相信我发现Qt只支持linux而不支持windows。它在Windows上的实现只是一个存根。
发布于 2018-07-02 12:14:11
张贴的代码是实际使用的代码吗?(下次提供一个MCVE)。你在运行哪种版本的QT?
如果是的话,问题是discoveryAgent在startDeviceDiscovery的末尾变成null。对于编译器来说,这是合法的,但实际上是一个逻辑错误。
可能的解决办法可以是:
discoveryAgent成为类成员一个更快的尝试方法是一个Qt控制台应用程序。
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.cppmain.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();
}在我的例子中,程序输出以下行:
"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"我用QT5.11.1编译了代码
这里有一个逐步的指南由QT开始.
此外,正如这里所引用的,在Linux上,QT使用的是Bluez。
https://stackoverflow.com/questions/33128224
复制相似问题