diff --git a/src/Comms/BluetoothLink.cc b/src/Comms/BluetoothLink.cc index ab6bbaac419..f812bb68b46 100644 --- a/src/Comms/BluetoothLink.cc +++ b/src/Comms/BluetoothLink.cc @@ -12,6 +12,9 @@ #include "DeviceInfo.h" #include "QGCLoggingCategory.h" +#include +#include + QGC_LOGGING_CATEGORY(BluetoothLinkLog, "qgc.comms.bluetoothlink") /*===========================================================================*/ @@ -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); @@ -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"; + } +} diff --git a/src/Comms/BluetoothLink.h b/src/Comms/BluetoothLink.h index 1338959ae2d..2fb5d0af4dc 100644 --- a/src/Comms/BluetoothLink.h +++ b/src/Comms/BluetoothLink.h @@ -201,6 +201,8 @@ private slots: private: bool _connect() override; + void _checkPermission(); + void _handlePermissionStatus(Qt::PermissionStatus permissionStatus); const BluetoothConfiguration *_bluetoothConfig = nullptr; BluetoothWorker *_worker = nullptr; diff --git a/src/PositionManager/PositionManager.cpp b/src/PositionManager/PositionManager.cpp index 00525a2fc8a..6822953036b 100644 --- a/src/PositionManager/PositionManager.cpp +++ b/src/PositionManager/PositionManager.cpp @@ -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 {