Index: sources/bluetooth/BLEScanner.cpp =================================================================== diff -u -r7249125bb71e6fab4139590ee777c64ece9cf3be -r5696b8db4628ee3143d93952c5dbdf511fa985b1 --- sources/bluetooth/BLEScanner.cpp (.../BLEScanner.cpp) (revision 7249125bb71e6fab4139590ee777c64ece9cf3be) +++ sources/bluetooth/BLEScanner.cpp (.../BLEScanner.cpp) (revision 5696b8db4628ee3143d93952c5dbdf511fa985b1) @@ -12,7 +12,9 @@ BLEScanner::BLEScanner(QObject *parent) : QObject(parent) { discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); - macAddress = "EC:21:E5:F4:BC:C9"; // for testing only + + timer = new QTimer(this); + timer->setInterval(1000); // SRSUI 6 } void BLEScanner::onInitConnections() @@ -172,6 +174,9 @@ else if (uuid.toString() == omronBatteryLevelServiceName) { omronBatteryLevelService = service; } + else if (uuid.toString() == omronDeviceInformationServiceName) { + omronDeviceInformationService = service; + } service->discoverDetails(); @@ -180,12 +185,41 @@ foreach (const QLowEnergyCharacteristic &c, service->characteristics()) { qDebug() << "----> Characteristic: " << c.name() << " uuid: " << c.uuid(); + } +} +/*! + * \brief BLEScanner::onRequestDeviceSerialNumber + * Sends a request for the device's serial number + */ +void BLEScanner::onRequestDeviceSerialNumber() +{ + // read device serial number + const QLowEnergyCharacteristic c = omronDeviceInformationService->characteristic( + QBluetoothUuid(QBluetoothUuid::SerialNumberString) + ); + if (!c.isValid()) { + qDebug() << "Cannot read device information."; + disconnect(timer, SIGNAL(timeout()), + this, SLOT(onRequestDeviceInformation())); + connect(timer, SIGNAL(timeout()), + this, SLOT(onRetryConnectToDevice())); + return; } + omronDeviceInformationService->readCharacteristic(c); +} - +/*! + * \brief BLEScanner::onRequestDeviceInformation + * Requests BP monitor device information + */ +void BLEScanner::onRequestDeviceInformation() +{ + qDebug() << __FUNCTION__; +// onRequestDeviceInformation(); } + /*! * \brief BLEScanner::requestBPMeasurement * Requests BP Measurement data. @@ -343,13 +377,15 @@ void BLEScanner::onConfirmedDescriptorWrite(const QLowEnergyDescriptor &desc, const QByteArray &byteArray) { qDebug() << "Confirmed descriptor write: " << byteArray; + if (desc.isValid() && desc == desc && byteArray == QByteArray::fromHex("0000")) { //disabled notifications -> assume disconnect qDebug() << "deleting omron blood pressure service"; lowEnergyController->disconnectFromDevice(); delete omronBloodPressureService; omronBloodPressureService = nullptr; } + emit didConnectToDevice(selectedDeviceInfo); } @@ -414,6 +450,14 @@ qDebug() << __FUNCTION__; lowEnergyController->discoverServices(); + + disconnect(timer, SIGNAL(timeout()), + this, SLOT(onRetryConnectToDevice())); + connect(timer, SIGNAL(timeout()), + this, SLOT(onRequestDeviceInformation())); + + if (!timer->isActive()) + timer->start(); } /*! @@ -423,9 +467,33 @@ void BLEScanner::onDeviceDisconnected() { qDebug() << __FUNCTION__; + emit didDisconnectFromDevice(selectedDeviceInfo); } /*! + * \brief BLEScanner::onControllerStateChanged + * Slot called when the low energy controller state changes + * \param state - the new state of the low energy controller + */ +void BLEScanner::onControllerStateChanged(const QLowEnergyController::ControllerState &state) +{ + qDebug() << __FUNCTION__ << state; + switch (state) { + case (QLowEnergyController::ClosingState): + emit didDisconnectFromDevice(selectedDeviceInfo); + break; + case (QLowEnergyController::UnconnectedState): + case (QLowEnergyController::ConnectingState): + case (QLowEnergyController::ConnectedState): + case (QLowEnergyController::DiscoveringState): + case (QLowEnergyController::DiscoveredState): + case (QLowEnergyController::AdvertisingState): + default: + break; + } +} + +/*! * \brief BLEScanner::connectToDevice * Creates the low energy controller object * Configures the low energy controller signals @@ -453,6 +521,15 @@ connect(lowEnergyController, SIGNAL(disconnected()), this, SLOT(onDeviceDisconnected())); + connect(lowEnergyController, SIGNAL(stateChanged(QLowEnergyController::ControllerState)), + this, SLOT(onControllerStateChanged(QLowEnergyController::ControllerState))); + emit didStartConnectingToDevice(); lowEnergyController->connectToDevice(); } + +void BLEScanner::onRetryConnectToDevice() +{ + qDebug() << __FUNCTION__ << "Retrying to connect to device..."; + lowEnergyController->connectToDevice(); +}