Index: sources/bluetooth/BLEScanner.cpp =================================================================== diff -u -rfdb48ba3fba8e95027ebf573325c8f25db74c070 -rac16fe2e6ab8e5a7513ebea4959f5a7664238901 --- sources/bluetooth/BLEScanner.cpp (.../BLEScanner.cpp) (revision fdb48ba3fba8e95027ebf573325c8f25db74c070) +++ sources/bluetooth/BLEScanner.cpp (.../BLEScanner.cpp) (revision ac16fe2e6ab8e5a7513ebea4959f5a7664238901) @@ -13,9 +13,6 @@ BLEScanner::BLEScanner(QObject *parent) : QObject(parent) { discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); - - timer = new QTimer(this); - timer->setInterval(1000); // SRSUI 6 } void BLEScanner::onInitConnections() @@ -27,7 +24,6 @@ connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(onDiscoveryAgentError(QBluetoothDeviceDiscoveryAgent::Error))); connect(discoveryAgent, SIGNAL(finished()), this, SLOT(onScanFinished())); - } /*! @@ -73,8 +69,59 @@ moveToThread(qApp->thread()); } +/*! + * \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 + */ +int BLEScanner::startBLETimer(int interval) +{ + _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) + return; + + killTimer(id); +} + +/*! + * \brief BLEScanner::timerEvent + * Called at the end of the timer interval when the + * timer is active + * \param event - (QTimerEvent*) holds the timer event + * information (e.g. timer id) + */ +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."); + } + if (_retryConnection) { + onRetryConnectToDevice(); + } else{ + 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") @@ -114,6 +161,7 @@ { LOG_DEBUG(QString("Reselecting device %1").arg(deviceInfo.address().toString())); selectedDeviceInfo = deviceInfo; + macAddress = deviceInfo.address().toString(); onConnectToDevice(deviceInfo); updateBLECuffCheckinType(true); } @@ -152,6 +200,8 @@ void BLEScanner::onScanFinished() { emit didFinishScan(); + if (!macAddress.isEmpty()) + _timerID = startBLETimer(_timerInterval); } /*! @@ -160,7 +210,7 @@ */ void BLEScanner::doScanForDevices() { - timer->stop(); + killBLETimer(_timerID); discoveryAgent->start(); } @@ -504,19 +554,9 @@ */ void BLEScanner::updateBLECuffCheckinType(bool retryConnection) { - timer->stop(); - if (retryConnection) { - disconnect(timer, SIGNAL(timeout()), - this, SLOT(onRequestDeviceInformation())); - connect(timer, SIGNAL(timeout()), - this, SLOT(onRetryConnectToDevice())); - } else{ - disconnect(timer, SIGNAL(timeout()), - this, SLOT(onRetryConnectToDevice())); - connect(timer, SIGNAL(timeout()), - this, SLOT(onRequestDeviceInformation())); - } - timer->start(); + killBLETimer(_timerID); + _retryConnection = retryConnection; + _timerID = startBLETimer(_timerInterval); } /*! @@ -532,8 +572,8 @@ updateBLECuffCheckinType(false); - if (!timer->isActive()) - timer->start(); + if (!_timerIsActive) + startBLETimer(_timerInterval); } /*!