Skip to content

Commit

Permalink
add autoreconnection, first attempts after 10sec, then 60sec and at l…
Browse files Browse the repository at this point in the history
…east every hour
  • Loading branch information
StefWe authored and FlorentRevest committed Oct 23, 2019
1 parent 2d00e5f commit 2363e13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions watchconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ WatchConnection::WatchConnection(QObject *parent) : QObject(parent)
m_services.append(m_weatherService);

m_isConnected = false;

m_reconnectTimer.setSingleShot(true);
QObject::connect(&m_reconnectTimer, &QTimer::timeout, this, &WatchConnection::reconnect);
}

void WatchConnection::setDevice(Watch *device)
Expand All @@ -46,6 +49,24 @@ void WatchConnection::setDevice(Watch *device)
emit currentWatchChanged();
}

m_connectionAttempts = 0;
scheduleReconnect();
}

void WatchConnection::scheduleReconnect()
{
if (m_connectionAttempts == 0)
reconnect();
else if (m_connectionAttempts < 25)
m_reconnectTimer.start(1000 * 10);
else if (m_connectionAttempts < 35)
m_reconnectTimer.start(1000 * 60);
else
m_reconnectTimer.start(1000 * 60 * 15);
}

void WatchConnection::reconnect()
{
if (m_control) {
m_control->disconnectFromDevice();
delete m_control;
Expand All @@ -63,6 +84,7 @@ void WatchConnection::setDevice(Watch *device)

m_control->connectToDevice();
}
m_connectionAttempts++;
}

void WatchConnection::connectionError(QLowEnergyController::Error err)
Expand All @@ -75,12 +97,15 @@ void WatchConnection::deviceConnected()
m_isConnected = true;
emit connected();
m_control->discoverServices();
m_connectionAttempts = 1;
}

void WatchConnection::deviceDisconnected()
{
m_isConnected = false;
emit disconnected();
if (!m_reconnectTimer.isActive())
scheduleReconnect();
}

bool WatchConnection::isConnected()
Expand Down
5 changes: 5 additions & 0 deletions watchconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define WATCHCONNECTION_H

#include <QObject>
#include <QTimer>

#include <QLowEnergyController>
#include <QLowEnergyService>
Expand Down Expand Up @@ -77,9 +78,13 @@ private slots:
WeatherService *m_weatherService;

bool m_isConnected;
int m_connectionAttempts = 0;
QTimer m_reconnectTimer;

void serviceDiscovered(const QBluetoothUuid &);
void serviceScanDone();
void scheduleReconnect();
void reconnect();

void serviceStateChanged(QLowEnergyService::ServiceState s);
void updateHeartRateValue(const QLowEnergyCharacteristic &c, const QByteArray &value);
Expand Down

0 comments on commit 2363e13

Please sign in to comment.