diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2023-01-10 13:28:46 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-04-03 10:59:34 +0200 |
commit | 05b91367b3e1189e3deb2c61a2ba8a51399b806d (patch) | |
tree | 0ad60e878ad3413c18f2ee5af57028019dbc0cd0 | |
parent | 55abcc47ee36e901b6f14d08d9a2ea0483b40dab (diff) | |
download | qtconnectivity-05b91367b3e1189e3deb2c61a2ba8a51399b806d.tar.gz |
heartrate-game: use permission API to grant Bluetooth permission
Task-number: QTBUG-109964
Change-Id: Ic667c922f1d01d25a4eac8508481108d83f7a0af
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
6 files changed, 77 insertions, 9 deletions
diff --git a/examples/bluetooth/heartrate-game/App.qml b/examples/bluetooth/heartrate-game/App.qml index 98633c98..db6aa714 100644 --- a/examples/bluetooth/heartrate-game/App.qml +++ b/examples/bluetooth/heartrate-game/App.qml @@ -70,7 +70,8 @@ Item { BluetoothAlarmDialog { id: btAlarmDialog anchors.fill: parent - visible: !app.connectionHandler.alive + visible: !app.connectionHandler.alive || permissionError + permissionError: !app.connectionHandler.hasPermission } Keys.onReleased: (event) => { diff --git a/examples/bluetooth/heartrate-game/BluetoothAlarmDialog.qml b/examples/bluetooth/heartrate-game/BluetoothAlarmDialog.qml index 93f4a3cd..af0b0545 100644 --- a/examples/bluetooth/heartrate-game/BluetoothAlarmDialog.qml +++ b/examples/bluetooth/heartrate-game/BluetoothAlarmDialog.qml @@ -5,6 +5,9 @@ import QtQuick Item { id: root + + property bool permissionError: false + anchors.fill: parent Rectangle { @@ -51,7 +54,9 @@ Item { wrapMode: Text.WordWrap font.pixelSize: GameSettings.mediumFontSize color: GameSettings.textColor - text: qsTr("This application cannot be used without Bluetooth. Please switch Bluetooth ON to continue.") + text: root.permissionError + ? qsTr("Bluetooth permissions are not granted. Please grant the permissions in the system settings.") + : qsTr("This application cannot be used without Bluetooth. Please switch Bluetooth ON to continue.") } GameButton { diff --git a/examples/bluetooth/heartrate-game/connectionhandler.cpp b/examples/bluetooth/heartrate-game/connectionhandler.cpp index 039b94d3..0b86ed5f 100644 --- a/examples/bluetooth/heartrate-game/connectionhandler.cpp +++ b/examples/bluetooth/heartrate-game/connectionhandler.cpp @@ -8,10 +8,14 @@ #include <QtCore/qsystemdetection.h> +#if QT_CONFIG(permissions) +#include <QtCore/qcoreapplication.h> +#include <QtCore/qpermissions.h> +#endif + ConnectionHandler::ConnectionHandler(QObject *parent) : QObject(parent) { - connect(&m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, - this, &ConnectionHandler::hostModeChanged); + initLocalDevice(); } bool ConnectionHandler::alive() const @@ -23,11 +27,16 @@ bool ConnectionHandler::alive() const #else if (simulator) return true; - return m_localDevice.isValid() - && m_localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff; + return m_localDevice && m_localDevice->isValid() + && m_localDevice->hostMode() != QBluetoothLocalDevice::HostPoweredOff; #endif } +bool ConnectionHandler::hasPermission() const +{ + return m_hasPermission; +} + bool ConnectionHandler::requiresAddressType() const { #if QT_CONFIG(bluez) @@ -39,15 +48,36 @@ bool ConnectionHandler::requiresAddressType() const QString ConnectionHandler::name() const { - return m_localDevice.name(); + return m_localDevice ? m_localDevice->name() : QString(); } QString ConnectionHandler::address() const { - return m_localDevice.address().toString(); + return m_localDevice ? m_localDevice->address().toString() : QString(); } void ConnectionHandler::hostModeChanged(QBluetoothLocalDevice::HostMode /*mode*/) { emit deviceChanged(); } + +void ConnectionHandler::initLocalDevice() +{ +#if QT_CONFIG(permissions) + QBluetoothPermission permission{}; + switch (qApp->checkPermission(permission)) { + case Qt::PermissionStatus::Undetermined: + qApp->requestPermission(permission, this, &ConnectionHandler::initLocalDevice); + return; + case Qt::PermissionStatus::Denied: + return; + case Qt::PermissionStatus::Granted: + break; // proceed to initialization + } +#endif + m_localDevice = new QBluetoothLocalDevice(this); + connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, + this, &ConnectionHandler::hostModeChanged); + m_hasPermission = true; + emit deviceChanged(); +} diff --git a/examples/bluetooth/heartrate-game/connectionhandler.h b/examples/bluetooth/heartrate-game/connectionhandler.h index eea1036c..7cacd56f 100644 --- a/examples/bluetooth/heartrate-game/connectionhandler.h +++ b/examples/bluetooth/heartrate-game/connectionhandler.h @@ -15,6 +15,7 @@ class ConnectionHandler : public QObject Q_OBJECT Q_PROPERTY(bool alive READ alive NOTIFY deviceChanged) + Q_PROPERTY(bool hasPermission READ hasPermission NOTIFY deviceChanged) Q_PROPERTY(QString name READ name NOTIFY deviceChanged) Q_PROPERTY(QString address READ address NOTIFY deviceChanged) Q_PROPERTY(bool requiresAddressType READ requiresAddressType CONSTANT) @@ -24,6 +25,7 @@ public: explicit ConnectionHandler(QObject *parent = nullptr); bool alive() const; + bool hasPermission() const; bool requiresAddressType() const; QString name() const; QString address() const; @@ -33,9 +35,11 @@ signals: private slots: void hostModeChanged(QBluetoothLocalDevice::HostMode mode); + void initLocalDevice(); private: - QBluetoothLocalDevice m_localDevice; + QBluetoothLocalDevice *m_localDevice = nullptr; + bool m_hasPermission = false; }; #endif // CONNECTIONHANDLER_H diff --git a/examples/bluetooth/heartrate-game/devicefinder.cpp b/examples/bluetooth/heartrate-game/devicefinder.cpp index 5d1c129a..cdaa83d4 100644 --- a/examples/bluetooth/heartrate-game/devicefinder.cpp +++ b/examples/bluetooth/heartrate-game/devicefinder.cpp @@ -8,6 +8,11 @@ #include <QtBluetooth/qbluetoothdeviceinfo.h> +#if QT_CONFIG(permissions) +#include <QtCore/qcoreapplication.h> +#include <QtCore/qpermissions.h> +#endif + DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent): BluetoothBaseClass(parent), m_deviceHandler(handler) @@ -43,6 +48,21 @@ DeviceFinder::~DeviceFinder() void DeviceFinder::startSearch() { +#if QT_CONFIG(permissions) + //! [permissions] + QBluetoothPermission permission{}; + switch (qApp->checkPermission(permission)) { + case Qt::PermissionStatus::Undetermined: + qApp->requestPermission(permission, this, &DeviceFinder::startSearch); + return; + case Qt::PermissionStatus::Denied: + setError(tr("Bluetooth permissions not granted!")); + return; + case Qt::PermissionStatus::Granted: + break; // proceed to search + } + //! [permissions] +#endif // QT_CONFIG(permissions) clearMessages(); m_deviceHandler->setDevice(nullptr); qDeleteAll(m_devices); diff --git a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc index 256ff243..c45f4643 100644 --- a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc +++ b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc @@ -45,6 +45,14 @@ \include examples-run.qdocinc + \section1 Checking Bluetooth Permissions + + Before the application can start using Bluetooth, we have to check that + appropriate permissions were granted: + + \snippet heartrate-game/devicefinder.cpp permissions + + \section1 Visual Tour The application searches for all Bluetooth Low Energy peripheral devices in the vicinity. |