From 425654d54a401e0142ac528f44ab53df05636874 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 22 Jun 2023 14:30:01 +0200 Subject: 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 (cherry picked from commit 92e095a4ff780145d91feae2be33184be4503b40) Reviewed-by: Qt Cherry-pick Bot --- examples/bluetooth/lowenergyscanner/Devices.qml | 43 +++++++++++++++++----- examples/bluetooth/lowenergyscanner/device.cpp | 16 -------- .../lowenergyscanner/doc/src/lowenergyscanner.qdoc | 16 ++++++-- 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 -- cgit v1.2.3