Last active
November 1, 2023 10:57
-
-
Save ekke/843b58a884dd375641d08a04b7f35db6 to your computer and use it in GitHub Desktop.
PopupPermissionBluetooth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| see my blog: | |
| https://ekkesapps.wordpress.com/qt-6-qmake/qt-permissions-api-new/ | |
| .cpp: | |
| ... | |
| #include <QGuiApplication> | |
| #include <QPermissions> | |
| ... | |
| // 0: undetermined, 1: granted, 2: denied | |
| int DataUtil::checkPermissionsBluetoothGranted() | |
| { | |
| #if QT_CONFIG(permissions) | |
| QBluetoothPermission bluetoothPermission; | |
| bluetoothPermission.setCommunicationModes(QBluetoothPermission::Access); | |
| switch (qApp->checkPermission(bluetoothPermission)) { | |
| case Qt::PermissionStatus::Undetermined: | |
| qDebug() << "BT Permissions UNDETERMINED"; | |
| return 0; | |
| case Qt::PermissionStatus::Denied: | |
| qDebug() << "BT Permissions DENIED"; | |
| return 2; | |
| case Qt::PermissionStatus::Granted: | |
| qDebug() << "BT Permissions GRANTED"; | |
| return 1; | |
| } | |
| #endif | |
| // unsupported OS | |
| qDebug() << "BT Permissions ARE Granted"; | |
| return 1; | |
| } | |
| void DataUtil::onCheckPermissionsBluetooth() { | |
| // The QT_CONFIG(permissions) macro ensures that the code will work as before on platforms where permissions are not available. | |
| #if QT_CONFIG(permissions) | |
| QBluetoothPermission bluetoothPermission; | |
| bluetoothPermission.setCommunicationModes(QBluetoothPermission::Access); | |
| switch (qApp->checkPermission(bluetoothPermission)) { | |
| case Qt::PermissionStatus::Undetermined: | |
| // we'll request the Permission and come back | |
| qApp->requestPermission(bluetoothPermission, this, | |
| &DataUtil::onCheckPermissionsBluetooth); | |
| return; | |
| case Qt::PermissionStatus::Denied: | |
| qDebug() << "BT Permissions DENIED"; | |
| emit bluetoothPermissionStatus(2); | |
| return; | |
| case Qt::PermissionStatus::Granted: | |
| break; // Proceed and emit bluetoothPermissionStatus(1) | |
| } | |
| #endif | |
| // Go On | |
| qDebug() << "BT Permissions ARE Granted"; | |
| emit bluetoothPermissionStatus(1); | |
| } | |
| ---- | |
| .hpp: | |
| Q_INVOKABLE | |
| void onCheckPermissionsBluetooth(); | |
| Q_INVOKABLE | |
| int checkPermissionsBluetoothGranted(); | |
| signals: | |
| void bluetoothPermissionStatus(int bluetoothPermissionStatus); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ekke (Ekkehard Gentz) @ekkescorner | |
| import QtQuick | |
| import QtQuick.Controls | |
| import QtQuick.Layouts | |
| import QtQuick.Controls.Material | |
| // import "../common" | |
| // Popup requests Bluetooth Permissions (C++) | |
| // Hint: since 6.6 you can also use QML Application Permissions | |
| // This Popup is used in ekke's apps for Bluetooth LE Ring Scanner | |
| // There are other Apps from ekke, where BT LE is used for | |
| // Waiterlock (to login) | |
| // German eHealthCard | |
| // Control motors of slatted frames for beds (Lattoflex) | |
| // infoTexts are special for BT LE Barcode Scanner, change them for other use-cases | |
| Popup { | |
| id: popupPermission | |
| property bool goOn: false | |
| property var infoTexts: [qsTr("This Application needs BT LE Permissions (Devices Nearby and Location) to be able to scan Barcodes.\nHit YES to request the Permission now, you'll be redirected to App Permission Settings.\nHint: The App will never use your Location."), | |
| qsTr("Thanks, BT LE Permissions (Devices Nearby and Location) are now granted.\nAs next the App will search for BT LE Scanner."), | |
| qsTr("You have denied BT LE Permissions (Devices Nearby and/or Location).\nFrom your App Settings you can grant the Permissions again."), | |
| qsTr("Please wait...\nYour Device will ask you for Devices Nearby and Location Permissions.\nHint: Location Permissions will never be used.")] | |
| // -1: uninitialized, 0: undetermined, 1: granted, 2: denied, 3: waiting | |
| property int permissionStatus: -1 | |
| onPermissionStatusChanged: { | |
| if(permissionStatus < 0 || permissionStatus > 3) { | |
| console.log("WARNING: PermissionStatus must be 0...3, but was ", permissionStatus) | |
| return | |
| } | |
| infoText.text = infoTexts[permissionStatus] | |
| } | |
| modal: true | |
| focus: true | |
| closePolicy: Popup.NoAutoClose | |
| parent: Overlay.overlay | |
| anchors.centerIn: Overlay.overlay | |
| implicitWidth: Overlay.overlay.width * 0.9 | |
| ColumnLayout { | |
| id: mainColumn | |
| anchors.fill: parent | |
| Label { | |
| id: titleText | |
| Layout.fillWidth: true | |
| Layout.topMargin: 24 | |
| Layout.leftMargin: 24 | |
| Layout.rightMargin: 24 | |
| Layout.bottomMargin: 12 | |
| text: qsTr("Bluetooth LE Permissions") | |
| wrapMode: Label.WordWrap | |
| color: Material.primaryColor | |
| font.pixelSize: 24 | |
| } | |
| Label { | |
| id: infoText | |
| Layout.fillWidth: true | |
| Layout.leftMargin: 24 | |
| Layout.bottomMargin: 24 | |
| text: qsTr("...") | |
| wrapMode: Label.WordWrap | |
| font.pixelSize: 20 | |
| } | |
| RowLayout { | |
| id: undeterminedRow | |
| visible: permissionStatus === 0 | |
| Layout.leftMargin: 24 | |
| Layout.rightMargin: 24 | |
| Layout.bottomMargin: 6 | |
| Item { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 1 | |
| } | |
| Button { | |
| text: qsTr("No") | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 2 | |
| onClicked: popupPermission.close() | |
| } | |
| Item { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 1 | |
| } | |
| Button { | |
| text: qsTr("Yes") | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 2 | |
| highlighted: true | |
| onClicked: { | |
| permissionStatus = 3 | |
| requestPermissions() | |
| } | |
| } | |
| } | |
| RowLayout { | |
| id: grantedRow | |
| visible: permissionStatus === 1 | |
| Layout.leftMargin: 24 | |
| Layout.rightMargin: 24 | |
| Layout.bottomMargin: 6 | |
| Item { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 3 | |
| } | |
| Button { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 2 | |
| text: qsTr("Go On") | |
| highlighted: true | |
| onClicked: { | |
| popupPermission.goOn = true | |
| popupPermission.close() | |
| } | |
| } | |
| } | |
| RowLayout { | |
| id: deniedRow | |
| visible: permissionStatus === 2 | |
| Layout.leftMargin: 24 | |
| Layout.rightMargin: 24 | |
| Layout.bottomMargin: 6 | |
| Item { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 3 | |
| } | |
| Button { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 2 | |
| text: qsTr("OK") | |
| highlighted: true | |
| onClicked: popupPermission.close() | |
| } | |
| } | |
| RowLayout { | |
| id: waitingRow | |
| visible: permissionStatus === 3 | |
| Layout.leftMargin: 24 | |
| Layout.rightMargin: 24 | |
| Layout.bottomMargin: 6 | |
| Item { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 3 | |
| } | |
| Button { | |
| Layout.fillWidth: true | |
| Layout.preferredWidth : 2 | |
| text: qsTr("Cancel") | |
| highlighted: true | |
| onClicked: popupPermission.close() | |
| } | |
| } | |
| } // main column layout | |
| function requestPermissions() { | |
| dataUtil.onCheckPermissionsBluetooth() | |
| } | |
| Connections { | |
| target: dataUtil | |
| function onBluetoothPermissionStatus(bluetoothPermissionStatus) { | |
| popupPermission.permissionStatus = bluetoothPermissionStatus | |
| } | |
| } | |
| onOpened: { | |
| // reset goOn | |
| popupPermission.goOn = false | |
| } | |
| onClosed: { | |
| // reset permissionStatus | |
| popupPermission.permissionStatus = -1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment