Skip to content

Commit

Permalink
Merge pull request #2928 from MerginMaps/fix-permissions-qt-6_6_0
Browse files Browse the repository at this point in the history
Fix bluetooth and location permissions for Qt 6.6.0
  • Loading branch information
PeterPetrik authored Nov 30, 2023
2 parents 7fefc47 + 057ce52 commit a2e0473
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
17 changes: 16 additions & 1 deletion app/position/providers/bluetoothpositionprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "coreutils.h"
#include "androidutils.h"
#include "inpututils.h"
#include <QPermissions>

NmeaParser::NmeaParser() : QgsNmeaConnection( new QBluetoothSocket() )
{
Expand Down Expand Up @@ -134,8 +135,22 @@ void BluetoothPositionProvider::startReconnectionTime()
void BluetoothPositionProvider::handleLostConnection()
{
// we want to reconnect, but only to devices that are paired
QBluetoothPermission btPermission;
Qt::PermissionStatus permissionStatus;

if ( mReceiverDevice->pairingStatus( mTargetAddress ) == QBluetoothLocalDevice::Unpaired )
if ( qApp && ( permissionStatus = qApp->checkPermission( btPermission ) ) != Qt::PermissionStatus::Granted )
{
if ( permissionStatus == Qt::PermissionStatus::Undetermined )
{
qApp->requestPermission( btPermission, []() {} );
startReconnectionTime();
}
else
{
setState( tr( "Bluetooth permission disabled" ), State::NoConnection ); // permanent error
}
}
else if ( mReceiverDevice->pairingStatus( mTargetAddress ) == QBluetoothLocalDevice::Unpaired )
{
setState( tr( "Could not connect to device, not paired" ), State::NoConnection );
}
Expand Down
35 changes: 34 additions & 1 deletion app/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ ApplicationWindow {
}
}

LocationPermission {
id: locationPermission
accuracy: LocationPermission.Precise

function requestPermissionAsync() {
if ( locationPermission.status === Qt.Granted ) {
return true;
}
else if ( locationPermission.status === Qt.Undetermined ) {
locationPermission.request()
}
else if ( locationPermission.status === Qt.Denied ) {
__inputUtils.log("Permissions", "Location permission is denied")
showMessage( qsTr( "Location permission is required to show your location on map. Please enable it in system settings." ) );
}
return false;
}
}

MainPanel {
id: mainPanel

Expand All @@ -250,11 +269,17 @@ ApplicationWindow {
mapThemesPanel.visible = true
stateManager.state = "misc"
}
onMyLocationClicked: map.centerToPosition()
onMyLocationClicked: {
if ( locationPermission.requestPermissionAsync() ) {
map.centerToPosition()
}
}

onMyLocationHold: {
if ( locationPermission.requestPermissionAsync() ) {
__appSettings.autoCenterMapChecked = !__appSettings.autoCenterMapChecked
showMessage( __appSettings.autoCenterMapChecked ? qsTr("GPS auto-center mode on") : qsTr("GPS auto-center mode off") )
}
}
onOpenSettingsClicked: settingsPanel.visible = true
onZoomToProject: {
Expand Down Expand Up @@ -809,6 +834,14 @@ ApplicationWindow {

function onLoadingFinished() {
projectLoadingScreen.visible = false

// check location permission
if ( locationPermission.status === Qt.Undetermined ) {
locationPermission.request();
}
else if ( locationPermission.status === Qt.Denied ) {
__inputUtils.log("Permissions", "Location permission is denied")
}
}

function onLoadingErrorFound() {
Expand Down
45 changes: 34 additions & 11 deletions app/qml/misc/AddPositionProviderPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* *
***************************************************************************/

import QtCore
import QtQuick
import QtQuick.Controls
import Qt5Compat.GraphicalEffects
Expand Down Expand Up @@ -39,6 +40,14 @@ Page {
withBackButton: true
}

BluetoothPermission {
id: btPermission

communicationModes: BluetoothPermission.Access

onStatusChanged: btModel.initiateBtDiscovery()
}

focus: true

Keys.onReleased: function( event ) {
Expand All @@ -57,21 +66,35 @@ Page {
model: BluetoothDiscoveryModel {
id: btModel
discovering: false
}

Component.onCompleted: {
// Is bluetooth turned on?
// For Android we need to opt to enable Bluetooth and listen on response in the connections component.
if ( __inputUtils.isBluetoothTurnedOn() )
{
btModel.discovering = true
}
else
{
__inputUtils.turnBluetoothOn()
function initiateBtDiscovery() {
// Is bluetooth permission granted and is bluetooth turned on?
// For Android we need to opt to enable Bluetooth and listen on response in the connections component.

if ( btPermission.status === Qt.Granted )
{
if ( __inputUtils.isBluetoothTurnedOn() )
{
btModel.discovering = true
}
else
{
__inputUtils.turnBluetoothOn()
}
}
else if ( btPermission.status === Qt.Denied )
{
__inputUtils.showNotification( qsTr( "Bluetooth permission is required in order to connect to external receivers. Please enable it in system settings" ) )
}
else
{
btPermission.request()
}
}
}

Component.onCompleted: btModel.initiateBtDiscovery()

Connections {
target: __androidUtils

Expand Down

0 comments on commit a2e0473

Please sign in to comment.