0

我正在尝试使用此示例中直接从Qt 文档页面中给出的代码在外围模式下宣传 Qt 蓝牙类。我正在使用带有 C++ 代码的 Linux(Kubuntu 18.0.1)。我的 Qt 版本是5.7bluez 版本5.48-0ubuntu3.1

我的代码如下所示:

bleHololenseServer.h:

#ifndef BLEHOLOLENSESERVER_H
#define BLEHOLOLENSESERVER_H

#include <QDebug>
#include <QObject>
#include <QLowEnergyAdvertisingData>
#include <QLowEnergyAdvertisingParameters>
#include <QLowEnergyServiceData>
#include <QLowEnergyCharacteristicData>
#include <QLowEnergyDescriptorData>
#include <QLowEnergyController>
#include <QLowEnergyService>



/*
 * Visualization Class for the AnkiCar and street
*/

class BLEHololenseServer : public QObject{
    Q_OBJECT
    public:
        BLEHololenseServer();

        void startAdvertisting();

    private:

    signals:

    public slots:
};


#endif // BLEHOLOLENSESERVER_H

bleHololenseSever.cpp

#include <QDebug>
#include <QLowEnergyAdvertisingData>
#include <QLowEnergyAdvertisingParameters>
#include <QLowEnergyServiceData>
#include <QLowEnergyCharacteristicData>
#include <QLowEnergyDescriptorData>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include "../headers/bleServer.h"
#include <QScopedPointer>

BLEHololenseServer::BLEHololenseServer(){

}

void BLEHololenseServer::startAdvertisting(){
    QLowEnergyAdvertisingData advertisingData;
    advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
    advertisingData.setIncludePowerLevel(true);
    advertisingData.setLocalName("AnkiServer");
    advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);

    QLowEnergyCharacteristicData charData;
    charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
    charData.setValue(QByteArray(2, 0));
    charData.setProperties(QLowEnergyCharacteristic::Notify);
    const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration, QByteArray(2, 0));
    charData.addDescriptor(clientConfig);

    QLowEnergyServiceData serviceData;
    serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
    serviceData.setUuid(QBluetoothUuid::HeartRate);
    serviceData.addCharacteristic(charData);

    const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
    const QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
    const QLowEnergyAdvertisingParameters advertisingParameters  = QLowEnergyAdvertisingParameters();
    leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);

}

我在 main.cpp 中创建的 QObject 类中创建了这个类,当我调用 startAdert​​ising 函数时,我在调用 startAdvertising 函数的最后一行时得到一个 Segmentationfault:

leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);

回溯 gdb 中的错误给了我这样的信息:

#0  0x00007ffff71fb082 in ?? () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#1  0x00007ffff71b56b7 in QLowEnergyController::~QLowEnergyController() () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#2  0x00007ffff71b56d9 in QLowEnergyController::~QLowEnergyController() () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#3  0x000055555557eb4e in QScopedPointerDeleter<QLowEnergyController>::cleanup (pointer=0x5555557b78b0) at ../../../../Qt/5.7/gcc_64/include/QtCore/qscopedpointer.h:60
#4  0x000055555557eaa3 in QScopedPointer<QLowEnergyController, QScopedPointerDeleter<QLowEnergyController> >::~QScopedPointer (this=0x7fffffffe108, __in_chrg=<optimized out>)
    at ../../../../Qt/5.7/gcc_64/include/QtCore/qscopedpointer.h:107
#5  0x000055555557e8a2 in BLEHololenseServer::startAdvertisting (this=0x5555557b7aa0) at src/bleHololense/bleHololenseServer.cpp:35
#6  0x000055555556abab in DriveMode::DriveMode (this=0x5555557b2940, parent=0x0) at src/drivemode.cpp:87
#7  0x000055555555ccdb in main (argc=1, argv=0x7fffffffe448) at src/main.cpp:34

}

4

1 回答 1

1

Try not to use QScopedPointer.

This code works like a charm for me:

class header:

private:
    QLowEnergyController* m_controller;

source:

constructor

m_controller = QLowEnergyController::createPeripheral(this);

class::startAdvertisting() method

QLowEnergyCharacteristicData l_characteristic_data;
l_characteristic_data.setUuid(QBluetoothUuid::TemperatureMeasurement);
l_characteristic_data.setValue(QByteArray(2, 0));
l_characteristic_data.setProperties(QLowEnergyCharacteristic::Notify);

const QLowEnergyDescriptorData l_config(QBluetoothUuid::ClientCharacteristicConfiguration,
                                        QByteArray(2, 0));
l_characteristic_data.addDescriptor(l_config);

QLowEnergyServiceData l_temp_data;
l_temp_data.setType(QLowEnergyServiceData::ServiceTypePrimary);
l_temp_data.setUuid(QBluetoothUuid::Temperature);
l_temp_data.addCharacteristic(l_characteristic_data);

QLowEnergyService* l_temp_service = m_controller->addService(l_temp_data, this);

QLowEnergyAdvertisingData l_advertising_data; 
l_advertising_data.setLocalName("FakeBle");
l_advertising_data.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::Temperature);

m_controller->startAdvertising(QLowEnergyAdvertisingParameters(), l_advertising_data, l_advertising_data);
于 2018-10-26T09:51:31.177 回答