diff options
author | Juha Vuolle <juha.vuolle@insta.fi> | 2022-04-06 12:30:28 +0300 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@insta.fi> | 2022-04-12 09:06:27 +0300 |
commit | cc121cdef160a4ef528d1483ac365ac8319a1e42 (patch) | |
tree | 4ec7996c040fc815d746eece65ce45d79983726c /src/bluetooth/qlowenergycontroller.cpp | |
parent | dd8c21c9c09407003b933712c9d7c35f7b02cfbb (diff) |
Improve Android-12 bluetooth permission error reporting
The original commit to add support for new Android-12 (API Level 31)
Bluetooth permissions covered the possible codepaths the QtBluetooth
module user might use. This commit seeks to improve the error reporting
insofar as they are due to missing permissions.
Since there are many possible codepaths to take, the following
elaborates function-by-function the details for easier understanding.
QBluetoothDeviceDiscoveryAgent::start()
This is the main entry point for using device discovery and checks the
needed permissions.
QBluetoothServiceDiscoveryAgent::start()
This is the main entry point for using service discovery. In most use
cases it uses device discovery first which ensures adequate permissions
/ failure if not granted. There is one codepath bypassing this when
setRemoteAddress() has been called, for which reason there is a
permission check.
QBluetoothSocket::localName() localAddress() connectToServiceHelper()
These functions require permissions and can be called in any order so
all of them check for permissions. Notably this commit moves the
permission check from the constructor to these functions.
QBluetoothServiceInfo::registerService()
This function creates a local device and initiates active listening.
The local device creation in this function will fail if it is not
granted permissions, but this commit adds an earlier permission check
for slightly improved error reporting.
QBluetoothServer::listen()
This is the main entry point for using the server and it checks for
permission. The local device iteration in the function would also fail
without permissions, but the permission check is added for slightly
improved error reporting. Notably the initiateActiveListening() does not
have permission check as there is no code path to it without already
having the permission.
QBluetoothLocalDevice::allDevices() constructor() setHostMode
The local device needs permissions already at construction time. This
commit documents this on the local device documentation. In addition
the local device has a widely used allDevices() static function for
iterating devices which needs its own permission checks. Also the
setHostMode() function requires Advertise permission as it ultimately
uses ACTION_REQUEST_DISCOVERABLE Intent.
QLowEnergyController::connectToDevice() startAdvertising() addService()
This commit moves the permission check from the construction time to
these individual entry functions, as the creation itself does not seem
to require permissions (tested this with a somewhat contrived setup
which dodges earlier permission checks *). The aim here is to
slightly improve the error reporting if an error is suspected to be due
to missing permissions. User may decline the first permission requests
and call subsequent functions which may or may not trigger a new
permission query. For this reason this commit also adds another
permission to startAdvertising().
*) In most if not all practical scenarios a local device and a device
discovery has been made which both perform the permission checks.
In addition the header include order was reorganized to preferred order
where this commit touches them
This commit amends a0542cff15d58db83a835f1a378a55a0ec117c9c
Task-number: QTBUG-99590
Pick-to: 5.15 6.2 6.3
Change-Id: Ic34a0184a79f6ea7c4a6643b9603e8ddaa18e28e
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src/bluetooth/qlowenergycontroller.cpp')
-rw-r--r-- | src/bluetooth/qlowenergycontroller.cpp | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/bluetooth/qlowenergycontroller.cpp b/src/bluetooth/qlowenergycontroller.cpp index 90eb98e7..35031fa1 100644 --- a/src/bluetooth/qlowenergycontroller.cpp +++ b/src/bluetooth/qlowenergycontroller.cpp @@ -54,6 +54,7 @@ #include "qlowenergycontroller_bluez_p.h" #elif defined(QT_ANDROID_BLUETOOTH) #include "qlowenergycontroller_android_p.h" +#include "android/androidutils_p.h" #elif defined(QT_WINRT_BLUETOOTH) #include "qtbluetoothglobal_p.h" #include "qlowenergycontroller_winrt_p.h" @@ -68,7 +69,9 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT) -Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINDOWS) +#if defined(QT_ANDROID_BLUETOOTH) +Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID) +#endif /*! \class QLowEnergyController @@ -612,6 +615,7 @@ void QLowEnergyController::connectToDevice() } if (!d->isValidLocalAdapter()) { + qCWarning(QT_BT) << "connectToDevice() LE controller has invalid adapter"; d->setError(QLowEnergyController::InvalidBluetoothAdapterError); return; } @@ -818,6 +822,13 @@ QLowEnergyService *QLowEnergyController::addService(const QLowEnergyServiceData return nullptr; } +#if defined(QT_ANDROID_BLUETOOTH) + if (!ensureAndroidPermission(BluetoothPermission::Connect)) { + qCWarning(QT_BT_ANDROID) << "addService() failed due to missing permissions"; + return nullptr; + } +#endif + Q_D(QLowEnergyController); QLowEnergyService *newService = d->addServiceHelper(service); if (newService) |