summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2023-06-22 14:30:01 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-06-23 12:56:56 +0000
commit425654d54a401e0142ac528f44ab53df05636874 (patch)
tree94a0b333449a78aa8f562a8675e3841e35ade36f
parente9a2a004331b6892a0075494c95a55a767291ae0 (diff)
LowEnergyScanner example: migrate to QML Permission API
Update the example to use QML Permission API. This is not strictly necessary, because the example provides custom C++ wrappers around Qt Bluetooth classes, and the permission request was already conveniently implemented there. However, this example is a good candidate to show QML Permission API usage with Qt Bluetooth. That's the reason for the code update, as well as a documentation improvement. Fixes: QTBUG-114640 Change-Id: I3ffeb61a83c205724da8b462f930604e1adb26c0 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io> (cherry picked from commit 92e095a4ff780145d91feae2be33184be4503b40) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-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