我正在开发适用于 Android 的 qt 蓝牙广告。我有一个问题要开始做广告
F libc:致命信号 11 (SIGSEGV),代码 1 (SEGV_MAPERR),tid 3099 中的故障地址 0x8 (qtMainLoopThrea),pid 3072 (e.appAndroidBLE)
这是我的代码
#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();
}
我的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}
)
我认为我的代码缺少检查权限。但我不知道该怎么做。 大家帮我提出解决方案
您不仅必须创建指针
QSharedPointer<QLowEnergyController>
,还必须使用QLowEnergyController
的有效实例初始化指针。例如:
leController = QSharedPointer<QLowEnergyController>(QLowEnergyController::createPeripheral());