Skip to content

Commit

Permalink
Comms: Fix Bluetooth Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Jan 7, 2025
1 parent 533dc39 commit aba9397
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/Comms/BluetoothLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "DeviceInfo.h"
#include "QGCLoggingCategory.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QPermissions>

QGC_LOGGING_CATEGORY(BluetoothLinkLog, "qgc.comms.bluetoothlink")

/*===========================================================================*/
Expand Down Expand Up @@ -364,6 +367,8 @@ BluetoothLink::BluetoothLink(SharedLinkConfigurationPtr &config, QObject *parent
{
// qCDebug(BluetoothLinkLog) << Q_FUNC_INFO << this;

_checkPermission();

_workerThread->setObjectName(QStringLiteral("Bluetooth_%1").arg(_bluetoothConfig->name()));

_worker->moveToThread(_workerThread);
Expand Down Expand Up @@ -439,3 +444,29 @@ void BluetoothLink::_writeBytes(const QByteArray& bytes)
{
(void) QMetaObject::invokeMethod(_worker, "writeData", Qt::QueuedConnection, Q_ARG(QByteArray, bytes));
}

void BluetoothLink::_checkPermission()
{
QBluetoothPermission permission;
permission.setCommunicationModes(QBluetoothPermission::Access);

const Qt::PermissionStatus permissionStatus = QCoreApplication::instance()->checkPermission(permission);
if (permissionStatus == Qt::PermissionStatus::Undetermined) {
QCoreApplication::instance()->requestPermission(permission, this, [this](const QPermission &permission) {
_handlePermissionStatus(permission.status());
});
} else {
_handlePermissionStatus(permissionStatus);
}
}

void BluetoothLink::_handlePermissionStatus(Qt::PermissionStatus permissionStatus)
{
if (permissionStatus != Qt::PermissionStatus::Granted) {
qCWarning(BluetoothLinkLog) << "Bluetooth Permission Denied";
_onErrorOccurred("Bluetooth Permission Denied");
_onDisconnected();
} else {
qCDebug(BluetoothLinkLog) << "Bluetooth Permission Granted";
}
}
2 changes: 2 additions & 0 deletions src/Comms/BluetoothLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ private slots:

private:
bool _connect() override;
void _checkPermission();
void _handlePermissionStatus(Qt::PermissionStatus permissionStatus);

const BluetoothConfiguration *_bluetoothConfig = nullptr;
BluetoothWorker *_worker = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/PositionManager/PositionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ void QGCPositionManager::_checkPermission()
QLocationPermission locationPermission;
locationPermission.setAccuracy(QLocationPermission::Precise);

const Qt::PermissionStatus permissionStatus = qgcApp()->checkPermission(locationPermission);
const Qt::PermissionStatus permissionStatus = QCoreApplication::instance()->checkPermission(locationPermission);
if (permissionStatus == Qt::PermissionStatus::Undetermined) {
qgcApp()->requestPermission(locationPermission, this, [this](const QPermission &permission) {
QCoreApplication::instance()->requestPermission(locationPermission, this, [this](const QPermission &permission) {
_handlePermissionStatus(permission.status());
});
} else {
Expand Down

0 comments on commit aba9397

Please sign in to comment.