From e26edb020084554e8b567d3bc15ce638394cf7fc Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 14 Jun 2017 12:27:34 +0300 Subject: Improve processing of discovered device ... where now we use std::find_if algorithm instead of indexed container enumeration. Change-Id: I081dcc92e7b6c1d7a160e62d32ba2cc1182cf43a Reviewed-by: Alex Blasche --- .../qbluetoothdevicediscoveryagent_win.cpp | 60 ++++++++++------------ 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp index 0e6e1343..9f05ffa1 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp @@ -524,40 +524,34 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevice( { Q_Q(QBluetoothDeviceDiscoveryAgent); - for (int i = 0; i < discoveredDevices.size(); i++) { - QBluetoothDeviceInfo mergedDevice = discoveredDevices[i]; - - if (mergedDevice.address() == foundDevice.address()) { - if (mergedDevice == foundDevice - || mergedDevice.coreConfigurations() == foundDevice.coreConfigurations()) { - qCDebug(QT_BT_WINDOWS) << "Duplicate: " << foundDevice.address(); - return; - } - - // We assume that if the existing device it is low energy, it means that - // the found device should be as classic, because it is impossible to get - // same low energy device. - if (mergedDevice.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) - mergedDevice = foundDevice; - - // We assume that it is impossible to have multiple devices with same core - // configurations, which have one address. This possible only in case a device - // provided both low energy and classic features at the same time. - mergedDevice.setCoreConfigurations(QBluetoothDeviceInfo::BaseRateAndLowEnergyCoreConfiguration); - mergedDevice.setCached(foundDevice.isCached()); - - discoveredDevices.replace(i, mergedDevice); - Q_Q(QBluetoothDeviceDiscoveryAgent); - qCDebug(QT_BT_WINDOWS) << "Updated: " << mergedDevice.address(); - - emit q->deviceDiscovered(mergedDevice); - return; - } + auto equalAddress = [foundDevice](const QBluetoothDeviceInfo &targetDevice) { + return foundDevice.address() == targetDevice.address(); }; + auto end = discoveredDevices.end(); + auto deviceIt = std::find_if(discoveredDevices.begin(), end, equalAddress); + if (deviceIt == end) { + qCDebug(QT_BT_WINDOWS) << "Emit: " << foundDevice.address(); + discoveredDevices.append(foundDevice); + emit q->deviceDiscovered(foundDevice); + } else if (*deviceIt == foundDevice + || deviceIt->coreConfigurations() == foundDevice.coreConfigurations()) { + qCDebug(QT_BT_WINDOWS) << "Duplicate: " << foundDevice.address(); + } else { + // We assume that if the existing device it is low energy, it means that + // the found device should be as classic, because it is impossible to get + // same low energy device. + if (deviceIt->coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) + *deviceIt = foundDevice; + + // We assume that it is impossible to have multiple devices with same core + // configurations, which have one address. This possible only in case a device + // provided both low energy and classic features at the same time. + deviceIt->setCoreConfigurations(QBluetoothDeviceInfo::BaseRateAndLowEnergyCoreConfiguration); + deviceIt->setCached(foundDevice.isCached()); + + Q_Q(QBluetoothDeviceDiscoveryAgent); + qCDebug(QT_BT_WINDOWS) << "Updated: " << deviceIt->address(); + emit q->deviceDiscovered(*deviceIt); } - - qCDebug(QT_BT_WINDOWS) << "Emit: " << foundDevice.address(); - discoveredDevices.append(foundDevice); - emit q->deviceDiscovered(foundDevice); } QT_END_NAMESPACE -- cgit v1.2.3