Index: sources/bluetooth/BLEScanner.cpp =================================================================== diff -u -rac16fe2e6ab8e5a7513ebea4959f5a7664238901 -rdca8a36ea292270c4de986c0f0872f1ee1e5f85a --- sources/bluetooth/BLEScanner.cpp (.../BLEScanner.cpp) (revision ac16fe2e6ab8e5a7513ebea4959f5a7664238901) +++ sources/bluetooth/BLEScanner.cpp (.../BLEScanner.cpp) (revision dca8a36ea292270c4de986c0f0872f1ee1e5f85a) @@ -13,6 +13,8 @@ BLEScanner::BLEScanner(QObject *parent) : QObject(parent) { discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); + + startTimer(_timerInterval); } void BLEScanner::onInitConnections() @@ -70,35 +72,18 @@ } /*! - * \brief BLEScanner::startBLETimer - * Starts a new QObject basic timer at the specified interval - * and updates the timer state - * \param interval - (ms) the timer interval - * \return (int) - the timer ID that has been created + * \brief BLEScanner::toggleBLETimer + * Toggles whether the BLE timer is active or not + * \param enabled - (bool) if true, set BLE timer to active + * if false, BLE timer is set to inactive */ -int BLEScanner::startBLETimer(int interval) +void BLEScanner::toggleBLETimer(const bool &active) { - _timerIsActive = true; - int id = startTimer(interval); - LOG_DEBUG(QString("Starting a BLE timer with interval: %1 and id %2").arg(interval).arg(id)); - return id; -} - -/*! - * \brief BLEScanner::killBLETimer - * Kills the timer with the specified id - * and updates the timer state - * \param id - (int) the timer id to kill - */ -void BLEScanner::killBLETimer(int id) -{ - LOG_DEBUG(QString("Killing BLE timer with id: %1").arg(id)); - _timerIsActive = false; - - if (id == -1) + if (_timerIsActive == active) return; - killTimer(id); + LOG_DEBUG(QString("Toggling BLE timer from active=%1 to active=%2").arg(_timerIsActive).arg(active)); + _timerIsActive = active; } /*! @@ -110,46 +95,42 @@ */ void BLEScanner::timerEvent(QTimerEvent *event) { - LOG_DEBUG(QString("Timer event for timer with id %1").arg(event->timerId())); - if (event->timerId() != _timerID) { - LOG_DEBUG("Warning: The timer state is out of sync with the running timer."); - } + Q_UNUSED(event); + if (!_timerIsActive) + return; + if (_retryConnection) { + LOG_DEBUG(QString("BLE Device %1 is Not Connected. Attempting reconnection...").arg(selectedDeviceInfo.address().toString())); onRetryConnectToDevice(); } else{ + LOG_DEBUG(QString("BLE Device %1 is Connected. Checking in with device.").arg(selectedDeviceInfo.address().toString())); onRequestDeviceInformation(); } } /*! - * \brief BLEScanner::setMacAddress - * Sets the BLE mac address to pair with - * \param mac - (QString) The mac address to pair to (e.g. "EC:21:E5:F4:BC:C9") - */ -void BLEScanner::onSetMacAddress(const QString &mac) -{ - macAddress = mac; -} - -/*! * \brief BLEScanner::doSelectDevice * \param addr - The mac address of the device to connect to */ -void BLEScanner::doSelectDevice(const QString &addr) +void BLEScanner::doSelectDevice(const QString &addr, const QString &name) { - if (addr != macAddress) - onSetMacAddress(addr); + if (selectedDeviceInfo.address().toString() == addr) + return; - discoveryAgent->stop(); - emit didFinishScan(); + didDisconnectFromDevice(selectedDeviceInfo); - for (const QBluetoothDeviceInfo &deviceInfo : devices) { - if (deviceInfo.address().toString() == addr) { - selectedDeviceInfo = deviceInfo; - onConnectToDevice(deviceInfo); - return; - } + if (discoveryAgent->isActive()) + { + LOG_DEBUG(QString("Stopping Scan to connect to device addr %1 and name %2").arg(addr).arg(name)); + discoveryAgent->stop(); + LOG_DEBUG(QString("Discovery agent is active? ").arg(discoveryAgent->isActive())); + emit didFinishScan(); } + + selectedDeviceInfo = QBluetoothDeviceInfo(QBluetoothAddress(addr), name, + QBluetoothDeviceInfo::HealthBloodPressureMonitor); + onConnectToDevice(selectedDeviceInfo); + } /*! @@ -159,9 +140,14 @@ */ void BLEScanner::doReselectDevice(const QBluetoothDeviceInfo &deviceInfo) { + if (!deviceInfo.isValid()) + { + LOG_DEBUG(QString("Cannot reselect device with addr %1 and name %2 as it is not valid.") + .arg(deviceInfo.address().toString()).arg(deviceInfo.name())); + return; + } LOG_DEBUG(QString("Reselecting device %1").arg(deviceInfo.address().toString())); selectedDeviceInfo = deviceInfo; - macAddress = deviceInfo.address().toString(); onConnectToDevice(deviceInfo); updateBLECuffCheckinType(true); } @@ -200,8 +186,8 @@ void BLEScanner::onScanFinished() { emit didFinishScan(); - if (!macAddress.isEmpty()) - _timerID = startBLETimer(_timerInterval); + if (selectedDeviceInfo.isValid()) + toggleBLETimer(true); } /*! @@ -210,7 +196,7 @@ */ void BLEScanner::doScanForDevices() { - killBLETimer(_timerID); + toggleBLETimer(false); discoveryAgent->start(); } @@ -427,19 +413,19 @@ measurement.measurement_status = d[17]; - qDebug() << "flags: " << measurement.flags; - qDebug() << "systolic: " << measurement.systolic; - qDebug() << "diastolic: " << measurement.diastolic; - qDebug() << "mean arterial pressure: " << measurement.mean_arterial_pressure_value; - qDebug() << "year: " << measurement.year; - qDebug() << "month: " << measurement.month; - qDebug() << "day: " << measurement.day; - qDebug() << "hour: " << measurement.hour; - qDebug() << "minute: " << measurement.minute; - qDebug() << "second: " << measurement.second; - qDebug() << "pulse_rate: " << measurement.pulse_rate; - qDebug() << "user_id: " << measurement.user_id; - qDebug() << "measurement_status: " << measurement.measurement_status; + LOG_DEBUG(QString("flags: %1").arg(measurement.flags )); + LOG_DEBUG(QString("systolic: %1").arg(measurement.systolic )); + LOG_DEBUG(QString("diastolic: %1").arg(measurement.diastolic )); + LOG_DEBUG(QString("mean arterial pressure: %1").arg(measurement.mean_arterial_pressure_value)); + LOG_DEBUG(QString("year: %1").arg(measurement.year )); + LOG_DEBUG(QString("month: %1").arg(measurement.month )); + LOG_DEBUG(QString("day: %1").arg(measurement.day )); + LOG_DEBUG(QString("hour: %1").arg(measurement.hour )); + LOG_DEBUG(QString("minute: %1").arg(measurement.minute )); + LOG_DEBUG(QString("second: %1").arg(measurement.second )); + LOG_DEBUG(QString("pulse_rate: %1").arg(measurement.pulse_rate )); + LOG_DEBUG(QString("user_id: %1").arg(measurement.user_id )); + LOG_DEBUG(QString("measurement_status: %1").arg(measurement.measurement_status )); emit didReceiveBPMeasurement(measurement); } @@ -554,9 +540,8 @@ */ void BLEScanner::updateBLECuffCheckinType(bool retryConnection) { - killBLETimer(_timerID); _retryConnection = retryConnection; - _timerID = startBLETimer(_timerInterval); + toggleBLETimer(true); } /*! @@ -567,13 +552,9 @@ void BLEScanner::onDeviceConnected() { LOG_DEBUG("Device Connected"); - lowEnergyController->discoverServices(); - updateBLECuffCheckinType(false); - - if (!_timerIsActive) - startBLETimer(_timerInterval); + toggleBLETimer(true); } /*! @@ -597,6 +578,8 @@ { LOG_DEBUG(QString("Connecting to (%1, %2)").arg(deviceInfo.name()).arg(deviceInfo.address().toString())); + updateBLECuffCheckinType(true); + lowEnergyController = QLowEnergyController::createCentral(deviceInfo); // low energy controller @@ -625,7 +608,6 @@ */ void BLEScanner::onRetryConnectToDevice() { -// LOG_DEBUG(QString("Retrying to connect to BLE device with address %1").arg(selectedDeviceInfo.address().toString())); lowEnergyController->connectToDevice(); }