summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-06-14 12:27:34 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2017-06-20 07:52:31 +0000
commite26edb020084554e8b567d3bc15ce638394cf7fc (patch)
tree67d44ed43758cef5211d4da75f2bf6a9da3d6205
parent6c4bd9551755dac834b107202df5d95b346f0b79 (diff)
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 <alexander.blasche@qt.io>
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp60
1 files 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