diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2023-01-06 11:42:58 +0100 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2023-03-03 09:54:44 +0100 |
commit | aa6216ae79236bde72a388f609a4bc5b81d8ecba (patch) | |
tree | f0178c2ddad5cd708e47e2923a027a5ce508b156 | |
parent | f162d79280379ff056cd9c764699550177cabce6 (diff) | |
download | qtconnectivity-aa6216ae79236bde72a388f609a4bc5b81d8ecba.tar.gz |
Heartrate server: use permissions API to access Bluetooth
Add Bluetooth permission check/request to the heart-rate server's code.
Task-number: QTBUG-109964
Change-Id: I7fc6a4cf6792e1aa223cc5dc4173963f995e305d
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
4 files changed, 47 insertions, 9 deletions
diff --git a/examples/bluetooth/heartrate-server/CMakeLists.txt b/examples/bluetooth/heartrate-server/CMakeLists.txt index 6b559a4a..02dbf876 100644 --- a/examples/bluetooth/heartrate-server/CMakeLists.txt +++ b/examples/bluetooth/heartrate-server/CMakeLists.txt @@ -14,7 +14,7 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/bluetooth/heartrate-server") find_package(Qt6 REQUIRED COMPONENTS Bluetooth Core) -if(ANDROID OR IOS) +if(ANDROID OR APPLE) find_package(Qt6 REQUIRED COMPONENTS Gui) endif() @@ -31,7 +31,7 @@ target_link_libraries(heartrate-server PUBLIC Qt::Core ) -if(ANDROID OR IOS) +if(ANDROID OR APPLE) target_link_libraries(heartrate-server PUBLIC Qt::Gui ) diff --git a/examples/bluetooth/heartrate-server/doc/src/heartrate-server.qdoc b/examples/bluetooth/heartrate-server/doc/src/heartrate-server.qdoc index bf9bbd2c..39d4c976 100644 --- a/examples/bluetooth/heartrate-server/doc/src/heartrate-server.qdoc +++ b/examples/bluetooth/heartrate-server/doc/src/heartrate-server.qdoc @@ -33,6 +33,16 @@ \note On Linux, advertising requires privileged access, so you need to run the example as root, for instance via \c sudo. + \section1 Checking Bluetooth Permission + Before the application can create a service and start advertising, we have to check + if the application has a permission to use Bluetooth. + \snippet heartrate-server/main.cpp Check Bluetooth Permission + + \section1 Request Bluetooth Permission + If the Bluetooth authorization status is undetermined, we have to request a permission + to use Bluetooth. + \snippet heartrate-server/main.cpp Request Bluetooth Permission + \section1 Setting up Advertising Data and Parameters Two classes are used to configure the advertising process: \l QLowEnergyAdvertisingData to specify which information is to be broadcast, and \l QLowEnergyAdvertisingParameters for diff --git a/examples/bluetooth/heartrate-server/heartrate-server.pro b/examples/bluetooth/heartrate-server/heartrate-server.pro index 56415a07..34d8eaf7 100644 --- a/examples/bluetooth/heartrate-server/heartrate-server.pro +++ b/examples/bluetooth/heartrate-server/heartrate-server.pro @@ -2,7 +2,8 @@ TEMPLATE = app TARGET = heartrate-server QT = core bluetooth -android: QT += gui +android|darwin: QT += gui + CONFIG += c++11 console SOURCES += main.cpp diff --git a/examples/bluetooth/heartrate-server/main.cpp b/examples/bluetooth/heartrate-server/main.cpp index f62087a0..6cc40a4d 100644 --- a/examples/bluetooth/heartrate-server/main.cpp +++ b/examples/bluetooth/heartrate-server/main.cpp @@ -10,25 +10,52 @@ #include <QtBluetooth/qlowenergyservice.h> #include <QtBluetooth/qlowenergyservicedata.h> #include <QtCore/qbytearray.h> -#ifndef Q_OS_ANDROID -#include <QtCore/qcoreapplication.h> -#else +#if defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN) #include <QtGui/qguiapplication.h> +#else +#include <QtCore/qcoreapplication.h> #endif #include <QtCore/qlist.h> #include <QtCore/qloggingcategory.h> #include <QtCore/qscopedpointer.h> #include <QtCore/qtimer.h> +#if QT_CONFIG(permissions) +#include <QtCore/qpermissions.h> +#endif int main(int argc, char *argv[]) { // QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); -#ifndef Q_OS_ANDROID - QCoreApplication app(argc, argv); -#else +#if defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN) QGuiApplication app(argc, argv); +#else + QCoreApplication app(argc, argv); #endif +#if QT_CONFIG(permissions) + //! [Check Bluetooth Permission] + auto permissionStatus = app.checkPermission(QBluetoothPermission{}); + //! [Check Bluetooth Permission] + + //! [Request Bluetooth Permission] + if (permissionStatus == Qt::PermissionStatus::Undetermined) { + qInfo("Requesting Bluetooth permission ..."); + app.requestPermission(QBluetoothPermission{}, [&permissionStatus](const QPermission &permission){ + qApp->exit(); + permissionStatus = permission.status(); + }); + // Now, wait for permission request to resolve. + app.exec(); + } + //! [Request Bluetooth Permission] + + if (permissionStatus == Qt::PermissionStatus::Denied) { + // Either explicitly denied by a user, or Bluetooth is off. + qWarning("This application cannot use Bluetooth, the permission was denied"); + return -1; + } + +#endif //! [Advertising Data] QLowEnergyAdvertisingData advertisingData; advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral); |