Search code examples
qtbluezqtbluetooth

Qt Bluetooth Peripheral Segmentation Fault


I'm trying to advertise a Qt Bluetooth class in peripheral mode using the code given in this example directly from the Qt Documentation Page. I'm using Linux(Kubuntu 18.0.1) with C++ code. My Qt version is 5.7 with bluez version 5.48-0ubuntu3.1

My code looks like this the following:

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);

}

I create this class in the a QObject class called created in main.cpp and when i call the startAdertising function, i get an Segmentationfault the second i call the last line in my startAdvertising function:

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

Backtracing the error in gdb gives me something like this:

#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

}


Solution

  • 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);