* @brief Select BLE device by index */
| 197 | * @brief Select BLE device by index |
| 198 | */ |
| 199 | API::CommandResponse API::Handlers::BluetoothLEHandler::selectDevice(const QString& id, |
| 200 | const QJsonObject& params) |
| 201 | { |
| 202 | if (!params.contains(QStringLiteral("deviceIndex"))) { |
| 203 | return CommandResponse::makeError( |
| 204 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: deviceIndex")); |
| 205 | } |
| 206 | |
| 207 | const int deviceIndex = params.value(QStringLiteral("deviceIndex")).toInt(); |
| 208 | auto* ble = IO::ConnectionManager::instance().bluetoothLE(); |
| 209 | |
| 210 | if (deviceIndex < 0 || deviceIndex >= ble->deviceCount()) { |
| 211 | return CommandResponse::makeError(id, |
| 212 | ErrorCode::InvalidParam, |
| 213 | QStringLiteral("Invalid deviceIndex: %1. Valid range: 0-%2") |
| 214 | .arg(deviceIndex) |
| 215 | .arg(ble->deviceCount() - 1)); |
| 216 | } |
| 217 | |
| 218 | ble->selectDevice(deviceIndex + 1); |
| 219 | |
| 220 | QJsonObject result; |
| 221 | result[QStringLiteral("deviceIndex")] = deviceIndex; |
| 222 | const auto& deviceNames = ble->deviceNames(); |
| 223 | if (deviceIndex + 1 < deviceNames.count()) |
| 224 | result[QStringLiteral("deviceName")] = deviceNames.at(deviceIndex + 1); |
| 225 | |
| 226 | return CommandResponse::makeSuccess(id, result); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @brief Select BLE service by index |
nothing calls this directly
no test coverage detected