summaryrefslogtreecommitdiff
path: root/libsowatchbt/bluetoothwatch.h
blob: 882e7c0125697a450f9c484ac73a7da0f0c87f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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