summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@qt.io>2023-03-31 13:41:40 +0300
committerJuha Vuolle <juha.vuolle@qt.io>2023-04-04 08:35:12 +0300
commit6ffb1c3bfa1243f71ad2f0a95c4d896ffc434712 (patch)
tree2d1f4b813eec9efe4e6477396eff7eca32299d0e
parent63f77a6833fffad5347dff7b5dd3760522600dfd (diff)
downloadqtconnectivity-6ffb1c3bfa1243f71ad2f0a95c4d896ffc434712.tar.gz
Add permission request to bluetoothtestdevice manual test application
In addition: - Add temporary workaround for linking against the permission plugin on macOS - Use QGuiApplication also on macOS. This is required with permissions until the underlying event dispatcher is changed to Core Foundation's dispatcher also for QCoreApplication Task-number: QTBUG-112215 Task-number: QTBUG-112212 Change-Id: I3d30766aa52846994e23746ed3f65518c02384ea Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
-rw-r--r--tests/bluetoothtestdevice/CMakeLists.txt12
-rw-r--r--tests/bluetoothtestdevice/bluetoothtestdevice.cpp26
2 files changed, 34 insertions, 4 deletions
diff --git a/tests/bluetoothtestdevice/CMakeLists.txt b/tests/bluetoothtestdevice/CMakeLists.txt
index e07e4227..e017eedf 100644
--- a/tests/bluetoothtestdevice/CMakeLists.txt
+++ b/tests/bluetoothtestdevice/CMakeLists.txt
@@ -14,7 +14,7 @@ if(NOT TARGET Qt::Bluetooth)
set(CMAKE_AUTOMOC ON)
find_package(Qt6 REQUIRED COMPONENTS Bluetooth Core)
- if(ANDROID OR IOS)
+ if(ANDROID OR APPLE)
find_package(Qt6 REQUIRED COMPONENTS Gui)
endif()
@@ -29,6 +29,14 @@ else()
SOURCES
bluetoothtestdevice.cpp
)
+
+ if(MACOS)
+ # Explicitly link against the static permission plugin because tests
+ # currently don't have finalizers run for them except for iOS.
+ # TODO: Remove this when qtbase automatically runs finalizers for tests: QTBUG-112212
+ target_link_libraries(bluetoothtestdevice PRIVATE Qt6::QDarwinBluetoothPermissionPlugin)
+ endif()
+
endif()
set_target_properties(bluetoothtestdevice PROPERTIES
@@ -57,7 +65,7 @@ target_link_libraries(
Qt::Bluetooth
)
-if(ANDROID OR IOS)
+if(ANDROID OR APPLE)
target_link_libraries(
bluetoothtestdevice
PUBLIC
diff --git a/tests/bluetoothtestdevice/bluetoothtestdevice.cpp b/tests/bluetoothtestdevice/bluetoothtestdevice.cpp
index 98da879d..ed47673f 100644
--- a/tests/bluetoothtestdevice/bluetoothtestdevice.cpp
+++ b/tests/bluetoothtestdevice/bluetoothtestdevice.cpp
@@ -13,12 +13,16 @@
#include <QLowEnergyDescriptorData>
#include <QTimer>
-#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
+#if defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN)
#include <QGuiApplication>
#else
#include <QCoreApplication>
#endif // Q_OS_ANDROID || Q_OS_IOS
+#if QT_CONFIG(permissions)
+#include <QtCore/qpermissions.h>
+#endif
+
#include <thread>
static const QLatin1String largeAttributeServiceUuid("1f85e37c-ac16-11eb-ae5c-93d3a763feed");
@@ -71,12 +75,30 @@ int main(int argc, char *argv[])
{
qDebug() << "build:" << __DATE__ << __TIME__;
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
-#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
+#if defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN)
QGuiApplication app(argc, argv);
#else
QCoreApplication app(argc, argv);
#endif // Q_OS_ANDROID || Q_OS_IOS
+#if QT_CONFIG(permissions)
+ // Check Bluetooth permission and request it if the app doesn't have it
+ auto permissionStatus = app.checkPermission(QBluetoothPermission{});
+ if (permissionStatus == Qt::PermissionStatus::Undetermined) {
+ app.requestPermission(QBluetoothPermission{},
+ [&permissionStatus](const QPermission &permission) {
+ qApp->exit(); // Exit the permission request processing started below
+ permissionStatus = permission.status();
+ });
+ // Process permission request
+ app.exec();
+ }
+ if (permissionStatus == Qt::PermissionStatus::Denied) {
+ qWarning("Bluetooth permission denied, exiting");
+ return -1;
+ }
+#endif
+
// prepare list of services
QList<QLowEnergyServiceData> serviceDefinitions;