Qt蓝牙外设分段故障
Qt Bluetooth Peripheral Segmentation Fault
我正在尝试使用本示例中直接来自 Qt Documentation Page 的代码在外设模式下宣传 Qt 蓝牙 class。
我正在使用 Linux(Kubuntu 18.0.1) 和 C++ 代码。我的 Qt 版本是 5.7
和 bluez 版本 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 class 中创建了这个 class 并且当我调用 startAdertising 函数时,我得到一个 Segmentationfault 第二个我调用最后一行我的 startAdvertising 函数:
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
}
尽量不要使用QScopedPointer.
这段代码对我来说很有魅力:
class header:
private:
QLowEnergyController* m_controller;
来源:
构造函数
m_controller = QLowEnergyController::createPeripheral(this);
class::startAdvertisting() 方法
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);
我正在尝试使用本示例中直接来自 Qt Documentation Page 的代码在外设模式下宣传 Qt 蓝牙 class。
我正在使用 Linux(Kubuntu 18.0.1) 和 C++ 代码。我的 Qt 版本是 5.7
和 bluez 版本 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 class 中创建了这个 class 并且当我调用 startAdertising 函数时,我得到一个 Segmentationfault 第二个我调用最后一行我的 startAdvertising 函数:
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
}
尽量不要使用QScopedPointer.
这段代码对我来说很有魅力:
class header:
private:
QLowEnergyController* m_controller;
来源:
构造函数
m_controller = QLowEnergyController::createPeripheral(this);
class::startAdvertisting() 方法
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);