簡體   English   中英

如何設置Qt以使用blueZ藍牙堆棧

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

我正在Windows 7計算機上進行開發,但最終產品將是linux,並且我還在使用Ubuntu虛擬機。

我需要搜索並連接到藍牙設備並運行示例,但是從研究來看,Qt藍牙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藍牙:

須藤apt-get install libbluetooth-dev

但是如何告訴Qt / Qt-Creator使用blueZ藍牙堆棧? Qt如何針對blueZ庫構建?

更新

我在3年前發布了這個問題,我相信版本是Qt 5.4,但是如果有人想發布解決方案,請將其發布到最新版本的Qt上,這樣可以使其他人受益。 據我所記得,我相信我發現Qt僅在Linux上支持藍牙,而在Windows上不支持。 它在Windows上的實現只是一個存根。

發布的代碼是實際使用的代碼嗎? (下一次提供MCVE )。 您正在運行哪個版本的QT?

如果是,則問題是在startDeviceDiscovery的末尾discoveryAgent變為null 對於編譯器而言,這是合法的,但實際上是邏輯錯誤。

可能的解決方案可能是:

  1. 實現一個包裝所有設置和內容的類以執行發現
  2. 使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.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編譯了代碼

是QT入門的分步指南。

此外,由於引這里 ,在Linux上,Qt使用配合bluez。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM