diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-08-17 08:44:51 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-08-17 11:38:14 +0000 |
commit | b3488be8b63dae8b949e05a241f60965f70200ec (patch) | |
tree | cb706fd1dbba42acc377a30875ad414922f1b32d | |
parent | 819f42dca8610c8f79477b9ce598fcf674efca45 (diff) |
Bluetooth heartrate-server: Handle errors
Make it a console application and terminate on errors, printing a
message.
This prevents it from silently hanging, locking up the libraries, on
Windows.
Change-Id: Ie7d022a2b193cf2c320cb918d35ce2ad52b2ac48
Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi>
(cherry picked from commit 58acbdf11e5e493c2d2d1c70c0b90d4dda34aa07)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | examples/bluetooth/heartrate-server/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/bluetooth/heartrate-server/heartrate-server.pro | 2 | ||||
-rw-r--r-- | examples/bluetooth/heartrate-server/main.cpp | 18 |
3 files changed, 18 insertions, 3 deletions
diff --git a/examples/bluetooth/heartrate-server/CMakeLists.txt b/examples/bluetooth/heartrate-server/CMakeLists.txt index 4f0c68de..e2038421 100644 --- a/examples/bluetooth/heartrate-server/CMakeLists.txt +++ b/examples/bluetooth/heartrate-server/CMakeLists.txt @@ -20,7 +20,6 @@ qt_add_executable(heartrate-server ) set_target_properties(heartrate-server PROPERTIES - WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) diff --git a/examples/bluetooth/heartrate-server/heartrate-server.pro b/examples/bluetooth/heartrate-server/heartrate-server.pro index 8ec3f703..56415a07 100644 --- a/examples/bluetooth/heartrate-server/heartrate-server.pro +++ b/examples/bluetooth/heartrate-server/heartrate-server.pro @@ -3,7 +3,7 @@ TARGET = heartrate-server QT = core bluetooth android: QT += gui -CONFIG += c++11 +CONFIG += c++11 console SOURCES += main.cpp diff --git a/examples/bluetooth/heartrate-server/main.cpp b/examples/bluetooth/heartrate-server/main.cpp index af0cc7eb..f62087a0 100644 --- a/examples/bluetooth/heartrate-server/main.cpp +++ b/examples/bluetooth/heartrate-server/main.cpp @@ -53,10 +53,25 @@ int main(int argc, char *argv[]) //! [Service Data] //! [Start Advertising] + bool errorOccurred = false; const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral()); + auto errorHandler = [&leController,&errorOccurred](QLowEnergyController::Error errorCode) + { + qWarning().noquote().nospace() << errorCode << " occurred: " + << leController->errorString(); + if (errorCode != QLowEnergyController::RemoteHostClosedError) { + qWarning("Heartrate-server quitting due to the error."); + errorOccurred = true; + QCoreApplication::quit(); + } + }; + QObject::connect(leController.data(), &QLowEnergyController::errorOccurred, errorHandler); + QScopedPointer<QLowEnergyService> service(leController->addService(serviceData)); leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData, advertisingData); + if (errorOccurred) + return -1; //! [Start Advertising] //! [Provide Heartbeat] @@ -93,5 +108,6 @@ int main(int argc, char *argv[]) }; QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect); - return app.exec(); + const int retval = QCoreApplication::exec(); + return errorOccurred ? -1 : retval; } |