Index: denali.pro.user =================================================================== diff -u -rc5f61421dd55f0b26a884b5739867f370a52e023 -rdca8a36ea292270c4de986c0f0872f1ee1e5f85a --- denali.pro.user (.../denali.pro.user) (revision c5f61421dd55f0b26a884b5739867f370a52e023) +++ denali.pro.user (.../denali.pro.user) (revision dca8a36ea292270c4de986c0f0872f1ee1e5f85a) @@ -1,14 +1,14 @@ - + EnvironmentId {67370740-e20f-4fc6-be45-6652e866a8bf} ProjectExplorer.Project.ActiveTarget - 1 + 0 ProjectExplorer.Project.EditorSettings @@ -89,7 +89,7 @@ Desktop Qt 5.12.5 GCC 64bit Desktop Qt 5.12.5 GCC 64bit qt.qt5.5125.gcc_64_kit - 0 + 1 0 0 @@ -1185,7 +1185,7 @@ false true - /home/denali/Projects/tmp/build/denali-Desktop_Qt_5_12_5_GCC_64bit-Debug + /home/denali/Projects/tmp/build/denali-Desktop_Qt_5_12_5_GCC_64bit-Release 1 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(); } Index: sources/bluetooth/BLEScanner.h =================================================================== diff -u -rac16fe2e6ab8e5a7513ebea4959f5a7664238901 -rdca8a36ea292270c4de986c0f0872f1ee1e5f85a --- sources/bluetooth/BLEScanner.h (.../BLEScanner.h) (revision ac16fe2e6ab8e5a7513ebea4959f5a7664238901) +++ sources/bluetooth/BLEScanner.h (.../BLEScanner.h) (revision dca8a36ea292270c4de986c0f0872f1ee1e5f85a) @@ -39,7 +39,6 @@ QThread *_thread = nullptr; bool _init = false; const int _timerInterval = 1000; // SRSUI 6 - int _timerID = -1; bool _timerIsActive = false; bool _retryConnection = false; @@ -69,7 +68,6 @@ QList devices; QBluetoothDeviceInfo selectedDeviceInfo; - QString macAddress = ""; QBluetoothDeviceDiscoveryAgent *discoveryAgent; QLowEnergyController *lowEnergyController; @@ -95,15 +93,13 @@ void onInitConnections(); void onQuitThread(); void onConnectToDevice(const QBluetoothDeviceInfo& deviceInfo); - void onSetMacAddress(const QString &mac); void updateBLECuffCheckinType(bool retryConnection); void makeServiceConnections(QLowEnergyService *service); QLowEnergyService* setupService(const QBluetoothUuid &uuid); void timerEvent(QTimerEvent* event); // timer - int startBLETimer(int interval); - void killBLETimer(int id); + void toggleBLETimer(const bool &active); signals: void didReceiveBPMeasurement(BLEMeasurementData); @@ -118,7 +114,7 @@ public slots: bool doInit(); - void doSelectDevice(const QString &addr); + void doSelectDevice(const QString &addr, const QString &name); void doScanForDevices(); void doParseMeasurement(const QByteArray &byteArray); void doRequestBPMeasurement(); Index: sources/gui/qml/pages/SettingsBluetooth.qml =================================================================== diff -u -r5220c64dd3a0fc9c322a68884e73503bb7893d12 -rdca8a36ea292270c4de986c0f0872f1ee1e5f85a --- sources/gui/qml/pages/SettingsBluetooth.qml (.../SettingsBluetooth.qml) (revision 5220c64dd3a0fc9c322a68884e73503bb7893d12) +++ sources/gui/qml/pages/SettingsBluetooth.qml (.../SettingsBluetooth.qml) (revision dca8a36ea292270c4de986c0f0872f1ee1e5f85a) @@ -148,7 +148,7 @@ } onClicked: { - vBluetooth.doSelectDevice(modelData.address) + vBluetooth.doSelectDevice(modelData.address, modelData.name) } } } @@ -214,7 +214,7 @@ } onClicked: { - vBluetooth.doSelectDevice(modelData.address) + vBluetooth.doSelectDevice(modelData.address, modelData.name) } } } Index: sources/view/VBluetooth.cpp =================================================================== diff -u -rc5f61421dd55f0b26a884b5739867f370a52e023 -rdca8a36ea292270c4de986c0f0872f1ee1e5f85a --- sources/view/VBluetooth.cpp (.../VBluetooth.cpp) (revision c5f61421dd55f0b26a884b5739867f370a52e023) +++ sources/view/VBluetooth.cpp (.../VBluetooth.cpp) (revision dca8a36ea292270c4de986c0f0872f1ee1e5f85a) @@ -36,8 +36,8 @@ this, SLOT(onDisconnectedFromDevice(QBluetoothDeviceInfo))); // outgoing - connect(this, SIGNAL(didSelectDevice(const QString)), - &_BLEScanner, SLOT(doSelectDevice(const QString))); + connect(this, SIGNAL(didSelectDevice(const QString, const QString)), + &_BLEScanner, SLOT(doSelectDevice(const QString, const QString))); connect(this, SIGNAL(didRequestScanForDevices()), &_BLEScanner, SLOT(doScanForDevices())); @@ -131,18 +131,20 @@ * \brief VBluetooth::onSelectedDevice * Emits a signal that the device was selected * \param addr - the selected BLE mac address + * \param name - the selected BLE device name */ -void VBluetooth::doSelectDevice(const QString &addr) +void VBluetooth::doSelectDevice(const QString &addr, const QString &name) { - for (QObject *device : _unpairedDevices) { - VBluetoothDeviceInfo *d = static_cast(device); - if (addr == d->getAddress()) - { - delete _lastSelectedDevice; - _lastSelectedDevice = new VBluetoothDeviceInfo(addr, d->getName()); - emit didSelectDevice(addr); - } + if (_lastSelectedDevice != nullptr && _lastSelectedDevice->getAddress() != addr) + { + QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(QBluetoothAddress(addr), name, + QBluetoothDeviceInfo::HealthBloodPressureMonitor); + onDisconnectedFromDevice(deviceInfo); } + + delete _lastSelectedDevice; + _lastSelectedDevice = new VBluetoothDeviceInfo(addr, name); + emit didSelectDevice(addr, name); } /*! Index: sources/view/VBluetooth.h =================================================================== diff -u -rc5f61421dd55f0b26a884b5739867f370a52e023 -rdca8a36ea292270c4de986c0f0872f1ee1e5f85a --- sources/view/VBluetooth.h (.../VBluetooth.h) (revision c5f61421dd55f0b26a884b5739867f370a52e023) +++ sources/view/VBluetooth.h (.../VBluetooth.h) (revision dca8a36ea292270c4de986c0f0872f1ee1e5f85a) @@ -40,12 +40,12 @@ void doScanForDevices(); QVariant doGetDevices(); QVariant doGetPairedDevices(); - void doSelectDevice(const QString &addr); + void doSelectDevice(const QString &addr, const QString &name); void doSaveMyDevices(const QString &path = Bluetooth_Saved_Devices_Path_JSON); signals: void didDevicesChanged(); - void didSelectDevice(const QString &addr); + void didSelectDevice(const QString &addr, const QString &name); void didScanFinished(); void didStatusChanged(); void didRequestScanForDevices();