Add reconnect attempt timer after BT40Device unexpected disconnect (#4812)

Add reconnect attempt timer every 5s after BT40Device unexpected disconnect
This commit is contained in:
Anže Blagus
2026-01-23 17:59:52 +01:00
committed by GitHub
parent c85da479ca
commit b0d16d163e
2 changed files with 40 additions and 1 deletions

View File

@@ -83,6 +83,11 @@ BT40Device::BT40Device(QObject *parent, QBluetoothDeviceInfo devinfo) : parent(p
wheelSize = 2100;
has_power = 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()
@@ -105,6 +110,8 @@ BT40Device::disconnectDevice()
{
qDebug() << "Disconnecting from device" << m_currentDevice.name() << " " << m_currentDevice.deviceUuid();
connected = false;
reconnectTimer->stop();
reconnectAttempts = 0;
m_control->disconnectFromDevice();
}
@@ -112,6 +119,14 @@ void
BT40Device::deviceConnected()
{
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();
}
@@ -168,7 +183,10 @@ BT40Device::deviceDisconnected()
// Try to reconnect if the connection was lost inadvertently
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
BT40Device::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
{

View File

@@ -23,6 +23,7 @@
#include <QLowEnergyController>
#include <QLowEnergyService>
#include <QQueue>
#include <QTimer>
#include "CalibrationData.h"
#include "Ftms.h"
@@ -74,6 +75,7 @@ private slots:
void confirmedCharacteristicWrite(const QLowEnergyCharacteristic &c,
const QByteArray &value);
void serviceError(QLowEnergyService::ServiceError e);
void attemptReconnect();
signals:
void setNotification(QString msg, int timeout);
@@ -112,6 +114,8 @@ private:
FtmsDeviceInformation ftmsDeviceInfo;
bool connected;
QTimer *reconnectTimer;
int reconnectAttempts;
void getCadence(QDataStream& ds);
void getWheelRpm(QDataStream& ds);
void setLoadErg(double);