From c84773de3af76832d15806647d8529c2e5b75257 Mon Sep 17 00:00:00 2001 From: Tomasz Sterna Date: Fri, 11 Jul 2014 23:41:27 +0200 Subject: Fixed reconnect on disconnection Reconnection timeut will now raise gradually, to be 1000ms * reconnect attempt. This should help preserve power in the phone, when no Pebble is around. You can speed-up this process by reconnecting manually from manager app or its cover. Also force channel 1 for RFCOMM, as discovery does not work anymore after disconnection. --- daemon/watchconnector.cpp | 19 ++++++++++++++----- daemon/watchconnector.h | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'daemon') diff --git a/daemon/watchconnector.cpp b/daemon/watchconnector.cpp index d999229..d0fcdad 100644 --- a/daemon/watchconnector.cpp +++ b/daemon/watchconnector.cpp @@ -4,11 +4,14 @@ using namespace watch; -static int __reconnect_timeout = 5000; //ms +static int __reconnect_timeout = 1000; //ms WatchConnector::WatchConnector(QObject *parent) : QObject(parent), socket(nullptr), is_connected(false) -{} +{ + reconnectTimer.setSingleShot(true); + connect(&reconnectTimer, SIGNAL(timeout()), SLOT(reconnect())); +} WatchConnector::~WatchConnector() { @@ -43,11 +46,14 @@ void WatchConnector::disconnect() logger()->debug() << __FUNCTION__; socket->close(); socket->deleteLater(); + reconnectTimer.stop(); + logger()->debug() << "Stopped reconnect timer"; } void WatchConnector::handleWatch(const QString &name, const QString &address) { logger()->debug() << "handleWatch " << name << " " << address; + reconnectTimer.stop(); if (socket != nullptr && socket->isOpen()) { socket->close(); socket->deleteLater(); @@ -67,7 +73,7 @@ void WatchConnector::handleWatch(const QString &name, const QString &address) connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(onError(QBluetoothSocket::SocketError))); // FIXME: Assuming port 1 (with Pebble) - socket->connectToService(QBluetoothAddress(address), QBluetoothUuid(QBluetoothUuid::SerialPort)); + socket->connectToService(QBluetoothAddress(address), 1); } QString WatchConnector::decodeEndpoint(unsigned int val) @@ -158,6 +164,8 @@ void WatchConnector::onConnected() logger()->debug() << "Connected!"; bool was_connected = is_connected; is_connected = true; + reconnectTimer.stop(); + reconnectTimer.setInterval(0); if (not was_connected) emit connectedChanged(); } @@ -175,8 +183,9 @@ void WatchConnector::onDisconnected() socket->deleteLater(); - // Try to connect again after a timeout - QTimer::singleShot(__reconnect_timeout, this, SLOT(reconnect())); + reconnectTimer.setInterval(reconnectTimer.interval() + __reconnect_timeout); + reconnectTimer.start(); + logger()->debug() << "Will reconnect in " << reconnectTimer.interval() << " ms"; } void WatchConnector::onError(QBluetoothSocket::SocketError error) { diff --git a/daemon/watchconnector.h b/daemon/watchconnector.h index 4015870..36d0936 100644 --- a/daemon/watchconnector.h +++ b/daemon/watchconnector.h @@ -130,6 +130,7 @@ private: QPointer socket; bool is_connected; + QTimer reconnectTimer; QString _last_name; QString _last_address; }; -- cgit v1.2.3