From 14db1fabef1ba2eb1a889c968e3b038f7978d6bd Mon Sep 17 00:00:00 2001 From: Juha Vuolle Date: Tue, 6 Sep 2022 13:25:47 +0300 Subject: Clear previous bluetooth device/service discovery errors on restart An example of the problem is when discovery is first tried while bluetooth is OFF, and one gets the corresponding error. Then the bluetooth is switched ON, and the discovery is restarted successfully. However in this case the error() still remains as "power OFF" error instead of "no error", which is misleading. With device discovery agent the clearing is done at each individual backend, rather than at the common public class start(). This is due to the pending cancel/start logic, which we probably shouldn't interfere with by clearing errors amidst pending operation. The Darwin backend already cleared the error in its start(), and with this commit the intent is to add similar clearing to the Android/Bluez/Win. Pick-to: 6.2 6.3 6.4 Fixes: QTBUG-104473 Change-Id: I713590a26eb2b8d4ee7873f3fe84e63e504523df Reviewed-by: Ivan Solovev --- src/bluetooth/qbluetoothdevicediscoveryagent.cpp | 4 ++++ .../qbluetoothdevicediscoveryagent_android.cpp | 2 ++ .../qbluetoothdevicediscoveryagent_bluez.cpp | 2 ++ .../qbluetoothdevicediscoveryagent_winrt.cpp | 2 ++ src/bluetooth/qbluetoothservicediscoveryagent.cpp | 8 +++++++- tests/bttestui/btlocaldevice.cpp | 21 +++++++++++++++++++++ tests/bttestui/btlocaldevice.h | 1 + tests/bttestui/main.qml | 5 +++++ 8 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent.cpp index 5dae7f2f..da88f4aa 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent.cpp @@ -349,6 +349,8 @@ bool QBluetoothDeviceDiscoveryAgent::isActive() const /*! Returns the last error. + + Any possible previous errors are cleared upon restarting the discovery. */ QBluetoothDeviceDiscoveryAgent::Error QBluetoothDeviceDiscoveryAgent::error() const { @@ -359,6 +361,8 @@ QBluetoothDeviceDiscoveryAgent::Error QBluetoothDeviceDiscoveryAgent::error() co /*! Returns a human-readable description of the last error. + + \sa error(), errorOccurred() */ QString QBluetoothDeviceDiscoveryAgent::errorString() const { diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp index cd6327f5..b52313e0 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp @@ -205,6 +205,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent QObject::connect(receiver, SIGNAL(finished()), this, SLOT(processSdpDiscoveryFinished())); } + lastError = QBluetoothDeviceDiscoveryAgent::NoError; + errorString.clear(); discoveredDevices.clear(); // by arbitrary definition we run classic search first diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp index b93dde22..24ae2511 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp @@ -76,6 +76,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent return; } + lastError = QBluetoothDeviceDiscoveryAgent::NoError; + errorString.clear(); discoveredDevices.clear(); devicesProperties.clear(); diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp index 37659d4b..8940a2c0 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp @@ -765,6 +765,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent worker = std::make_shared(methods, lowEnergySearchTimeout); + lastError = QBluetoothDeviceDiscoveryAgent::NoError; + errorString.clear(); discoveredDevices.clear(); connect(worker.get(), &QWinRTBluetoothDeviceDiscoveryWorker::deviceFound, this, &QBluetoothDeviceDiscoveryAgentPrivate::registerDevice); diff --git a/src/bluetooth/qbluetoothservicediscoveryagent.cpp b/src/bluetooth/qbluetoothservicediscoveryagent.cpp index 8456c341..2f5d9dd6 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent.cpp @@ -308,6 +308,9 @@ void QBluetoothServiceDiscoveryAgent::start(DiscoveryMode mode) d->foundHostAdapterPath.clear(); #endif d->setDiscoveryMode(mode); + // Clear any possible previous errors + d->error = QBluetoothServiceDiscoveryAgent::NoError; + d->errorString.clear(); if (d->deviceAddress.isNull()) { d->startDeviceDiscovery(); } else { @@ -378,9 +381,10 @@ bool QBluetoothServiceDiscoveryAgent::isActive() const discovered by a scan, errors during service discovery on individual devices are not saved and no signals are emitted. In this case, errors are fairly normal as some devices may not respond to discovery or - may no longer be in range. Such errors are surpressed. If no services + may no longer be in range. Such errors are suppressed. If no services are returned, it can be assumed no services could be discovered. + Any possible previous errors are cleared upon restarting the discovery. */ QBluetoothServiceDiscoveryAgent::Error QBluetoothServiceDiscoveryAgent::error() const { @@ -392,6 +396,8 @@ QBluetoothServiceDiscoveryAgent::Error QBluetoothServiceDiscoveryAgent::error() /*! Returns a human-readable description of the last error that occurred during the service discovery. + + \sa error(), errorOccurred() */ QString QBluetoothServiceDiscoveryAgent::errorString() const { diff --git a/tests/bttestui/btlocaldevice.cpp b/tests/bttestui/btlocaldevice.cpp index e6aa8a8e..bf8f492e 100644 --- a/tests/bttestui/btlocaldevice.cpp +++ b/tests/bttestui/btlocaldevice.cpp @@ -794,6 +794,27 @@ void BtLocalDevice::dumpServerInformation() } } +template +void printError(const QLatin1StringView name, T* ptr) +{ + if (!ptr) + return; + qDebug() << name << "error:" << ptr->error(); +} + +void BtLocalDevice::dumpErrors() +{ + qDebug() << "###### Errors"; + printError("Device agent"_L1, deviceAgent); + printError("Service agent"_L1, serviceAgent); + printError("LE Central"_L1, leCentralController.get()); + printError("LE Central Service"_L1, leCentralService.get()); + printError("LE Peripheral"_L1, lePeripheralController.get()); + printError("LE Peripheral Service"_L1, lePeripheralService.get()); + printError("Socket"_L1, socket); + printError("Server"_L1, server); +} + void BtLocalDevice::dumpInformation() { if (!localDevice) diff --git a/tests/bttestui/btlocaldevice.h b/tests/bttestui/btlocaldevice.h index 97e64ed1..4c093bdb 100644 --- a/tests/bttestui/btlocaldevice.h +++ b/tests/bttestui/btlocaldevice.h @@ -50,6 +50,7 @@ signals: public slots: //QBluetoothLocalDevice void dumpInformation(); + void dumpErrors(); void powerOn(); void reset(); void setHostMode(int newMode); diff --git a/tests/bttestui/main.qml b/tests/bttestui/main.qml index f0510fcf..38823199 100644 --- a/tests/bttestui/main.qml +++ b/tests/bttestui/main.qml @@ -350,6 +350,11 @@ Flickable { buttonText: "Reset" onClicked: device.reset() } + Button { + id: errorButton + buttonText: "Errors" + onClicked: device.dumpErrors() + } } } } -- cgit v1.2.3