summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTimur Pocheptsov <timur.pocheptsov@qt.io>2023-01-04 14:18:43 +0100
committerTimur Pocheptsov <timur.pocheptsov@qt.io>2023-03-03 09:19:15 +0100
commit93759889214973d5611e75db7c9f6806f83820e1 (patch)
tree4601ca5a330ab3e26178da2b1b0b226345eccee0 /examples
parentbdf5a57410f3b3ccc05164c0bd03bde79c9a7d76 (diff)
downloadqtconnectivity-93759889214973d5611e75db7c9f6806f83820e1.tar.gz
lowenergyscanner: use permissions API
To explicitly handle Bluetooth permissions, not relying on QtBluetooth silently doing this under the hood. Task-number: QTBUG-109964 Change-Id: If8851e764bd0d06bddac815e8c8d1a88eb749438 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/bluetooth/lowenergyscanner/device.cpp20
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc6
2 files changed, 26 insertions, 0 deletions
diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp
index d645931c..f6c346c3 100644
--- a/examples/bluetooth/lowenergyscanner/device.cpp
+++ b/examples/bluetooth/lowenergyscanner/device.cpp
@@ -9,6 +9,12 @@
#include <qbluetoothlocaldevice.h>
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothservicediscoveryagent.h>
+
+#if QT_CONFIG(permissions)
+#include <QGuiApplication>
+#include <QPermission>
+#endif
+
#include <QDebug>
#include <QList>
#include <QMetaEnum>
@@ -43,6 +49,20 @@ Device::~Device()
void Device::startDeviceDiscovery()
{
+#if QT_CONFIG(permissions)
+ //! [les-bluetooth-permission]
+ const auto permissionStatus = qApp->checkPermission(QBluetoothPermission{});
+ if (permissionStatus == Qt::PermissionStatus::Undetermined) {
+ qApp->requestPermission(QBluetoothPermission{}, this, &Device::startDeviceDiscovery);
+ return;
+ }
+ if (permissionStatus == Qt::PermissionStatus::Denied) {
+ setUpdate("Bluetooth permission required.");
+ return;
+ }
+ //! [les-bluetooth-permission]
+#endif // QT_CONFIG(permissions)
+
qDeleteAll(devices);
devices.clear();
emit devicesUpdated();
diff --git a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
index bba28796..9d013203 100644
--- a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
+++ b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
@@ -32,6 +32,12 @@
\include examples-run.qdocinc
+ \section1 Requesting Permission to use Bluetooth
+ Before the application can start using Bluetooth, we have to check Bluetooth
+ permission, and if its status is not determined yet, request this permission:
+
+ \snippet lowenergyscanner/device.cpp les-bluetooth-permission
+
\section1 Scanning for Devices
The first step is to find all peripheral devices. The devices can be found using