summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@insta.fi>2022-05-13 10:00:08 +0300
committerJuha Vuolle <juha.vuolle@insta.fi>2022-05-30 17:29:15 +0300
commite24f99cc1dc3fa4e8131e590b0663695c4f81b63 (patch)
tree562bfe28f90fa93019e81153c01c0178aabff4c0 /src
parent3aafe9d5ce117858143dbb527f742cf875aa83e8 (diff)
Use the new "permissions error" code on macOS
Qt 6.4 introduces new error code for permission errors (QTBUG-102373). This commit makes use of the new error code. This consists of two parts: 1) If the bluetooth permission key is missing from the application's Info.plist and we know it's needed => permission error. This is limited to Low Energy because classic bluetooth does not need it on macOS, and iOS does not support classic bluetooth. If the key is missing, it is an application development/deployment error and we should not make too many of these checks - few places is enough. 2) At runtime the platform prompts for bluetooth permission and the user may disallow it => permission error. Amends: d58d134d25695dd044fc13d57a4d754b1dfb4e4a Fixes: QTBUG-103388 Change-Id: I96de202e9ac8c33e51bc126389f62091d03eab70 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/darwin/btcentralmanager.mm16
-rw-r--r--src/bluetooth/darwin/btledeviceinquiry.mm9
-rw-r--r--src/bluetooth/darwin/btledeviceinquiry_p.h3
-rw-r--r--src/bluetooth/darwin/btperipheralmanager.mm5
-rw-r--r--src/bluetooth/darwin/uistrings.cpp2
-rw-r--r--src/bluetooth/darwin/uistrings_p.h2
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_darwin.mm8
-rw-r--r--src/bluetooth/qlowenergycontroller_darwin.mm15
-rw-r--r--src/bluetooth/qlowenergycontrollerbase.cpp3
9 files changed, 46 insertions, 17 deletions
diff --git a/src/bluetooth/darwin/btcentralmanager.mm b/src/bluetooth/darwin/btcentralmanager.mm
index 3d026d74..10fc9d7e 100644
--- a/src/bluetooth/darwin/btcentralmanager.mm
+++ b/src/bluetooth/darwin/btcentralmanager.mm
@@ -1280,17 +1280,13 @@ using DiscoveryMode = QLowEnergyService::DiscoveryMode;
// Let's check some states we do not like first:
if (state == CBManagerStateUnsupported || state == CBManagerStateUnauthorized) {
- if (managerState == CentralManagerUpdating) {
- // We tried to connect just to realize, LE is not supported. Report this.
- managerState = CentralManagerIdle;
- if (notifier)
+ managerState = CentralManagerIdle;
+ // The LE is not supported or its usage was not authorized
+ if (notifier) {
+ if (state == CBManagerStateUnsupported)
emit notifier->LEnotSupported();
- } else {
- // TODO: if we are here, LE _was_ supported and we first managed to update
- // and reset managerState from CentralManagerUpdating.
- managerState = CentralManagerIdle;
- if (notifier)
- emit notifier->CBManagerError(QLowEnergyController::InvalidBluetoothAdapterError);
+ else
+ emit notifier->CBManagerError(QLowEnergyController::MissingPermissionsError);
}
[self stopWatchers];
return;
diff --git a/src/bluetooth/darwin/btledeviceinquiry.mm b/src/bluetooth/darwin/btledeviceinquiry.mm
index 6cc61955..321223dc 100644
--- a/src/bluetooth/darwin/btledeviceinquiry.mm
+++ b/src/bluetooth/darwin/btledeviceinquiry.mm
@@ -233,7 +233,7 @@ QT_USE_NAMESPACE
[manager scanForPeripheralsWithServices:nil options:nil];
}
} // Else we ignore.
- } else if (state == CBManagerStateUnsupported || state == CBManagerStateUnauthorized) {
+ } else if (state == CBManagerStateUnsupported) {
if (internalState == InquiryActive) {
[self stopScanSafe];
// Not sure how this is possible at all,
@@ -244,7 +244,12 @@ QT_USE_NAMESPACE
internalState = ErrorLENotSupported;
emit notifier->LEnotSupported();
}
-
+ [manager setDelegate:nil];
+ } else if (state == CBManagerStateUnauthorized) {
+ if (internalState == InquiryActive)
+ [self stopScanSafe];
+ internalState = ErrorNotAuthorized;
+ emit notifier->CBManagerError(QBluetoothDeviceDiscoveryAgent::MissingPermissionsError);
[manager setDelegate:nil];
} else if (state == CBManagerStatePoweredOff) {
diff --git a/src/bluetooth/darwin/btledeviceinquiry_p.h b/src/bluetooth/darwin/btledeviceinquiry_p.h
index 58c66e56..fc82d4e7 100644
--- a/src/bluetooth/darwin/btledeviceinquiry_p.h
+++ b/src/bluetooth/darwin/btledeviceinquiry_p.h
@@ -86,7 +86,8 @@ enum LEInquiryState
InquiryFinished,
InquiryCancelled,
ErrorPoweredOff,
- ErrorLENotSupported
+ ErrorLENotSupported,
+ ErrorNotAuthorized
};
@interface QT_MANGLE_NAMESPACE(DarwinBTLEDeviceInquiry) : NSObject<CBCentralManagerDelegate, QT_MANGLE_NAMESPACE(GCDTimerDelegate)>
diff --git a/src/bluetooth/darwin/btperipheralmanager.mm b/src/bluetooth/darwin/btperipheralmanager.mm
index bafe2a23..a2747657 100644
--- a/src/bluetooth/darwin/btperipheralmanager.mm
+++ b/src/bluetooth/darwin/btperipheralmanager.mm
@@ -456,9 +456,12 @@ bool qt_validate_value_range(const QLowEnergyCharacteristicData &data)
explicitly added again."
*/
- if (peripheral.state == CBManagerStateUnauthorized || peripheral.state == CBManagerStateUnsupported) {
+ if (peripheral.state == CBManagerStateUnsupported) {
+ state = PeripheralState::idle;
emit notifier->LEnotSupported();
+ } else if (peripheral.state == CBManagerStateUnauthorized) {
state = PeripheralState::idle;
+ emit notifier->CBManagerError(QLowEnergyController::MissingPermissionsError);
}
#pragma clang diagnostic pop
diff --git a/src/bluetooth/darwin/uistrings.cpp b/src/bluetooth/darwin/uistrings.cpp
index 7260417f..434caa62 100644
--- a/src/bluetooth/darwin/uistrings.cpp
+++ b/src/bluetooth/darwin/uistrings.cpp
@@ -50,6 +50,7 @@ const char DD_INVALID_ADAPTER[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAg
const char DD_IO[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Input Output Error");
const char DD_NOTSUPPORTED[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Bluetooth LE is not supported");
const char DD_UNKNOWN_ERROR[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Unknown error");
+const char DD_MISSING_PERMISSION[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Missing permission");
const char DD_NOT_STARTED[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Cannot start device inquiry");
const char DD_NOT_STARTED_LE[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Cannot start low energy device inquiry");
const char DD_NOT_STOPPED[] = QT_TRANSLATE_NOOP("QBluetoothDeviceDiscoveryAgent", "Discovery cannot be stopped");
@@ -84,5 +85,6 @@ const char LEC_RDEV_NO_FOUND[] = QT_TRANSLATE_NOOP("QLowEnergyController", "Remo
const char LEC_NO_LOCAL_DEV[] = QT_TRANSLATE_NOOP("QLowEnergyController", "Cannot find local adapter");
const char LEC_IO_ERROR[] = QT_TRANSLATE_NOOP("QLowEnergyController", "Error occurred during connection I/O");
const char LEC_UNKNOWN_ERROR[] = QT_TRANSLATE_NOOP("QLowEnergyController", "Unknown Error");
+const char LEC_MISSING_PERMISSION[] = QT_TRANSLATE_NOOP("QLowEnergyController", "Missing permission");
QT_END_NAMESPACE
diff --git a/src/bluetooth/darwin/uistrings_p.h b/src/bluetooth/darwin/uistrings_p.h
index 97e6df72..162fcc22 100644
--- a/src/bluetooth/darwin/uistrings_p.h
+++ b/src/bluetooth/darwin/uistrings_p.h
@@ -64,6 +64,7 @@ extern const char DD_INVALID_ADAPTER[];
extern const char DD_IO[];
extern const char DD_NOTSUPPORTED[];
extern const char DD_UNKNOWN_ERROR[];
+extern const char DD_MISSING_PERMISSION[];
extern const char DD_NOT_STARTED[];
extern const char DD_NOT_STARTED_LE[];
extern const char DD_NOT_STOPPED[];
@@ -102,6 +103,7 @@ extern const char LEC_RDEV_NO_FOUND[];
extern const char LEC_NO_LOCAL_DEV[];
extern const char LEC_IO_ERROR[];
extern const char LEC_UNKNOWN_ERROR[];
+extern const char LEC_MISSING_PERMISSION[];
QT_END_NAMESPACE
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_darwin.mm b/src/bluetooth/qbluetoothdevicediscoveryagent_darwin.mm
index e7b931c8..46c76dc2 100644
--- a/src/bluetooth/qbluetoothdevicediscoveryagent_darwin.mm
+++ b/src/bluetooth/qbluetoothdevicediscoveryagent_darwin.mm
@@ -184,7 +184,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent
qCWarning(QT_BT_DARWIN)
<< "A proper Info.plist with NSBluetoothAlwaysUsageDescription "
"entry is required, cannot start device discovery";
- setError(QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod);
+ setError(QBluetoothDeviceDiscoveryAgent::MissingPermissionsError);
emit q_ptr->errorOccurred(lastError);
return;
}
@@ -439,6 +439,9 @@ void QBluetoothDeviceDiscoveryAgentPrivate::setError(QBluetoothDeviceDiscoveryAg
case QBluetoothDeviceDiscoveryAgent::UnsupportedPlatformError:
errorString = QCoreApplication::translate(DEV_DISCOVERY, DD_NOTSUPPORTED);
break;
+ case QBluetoothDeviceDiscoveryAgent::MissingPermissionsError:
+ errorString = QCoreApplication::translate(DEV_DISCOVERY, DD_MISSING_PERMISSION);
+ break;
case QBluetoothDeviceDiscoveryAgent::UnknownError:
default:
errorString = QCoreApplication::translate(DEV_DISCOVERY, DD_UNKNOWN_ERROR);
@@ -449,7 +452,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::setError(QBluetoothDeviceDiscoveryAg
void QBluetoothDeviceDiscoveryAgentPrivate::LEinquiryError(QBluetoothDeviceDiscoveryAgent::Error error)
{
Q_ASSERT(error == QBluetoothDeviceDiscoveryAgent::PoweredOffError
- || error == QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod);
+ || error == QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod
+ || error == QBluetoothDeviceDiscoveryAgent::MissingPermissionsError);
inquiryLE.reset();
diff --git a/src/bluetooth/qlowenergycontroller_darwin.mm b/src/bluetooth/qlowenergycontroller_darwin.mm
index e2a5196b..224f4f8e 100644
--- a/src/bluetooth/qlowenergycontroller_darwin.mm
+++ b/src/bluetooth/qlowenergycontroller_darwin.mm
@@ -208,6 +208,14 @@ void QLowEnergyControllerPrivateDarwin::connectToDevice()
Q_ASSERT_X(state == QLowEnergyController::UnconnectedState,
Q_FUNC_INFO, "invalid state");
+ if (qt_appNeedsBluetoothUsageDescription()
+ && !qt_appPlistContainsDescription(bluetoothUsageKey)) {
+ qCWarning(QT_BT_DARWIN)
+ << "The Info.plist file is required to contain "
+ "'NSBluetoothAlwaysUsageDescription' entry";
+ return _q_CBManagerError(QLowEnergyController::MissingPermissionsError);
+ }
+
if (!isValid()) {
// init() had failed or was never called.
return _q_CBManagerError(QLowEnergyController::UnknownError);
@@ -702,11 +710,13 @@ void QLowEnergyControllerPrivateDarwin::_q_LEnotSupported()
void QLowEnergyControllerPrivateDarwin::_q_CBManagerError(QLowEnergyController::Error errorCode)
{
+ qCDebug(QT_BT_DARWIN) << "QLowEnergyController error:" << errorCode << "in state:" << state;
// This function handles errors reported while connecting to a remote device
// and also other errors in general.
setError(errorCode);
- if (state == QLowEnergyController::ConnectingState)
+ if (state == QLowEnergyController::ConnectingState
+ || state == QLowEnergyController::AdvertisingState)
setState(QLowEnergyController::UnconnectedState);
else if (state == QLowEnergyController::DiscoveringState)
setState(QLowEnergyController::ConnectedState);
@@ -1013,6 +1023,9 @@ void QLowEnergyControllerPrivateDarwin::setErrorDescription(QLowEnergyController
case QLowEnergyController::AdvertisingError:
errorString = QLowEnergyController::tr("Error occurred trying to start advertising");
break;
+ case QLowEnergyController::MissingPermissionsError:
+ errorString = QLowEnergyController::tr("Error missing permission");
+ break;
case QLowEnergyController::UnknownError:
default:
errorString = QLowEnergyController::tr("Unknown Error");
diff --git a/src/bluetooth/qlowenergycontrollerbase.cpp b/src/bluetooth/qlowenergycontrollerbase.cpp
index be9555d8..a116887b 100644
--- a/src/bluetooth/qlowenergycontrollerbase.cpp
+++ b/src/bluetooth/qlowenergycontrollerbase.cpp
@@ -109,6 +109,9 @@ void QLowEnergyControllerPrivate::setError(
case QLowEnergyController::AuthorizationError:
errorString = QLowEnergyController::tr("Failed to authorize on the remote device");
break;
+ case QLowEnergyController::MissingPermissionsError:
+ errorString = QLowEnergyController::tr("Missing permissions error");
+ break;
case QLowEnergyController::NoError:
return;
default: