summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJouni Roivas <jroivas@iki.fi>2014-03-02 11:19:19 +0200
committerJouni Roivas <jroivas@iki.fi>2014-03-02 11:19:19 +0200
commitc89b4e0cce457c6b96f44e23b66d6ab0254f0eac (patch)
treeb80c4c1e4d490656aacb20c2410bde68836bc817
parentdb2e550807b363cb8f31fa976da361bcf90e8f2c (diff)
Try to reconnect on error
-rw-r--r--src/watchconnector.cpp18
-rw-r--r--src/watchconnector.h8
-rw-r--r--src/waterwatch.cpp2
3 files changed, 26 insertions, 2 deletions
diff --git a/src/watchconnector.cpp b/src/watchconnector.cpp
index fb518ed..ab27b71 100644
--- a/src/watchconnector.cpp
+++ b/src/watchconnector.cpp
@@ -1,6 +1,10 @@
#include "watchconnector.h"
#include <QDateTime>
+using namespace watch;
+
+static int __reconnect_timeout = 1000;
+
WatchConnector::WatchConnector(QObject *parent) :
QObject(parent)
{
@@ -13,7 +17,7 @@ WatchConnector::~WatchConnector()
void WatchConnector::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
- //FIXME: Configurable
+ //FIXME TODO: Configurable
if (device.name().startsWith("Pebble")) {
qDebug() << "Found Pebble:" << device.name() << '(' << device.address().toString() << ')';
handleWatch(device);
@@ -25,11 +29,20 @@ void WatchConnector::deviceDiscovered(const QBluetoothDeviceInfo &device)
void WatchConnector::deviceConnect(const QString name, const QString address)
{
if (name.startsWith("Pebble")) {
+ _last_name = name;
+ _last_address = address;
QBluetoothDeviceInfo device(QBluetoothAddress(address), name, 0);
deviceDiscovered(device);
}
}
+void WatchConnector::reconnect()
+{
+ if (_last_name != "" && _last_address != "") {
+ deviceConnect(_last_name, _last_address);
+ }
+}
+
void WatchConnector::handleWatch(const QBluetoothDeviceInfo &device)
{
qDebug() << "handleWatch" << device.name();
@@ -153,6 +166,9 @@ void WatchConnector::disconnected()
socket = nullptr;
emit connectedChanged();
emit nameChanged();
+
+ // Try to connect again after a timeout
+ QTimer::singleShot(__reconnect_timeout, this, SLOT(reconnect()));
}
void WatchConnector::sendData(const QByteArray &data)
diff --git a/src/watchconnector.h b/src/watchconnector.h
index 198f703..56a0f5c 100644
--- a/src/watchconnector.h
+++ b/src/watchconnector.h
@@ -39,6 +39,9 @@
using namespace QtBluetooth;
+namespace watch
+{
+
class WatchConnector : public QObject
{
Q_OBJECT
@@ -113,13 +116,18 @@ public slots:
void readSocket();
void connected();
void disconnected();
+ void reconnect();
private:
void decodeMsg(QByteArray data);
QBluetoothSocket *socket;
bool is_connected;
+ QString _last_name;
+ QString _last_address;
};
void registerWatchConnector();
+}
+
#endif // WATCHCONNECTOR_H
diff --git a/src/waterwatch.cpp b/src/waterwatch.cpp
index 02da4b8..795a310 100644
--- a/src/waterwatch.cpp
+++ b/src/waterwatch.cpp
@@ -37,7 +37,7 @@
int main(int argc, char *argv[])
{
// Registert WatchController object on QML side
- registerWatchConnector();
+ watch::registerWatchConnector();
return SailfishApp::main(argc, argv);
}