gpt4 book ai didi

c++ - 如何设置 Qt 以使用 blueZ 蓝牙堆栈

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:56 31 4
gpt4 key购买 nike

我在 Windows 7 机器上开发,但最终产品将是 linux,我也在使用 Ubuntu 虚拟机。

我需要搜索并连接到蓝牙设备并运行 this例如,但从研究来看,Qt Bluetooth API 似乎并不真正支持 Windows - 没关系,无论如何我都需要它用于 linux。蓝牙设备发现代码供引用:

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-get install libbluetooth-dev

但是我如何告诉 Qt/Qt-Creator 使用 blueZ 蓝牙协议(protocol)栈呢? Qt 如何针对 blueZ 库进行构建?

更新

我将近 3 年前发布了这个问题,我相信版本是 Qt 5.4,但如果有人想发布解决方案,请将其发布到最新版本的 Qt,以便其他人受益。据我所知,我相信我已经发现 Qt 仅在 linux 上支持蓝牙,但在 windows 上不支持。它在 Windows 上的实现只是一个 stub 。

最佳答案

发布的代码是实际使用的代码吗? (下次提供 MCVE )。您运行的是哪个版本的 QT?

如果是,问题是 discoveryAgentstartDeviceDiscovery 结束时变为 null。对于编译器来说它是合法的,但它实际上是一个逻辑错误。

可能的解决方案可能是:

  1. 实现一个包含所有设置和内容的类以执行发现
  2. discoveryAgent成为类(class)成员

一种更快的试用方法是 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.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();
}

在我的例子中,程序输出以下几行:

"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"

我用Qt 5.11.1编译代码

Here QT 提供了入门分步指南。

此外,如引用here , 在 Linux 上,QT 使用 Bluez。

关于c++ - 如何设置 Qt 以使用 blueZ 蓝牙堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33128224/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com