androidqtandroid-bluetooth

How to start qt bluetooth advertising on android


I am deverloping the qt bluetooth advertision for android . I have an issue to start adverting

F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x8 in tid 3099 (qtMainLoopThrea), pid 3072 (e.appAndroidBLE)

Here is my code

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include <QLowEnergyController>
#include <QLowEnergyAdvertisingData>
#include <QByteArray>
#include <QLowEnergyAdvertisingParameters>
#include <QPermissions>
#include <QBluetoothLocalDevice>


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QBluetoothLocalDevice localDevice;
    if (localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff) {
        qDebug() << "Bluetooth is turned off!";
        localDevice.powerOn();
    }


    QSharedPointer<QLowEnergyController> leController;

    QLowEnergyAdvertisingParameters advertisingParameters;
    advertisingParameters.setMode(QLowEnergyAdvertisingParameters::AdvNonConnInd);

    QByteArray advData = QByteArray::fromRawData(
        "\x02\x01\x06\x03\x02\x50\xFD\x17\x16\x50\xFD\x41\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
        31
        );
    QByteArray scanRspData = QByteArray::fromRawData(
        "\x17\xFF\xD0\x07\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x03\x09\x54\x59",
        31
        );

    QLowEnergyAdvertisingData advertisingData;
    advertisingData.setRawData(advData);

    QLowEnergyAdvertisingData advertisingScanData;
    advertisingScanData.setRawData(scanRspData);

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

    qDebug() << "start adv ble";


    QQmlApplicationEngine engine;
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.loadFromModule("AndroidBLE", "Main");

    return app.exec();
}

My CMakeList.txt

cmake_minimum_required(VERSION 3.16)

project(AndroidBLE VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_executable(appAndroidBLE
    main.cpp
)

qt_add_qml_module(appAndroidBLE
    URI AndroidBLE
    VERSION 1.0
    QML_FILES
        Main.qml
        SOURCES datautil.h datautil.cpp
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appAndroidBLE PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appAndroidBLE
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)
find_package(Qt6 REQUIRED COMPONENTS Bluetooth)

target_link_libraries(appAndroidBLE
    PRIVATE Qt6::Quick
        Qt6::Bluetooth
        Qt6::Core
)

# target_link_libraries(appAndroidBLE PRIVATE Qt6::Bluetooth )


include(GNUInstallDirs)
install(TARGETS appAndroidBLE
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

I think my code is missing the check permission. But I dont know how to do that. Everyone help me to suggest the solution


Solution

  • You must not only create the pointer QSharedPointer<QLowEnergyController>, you must also initialise the pointer with a valid instance of QLowEnergyController. For example:

       leController = QSharedPointer<QLowEnergyController>(QLowEnergyController::createPeripheral());