mirror of
https://github.com/GoldenCheetah/GoldenCheetah.git
synced 2026-04-15 05:32:21 +00:00
Add reconnect attempt timer after BT40Device unexpected disconnect (#4812)
Add reconnect attempt timer every 5s after BT40Device unexpected disconnect
This commit is contained in:
@@ -83,6 +83,11 @@ BT40Device::BT40Device(QObject *parent, QBluetoothDeviceInfo devinfo) : parent(p
|
|||||||
wheelSize = 2100;
|
wheelSize = 2100;
|
||||||
has_power = false;
|
has_power = false;
|
||||||
has_controllable_service = false;
|
has_controllable_service = false;
|
||||||
|
|
||||||
|
reconnectTimer = new QTimer(this);
|
||||||
|
reconnectTimer->setInterval(5000); // 5 seconds
|
||||||
|
connect(reconnectTimer, SIGNAL(timeout()), this, SLOT(attemptReconnect()));
|
||||||
|
reconnectAttempts = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BT40Device::~BT40Device()
|
BT40Device::~BT40Device()
|
||||||
@@ -105,6 +110,8 @@ BT40Device::disconnectDevice()
|
|||||||
{
|
{
|
||||||
qDebug() << "Disconnecting from device" << m_currentDevice.name() << " " << m_currentDevice.deviceUuid();
|
qDebug() << "Disconnecting from device" << m_currentDevice.name() << " " << m_currentDevice.deviceUuid();
|
||||||
connected = false;
|
connected = false;
|
||||||
|
reconnectTimer->stop();
|
||||||
|
reconnectAttempts = 0;
|
||||||
m_control->disconnectFromDevice();
|
m_control->disconnectFromDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,6 +119,14 @@ void
|
|||||||
BT40Device::deviceConnected()
|
BT40Device::deviceConnected()
|
||||||
{
|
{
|
||||||
qDebug() << "Connected to device" << m_currentDevice.name() << " " << m_currentDevice.deviceUuid();
|
qDebug() << "Connected to device" << m_currentDevice.name() << " " << m_currentDevice.deviceUuid();
|
||||||
|
|
||||||
|
// Stop reconnection timer if it's running
|
||||||
|
if (reconnectTimer->isActive()) {
|
||||||
|
reconnectTimer->stop();
|
||||||
|
reconnectAttempts = 0;
|
||||||
|
emit setNotification(tr("Reconnected to %1").arg(m_currentDevice.name()), 3);
|
||||||
|
}
|
||||||
|
|
||||||
m_control->discoverServices();
|
m_control->discoverServices();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +183,10 @@ BT40Device::deviceDisconnected()
|
|||||||
|
|
||||||
// Try to reconnect if the connection was lost inadvertently
|
// Try to reconnect if the connection was lost inadvertently
|
||||||
if (connected) {
|
if (connected) {
|
||||||
this->connectDevice();
|
reconnectAttempts = 0;
|
||||||
|
emit setNotification(tr("Lost connection to %1, attempting to reconnect...").arg(m_currentDevice.name()), 3);
|
||||||
|
reconnectTimer->start();
|
||||||
|
attemptReconnect(); // Try immediately first
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -892,6 +910,23 @@ BT40Device::serviceError(QLowEnergyService::ServiceError e)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
BT40Device::attemptReconnect()
|
||||||
|
{
|
||||||
|
if (!connected) {
|
||||||
|
// User manually disconnected, stop trying
|
||||||
|
reconnectTimer->stop();
|
||||||
|
reconnectAttempts = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnectAttempts++;
|
||||||
|
qDebug() << "Reconnection attempt" << reconnectAttempts << "for device" << m_currentDevice.name();
|
||||||
|
emit setNotification(tr("Reconnecting to %1 (attempt %2)...").arg(m_currentDevice.name()).arg(reconnectAttempts), 3);
|
||||||
|
|
||||||
|
this->connectDevice();
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
BT40Device::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
|
BT40Device::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include <QLowEnergyController>
|
#include <QLowEnergyController>
|
||||||
#include <QLowEnergyService>
|
#include <QLowEnergyService>
|
||||||
#include <QQueue>
|
#include <QQueue>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
#include "CalibrationData.h"
|
#include "CalibrationData.h"
|
||||||
#include "Ftms.h"
|
#include "Ftms.h"
|
||||||
@@ -74,6 +75,7 @@ private slots:
|
|||||||
void confirmedCharacteristicWrite(const QLowEnergyCharacteristic &c,
|
void confirmedCharacteristicWrite(const QLowEnergyCharacteristic &c,
|
||||||
const QByteArray &value);
|
const QByteArray &value);
|
||||||
void serviceError(QLowEnergyService::ServiceError e);
|
void serviceError(QLowEnergyService::ServiceError e);
|
||||||
|
void attemptReconnect();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void setNotification(QString msg, int timeout);
|
void setNotification(QString msg, int timeout);
|
||||||
@@ -112,6 +114,8 @@ private:
|
|||||||
FtmsDeviceInformation ftmsDeviceInfo;
|
FtmsDeviceInformation ftmsDeviceInfo;
|
||||||
|
|
||||||
bool connected;
|
bool connected;
|
||||||
|
QTimer *reconnectTimer;
|
||||||
|
int reconnectAttempts;
|
||||||
void getCadence(QDataStream& ds);
|
void getCadence(QDataStream& ds);
|
||||||
void getWheelRpm(QDataStream& ds);
|
void getWheelRpm(QDataStream& ds);
|
||||||
void setLoadErg(double);
|
void setLoadErg(double);
|
||||||
|
|||||||
Reference in New Issue
Block a user