* @brief Tries to find a discovered BLE device matching a previously saved identifier. */
| 1385 | * @brief Tries to find a discovered BLE device matching a previously saved identifier. |
| 1386 | */ |
| 1387 | bool IO::Drivers::BluetoothLE::selectByIdentifier(const QJsonObject& id) |
| 1388 | { |
| 1389 | if (id.isEmpty()) |
| 1390 | return false; |
| 1391 | |
| 1392 | const auto savedAddr = id.value(QStringLiteral("address")).toString(); |
| 1393 | const auto savedName = id.value(QStringLiteral("name")).toString(); |
| 1394 | |
| 1395 | int bestScore = 0; |
| 1396 | int bestIndex = -1; |
| 1397 | |
| 1398 | for (int i = 0; i < s_devices.count(); ++i) { |
| 1399 | const auto& device = s_devices.at(i); |
| 1400 | int score = 0; |
| 1401 | |
| 1402 | if (!savedAddr.isEmpty() && device.address().toString() == savedAddr) |
| 1403 | score += 100; |
| 1404 | |
| 1405 | if (!savedName.isEmpty() && device.name() == savedName) |
| 1406 | score += 10; |
| 1407 | |
| 1408 | if (score > bestScore) { |
| 1409 | bestScore = score; |
| 1410 | bestIndex = i; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | if (bestIndex >= 0) { |
| 1415 | selectDevice(bestIndex + 1); |
| 1416 | m_pendingIdentifier = {}; |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | m_pendingIdentifier = id; |
| 1421 | if (!s_discoveryAgent || !s_discoveryAgent->isActive()) |
| 1422 | startDiscovery(); |
| 1423 | |
| 1424 | return false; |
| 1425 | } |
| 1426 | |
| 1427 | //-------------------------------------------------------------------------------------------------- |
| 1428 | // Driver property model |