diff options
author | Alex Blasche <alexander.blasche@qt.io> | 2019-02-08 09:51:19 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2019-02-08 13:22:47 +0000 |
commit | 1606ccb76ba72990df652fbd7f01d709ae20b63c (patch) | |
tree | 42f3ed40bcf8bd001501d89eb31f5fde99feb3b4 | |
parent | 862c766f0896314bf850c5573bb39788f211c1e1 (diff) |
Fix unit test failure on Android
It fixes the following failure
FAIL! : tst_QBluetoothServiceDiscoveryAgent::tst_invalidBtAddress() Compared values are not the same
Actual (discoveryAgent->error()) : InvalidBluetoothAdapterError
Expected (QBluetoothServiceDiscoveryAgent::NoError): NoError
This behavior was caused by the fact that the InvalidBluetoothAdapterError
was already triggered in the QBluetoothServiceDiscoveryAgent ctor whereas
convention for this class states that the error is set when
QBluetoothServiceDiscoveryAgent::start() is called.
The fix detects whether the requested local adapter address matches the
existing local adapter address. If there is no match Invalid adapter error
is thrown.
Task-number: QTBUG-73571
Change-Id: I3216e1609820a66893768b33f0fc695fbad6966a
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
-rw-r--r-- | src/bluetooth/qbluetoothservicediscoveryagent_android.cpp | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp index ce2911d3..ddc53421 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp @@ -55,21 +55,33 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID) QBluetoothServiceDiscoveryAgentPrivate::QBluetoothServiceDiscoveryAgentPrivate( - QBluetoothServiceDiscoveryAgent *qp, const QBluetoothAddress &/*deviceAdapter*/) + QBluetoothServiceDiscoveryAgent *qp, const QBluetoothAddress &deviceAdapter) : error(QBluetoothServiceDiscoveryAgent::NoError), + m_deviceAdapterAddress(deviceAdapter), state(Inactive), mode(QBluetoothServiceDiscoveryAgent::MinimalDiscovery), singleDevice(false), q_ptr(qp) { - QList<QBluetoothHostInfo> devices = QBluetoothLocalDevice::allDevices(); - Q_ASSERT(devices.count() <= 1); //Android only supports one device at the moment - - if (devices.isEmpty()) { - error = QBluetoothServiceDiscoveryAgent::InvalidBluetoothAdapterError; - errorString = QBluetoothServiceDiscoveryAgent::tr("Invalid Bluetooth adapter address"); - return; + // If a specific adapter address is requested we need to check it matches + // the current local adapter. If it does not match we emit + // InvalidBluetoothAdapterError when calling start() + + bool createAdapter = true; + if (!deviceAdapter.isNull()) { + const QList<QBluetoothHostInfo> devices = QBluetoothLocalDevice::allDevices(); + if (devices.isEmpty()) { + createAdapter = false; + } else { + auto match = [deviceAdapter](const QBluetoothHostInfo& info) { + return info.address() == deviceAdapter; + }; + + auto result = std::find_if(devices.begin(), devices.end(), match); + if (result == devices.end()) + createAdapter = false; + } } if (QtAndroidPrivate::androidSdkVersion() < 15) @@ -84,7 +96,8 @@ QBluetoothServiceDiscoveryAgentPrivate::QBluetoothServiceDiscoveryAgentPrivate( The logic below must change once there is more than one adapter. */ - btAdapter = QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter", + if (createAdapter) + btAdapter = QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter", "getDefaultAdapter", "()Landroid/bluetooth/BluetoothAdapter;"); if (!btAdapter.isValid()) @@ -110,8 +123,15 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr Q_Q(QBluetoothServiceDiscoveryAgent); if (!btAdapter.isValid()) { - error = QBluetoothServiceDiscoveryAgent::UnknownError; - errorString = QBluetoothServiceDiscoveryAgent::tr("Platform does not support Bluetooth"); + if (m_deviceAdapterAddress.isNull()) { + error = QBluetoothServiceDiscoveryAgent::UnknownError; + errorString = QBluetoothServiceDiscoveryAgent::tr("Platform does not support Bluetooth"); + } else { + // specific adapter was requested which does not match the locally + // existing adapter + error = QBluetoothServiceDiscoveryAgent::InvalidBluetoothAdapterError; + errorString = QBluetoothServiceDiscoveryAgent::tr("Invalid Bluetooth adapter address"); + } //abort any outstanding discoveries discoveredDevices.clear(); |