Fix frequent reconnects on iOS

Tries to guess the used bearer type on iOS and only reconnect
if that one changes.
This commit is contained in:
Michael Zanetti 2023-01-25 19:37:02 +01:00
parent 20287d6ba9
commit 71b1885004
2 changed files with 20 additions and 2 deletions

View File

@ -57,8 +57,11 @@ NymeaConnection::NymeaConnection(QObject *parent) : QObject(parent)
#ifdef Q_OS_IOS
connect(m_networkReachabilityMonitor, &NetworkReachabilityMonitor::availableBearerTypesChanged, this, [this](){
if (m_currentTransport) {
m_currentTransport->disconnect();
if (m_currentTransport) {
if (!m_networkReachabilityMonitor->availableBearerTypes().testFlag(m_usedBearerType)) {
qCInfo(dcNymeaConnection()) << "Used bearer type" << m_usedBearerType << "isn't available any more. Reconnecting.";
m_currentTransport->disconnect();
}
}
});
#endif
@ -292,6 +295,17 @@ void NymeaConnection::onConnected()
if (!m_currentTransport) {
m_currentTransport = newTransport;
qCInfo(dcNymeaConnection()) << "Connected to" << m_currentHost->name() << "via" << m_currentTransport->url() << m_currentTransport->isEncrypted();
#ifdef Q_OS_IOS
// We can't know for sure which transport we're actually using, but let's assume the OS picked from the available ones in the order LAN, WiFi, MobileData
if (m_networkReachabilityMonitor->availableBearerTypes().testFlag(NymeaConnection::BearerTypeEthernet)) {
m_usedBearerType = NymeaConnection::BearerTypeEthernet;
} else if (m_networkReachabilityMonitor->availableBearerTypes().testFlag(NymeaConnection::BearerTypeWiFi)) {
m_usedBearerType = NymeaConnection::BearerTypeWiFi;
} else {
m_usedBearerType = NymeaConnection::BearerTypeMobileData;
}
qCDebug(dcNymeaConnection()) << "iOS: Assuming used bearer type:" << m_usedBearerType;
#endif
emit currentConnectionChanged();
emit connectedChanged(true);
return;

View File

@ -141,6 +141,10 @@ private:
Connection *m_preferredConnection = nullptr;
QTimer m_reconnectTimer;
#ifdef Q_OS_IOS
NymeaConnection::BearerType m_usedBearerType;
#endif
};
#endif // NYMEACONNECTION_H