summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/bluetooth/lowenergyscanner/Devices.qml43
-rw-r--r--examples/bluetooth/lowenergyscanner/device.cpp16
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc16
3 files changed, 46 insertions, 29 deletions
diff --git a/examples/bluetooth/lowenergyscanner/Devices.qml b/examples/bluetooth/lowenergyscanner/Devices.qml
index 40afce7f..b41c259d 100644
--- a/examples/bluetooth/lowenergyscanner/Devices.qml
+++ b/examples/bluetooth/lowenergyscanner/Devices.qml
@@ -3,6 +3,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
pragma ComponentBehavior: Bound
+import QtCore
import QtQuick
Rectangle {
@@ -14,6 +15,19 @@ Rectangle {
width: 300
height: 600
+ function toggleDiscovery() {
+ if (!Device.state) {
+ Device.startDeviceDiscovery()
+ // if startDeviceDiscovery() failed Device.state is not set
+ if (Device.state) {
+ info.dialogText = "Searching..."
+ info.visible = true
+ }
+ } else {
+ Device.stopDeviceDiscovery()
+ }
+ }
+
onDeviceStateChanged: {
if (!Device.state)
info.visible = false
@@ -99,23 +113,32 @@ Rectangle {
onButtonClick: Device.useRandomAddress = !Device.useRandomAddress
}
+ //! [permission-object]
+ BluetoothPermission {
+ id: permission
+ communicationModes: BluetoothPermission.Access
+ onStatusChanged: {
+ if (permission.status === Qt.PermissionStatus.Denied)
+ Device.update = "Bluetooth permission required"
+ else if (permission.status === Qt.PermissionStatus.Granted)
+ devicesPage.toggleDiscovery()
+ }
+ }
+ //! [permission-object]
+
Menu {
id: menu
anchors.bottom: parent.bottom
menuWidth: parent.width
menuHeight: (parent.height / 6)
menuText: Device.update
+ //! [permission-request]
onButtonClick: {
- if (!Device.state) {
- Device.startDeviceDiscovery()
- // if startDeviceDiscovery() failed Device.state is not set
- if (Device.state) {
- info.dialogText = "Searching..."
- info.visible = true
- }
- } else {
- Device.stopDeviceDiscovery()
- }
+ if (permission.status === Qt.PermissionStatus.Undetermined)
+ permission.request()
+ else if (permission.status === Qt.PermissionStatus.Granted)
+ devicesPage.toggleDiscovery()
}
+ //! [permission-request]
}
}
diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp
index ae74d54c..93960c31 100644
--- a/examples/bluetooth/lowenergyscanner/device.cpp
+++ b/examples/bluetooth/lowenergyscanner/device.cpp
@@ -48,22 +48,6 @@ Device::~Device()
void Device::startDeviceDiscovery()
{
-#if QT_CONFIG(permissions)
- //! [les-bluetooth-permission]
- QBluetoothPermission permission;
- permission.setCommunicationModes(QBluetoothPermission::Access);
- const auto permissionStatus = qApp->checkPermission(permission);
- if (permissionStatus == Qt::PermissionStatus::Undetermined) {
- qApp->requestPermission(permission, 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 df7f8484..d7f76c4d 100644
--- a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
+++ b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
@@ -34,10 +34,20 @@
\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
+ On certain platforms, it is required to explicitly grant permissions for
+ using Bluetooth. The example uses \c BluetoothPermission QML object to
+ check and request the permissions, if required:
+
+ \snippet lowenergyscanner/Devices.qml permission-object
+
+ The permission request dialog is triggered when the user tries to start
+ the device discovery, and the permission status is \c {Undetermined}:
+
+ \snippet lowenergyscanner/Devices.qml permission-request
+
+ The device discovery starts if the permission is granted by the user.
+ Otherwise the application is non-functional.
\section1 Scanning for Devices