首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置Qt以使用blueZ蓝牙堆栈

如何设置Qt以使用blueZ蓝牙堆栈
EN

Stack Overflow用户
提问于 2015-10-14 14:32:14
回答 1查看 2.9K关注 0票数 2

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

我需要搜索并连接到一个蓝牙设备并运行示例,但是从研究中看,QT蓝牙API并不真正支持windows --这是好的,我无论如何都需要它。供参考的蓝牙设备发现代码是:

代码语言:javascript
复制
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上的实现只是一个存根。

EN

回答 1

Stack Overflow用户

发布于 2018-07-02 12:14:11

张贴的代码是实际使用的代码吗?(下次提供一个MCVE)。你在运行哪种版本的QT?

如果是的话,问题是discoveryAgentstartDeviceDiscovery的末尾变成null。对于编译器来说,这是合法的,但实际上是一个逻辑错误。

可能的解决办法可以是:

  1. 实现一个类,它包装所有的设置和东西,以执行发现。
  2. 使discoveryAgent成为类成员

一个更快的尝试方法是一个Qt控制台应用程序

btdiscover.pro

代码语言:javascript
复制
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

代码语言:javascript
复制
#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();
}

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

代码语言:javascript
复制
"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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33128224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档