Skip to content

Commit

Permalink
Request bluetooth permission in context
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasMizera committed Nov 30, 2023
1 parent c49f540 commit 057ce52
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 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
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

1 comment on commit 057ce52

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 23.11.490811 just submitted!

Please sign in to comment.