简体   繁体   English

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

[英]How to setup Qt to use blueZ bluetooth stack

I am developing on Windows 7 machine but final product will be linux and I am also using Ubuntu virtual machine. 我正在Windows 7计算机上进行开发,但最终产品将是linux,并且我还在使用Ubuntu虚拟机。

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. 我需要搜索并连接到蓝牙设备并运行示例,但是从研究来看,Qt蓝牙API似乎并不真正支持Windows-没关系,无论如何我都需要Linux。 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. 现在,我正在使用Qt支持的blueZ版本4.x,但我的应用程序也没有在Linux中发现任何东西。 I have installed blueZ bluetooth in my virtual machine with: 我已经在虚拟机中通过以下方式安装了blueZ蓝牙:

sudo apt-get install libbluetooth-dev 须藤apt-get install libbluetooth-dev

But how do I tell Qt/Qt-Creator to use the blueZ bluetooth stack? 但是如何告诉Qt / Qt-Creator使用blueZ蓝牙堆栈? How does Qt builds against the blueZ library? Qt如何针对blueZ库构建?

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. 我在3年前发布了这个问题,我相信版本是Qt 5.4,但是如果有人想发布解决方案,请将其发布到最新版本的Qt上,这样可以使其他人受益。 As far as I recall, I believe I had found out that Qt supported bluetooth only on linux but not windows. 据我所记得,我相信我发现Qt仅在Linux上支持蓝牙,而在Windows上不支持。 Its implementation on Windows was just a stub. 它在Windows上的实现只是一个存根。

Is the posted code the actual used code? 发布的代码是实际使用的代码吗? (next time provide a MCVE ). (下一次提供MCVE )。 Which version of QT are you running? 您正在运行哪个版本的QT?

If yes, the problem is that discoveryAgent becomes null at the end of startDeviceDiscovery . 如果是,则问题是在startDeviceDiscovery的末尾discoveryAgent变为null For the compiler it's legit, but it's actually a logic error. 对于编译器而言,这是合法的,但实际上是逻辑错误。

Possible solutions could be: 可能的解决方案可能是:

  1. implement a class that wrap all the set up and stuff to perform a discovery 实现一个包装所有设置和内容的类以执行发现
  2. make discoveryAgent a class member 使discoveryAgent成为班级成员

A faster way to try it out is a Qt console application 一个更快的尝试方法是Qt控制台应用程序

btdiscover.pro 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 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 我用Qt 5.11.1编译了代码

Here there is a step-by-step guide by QT to getting started. 是QT入门的分步指南。

Moreover, as cited here , on Linux, QT uses Bluez. 此外,由于引这里 ,在Linux上,Qt使用配合bluez。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM