summaryrefslogtreecommitdiff
path: root/libsowatchbt/bluetoothwatch.h
diff options
context:
space:
mode:
Diffstat (limited to 'libsowatchbt/bluetoothwatch.h')
-rw-r--r--libsowatchbt/bluetoothwatch.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/libsowatchbt/bluetoothwatch.h b/libsowatchbt/bluetoothwatch.h
new file mode 100644
index 0000000..882e7c0
--- /dev/null
+++ b/libsowatchbt/bluetoothwatch.h
@@ -0,0 +1,74 @@
+#ifndef SOWATCHBT_BLUETOOTHWATCH_H
+#define SOWATCHBT_BLUETOOTHWATCH_H
+
+#include <QtConnectivity/QBluetoothAddress>
+#include <QtConnectivity/QBluetoothSocket>
+#include <QtConnectivity/QBluetoothLocalDevice>
+#include <QtSystemInfo/QSystemAlignedTimer>
+#include <sowatch.h>
+#include "sowatchbt_global.h"
+
+namespace sowatch
+{
+
+using QTM_PREPEND_NAMESPACE(QBluetoothSocket);
+using QTM_PREPEND_NAMESPACE(QBluetoothAddress);
+using QTM_PREPEND_NAMESPACE(QSystemAlignedTimer);
+using QTM_PREPEND_NAMESPACE(QBluetoothLocalDevice);
+
+class SOWATCHBT_EXPORT BluetoothWatch : public Watch
+{
+ Q_OBJECT
+
+public:
+ BluetoothWatch(const QBluetoothAddress& address, QObject *parent = 0);
+ ~BluetoothWatch();
+
+ bool isConnected() const;
+
+protected:
+ /** Start the initial connection attempt, reset failed connection timers. */
+ void scheduleConnect();
+ /** Schedule an new connection attempt, consider the current one failed. */
+ void scheduleRetryConnect();
+ /** Cancel any pending connection attempts. */
+ void unscheduleConnect();
+
+ /** Attempt a connection to the watch right now. */
+ virtual void connectToWatch();
+
+ /** To be overriden; should configure a newly connected watch. */
+ virtual void setupBluetoothWatch() = 0;
+ /** To be overriden; handle a sudden watch disconnection. */
+ virtual void desetupBluetoothWatch() = 0;
+
+private slots:
+ void handleConnectTimer();
+ void handleLocalDevModeChanged(QBluetoothLocalDevice::HostMode state);
+ void handleSocketConnected();
+ void handleSocketDisconnected();
+ void handleSocketError(QBluetoothSocket::SocketError error);
+ void handleSocketState(QBluetoothSocket::SocketState error);
+
+protected:
+ /** Local BT device used. */
+ QBluetoothLocalDevice *_localDev;
+ /** BT address of the watch we are trying to connect to. */
+ QBluetoothAddress _address;
+ /** Socket to the watch. */
+ QBluetoothSocket *_socket;
+ /** Whether we have succesfully connected to the watch or not. */
+ bool _connected;
+
+private:
+ // Timers to retry the connection when the watch is not found.
+ static const int connectRetryTimesSize = 6;
+ static const int connectRetryTimes[connectRetryTimesSize];
+ short _connectRetries;
+ QTimer *_connectTimer;
+ QSystemAlignedTimer *_connectAlignedTimer;
+};
+
+}
+
+#endif // SOWATCHBT_BLUETOOTHWATCH_H