1/***************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef DEVICEHANDLER_H
52#define DEVICEHANDLER_H
53
54#include "bluetoothbaseclass.h"
55
56#include <QDateTime>
57#include <QTimer>
58#include <QVector>
59
60#include <QLowEnergyController>
61#include <QLowEnergyService>
62
63class DeviceInfo;
64
65class DeviceHandler : public BluetoothBaseClass
66{
67 Q_OBJECT
68
69 Q_PROPERTY(bool measuring READ measuring NOTIFY measuringChanged)
70 Q_PROPERTY(bool alive READ alive NOTIFY aliveChanged)
71 Q_PROPERTY(int hr READ hr NOTIFY statsChanged)
72 Q_PROPERTY(int maxHR READ maxHR NOTIFY statsChanged)
73 Q_PROPERTY(int minHR READ minHR NOTIFY statsChanged)
74 Q_PROPERTY(float average READ average NOTIFY statsChanged)
75 Q_PROPERTY(int time READ time NOTIFY statsChanged)
76 Q_PROPERTY(float calories READ calories NOTIFY statsChanged)
77 Q_PROPERTY(AddressType addressType READ addressType WRITE setAddressType)
78
79public:
80 enum class AddressType {
81 PublicAddress,
82 RandomAddress
83 };
84 Q_ENUM(AddressType)
85
86 DeviceHandler(QObject *parent = nullptr);
87
88 void setDevice(DeviceInfo *device);
89 void setAddressType(AddressType type);
90 AddressType addressType() const;
91
92 bool measuring() const;
93 bool alive() const;
94
95 // Statistics
96 int hr() const;
97 int time() const;
98 float average() const;
99 int maxHR() const;
100 int minHR() const;
101 float calories() const;
102
103signals:
104 void measuringChanged();
105 void aliveChanged();
106 void statsChanged();
107
108public slots:
109 void startMeasurement();
110 void stopMeasurement();
111 void disconnectService();
112
113private:
114 //QLowEnergyController
115 void serviceDiscovered(const QBluetoothUuid &);
116 void serviceScanDone();
117
118 //QLowEnergyService
119 void serviceStateChanged(QLowEnergyService::ServiceState s);
120 void updateHeartRateValue(const QLowEnergyCharacteristic &c,
121 const QByteArray &value);
122 void confirmedDescriptorWrite(const QLowEnergyDescriptor &d,
123 const QByteArray &value);
124
125#ifdef SIMULATOR
126 void updateDemoHR();
127#endif
128private:
129 void addMeasurement(int value);
130
131 QLowEnergyController *m_control = nullptr;
132 QLowEnergyService *m_service = nullptr;
133 QLowEnergyDescriptor m_notificationDesc;
134 DeviceInfo *m_currentDevice = nullptr;
135
136 bool m_foundHeartRateService;
137 bool m_measuring;
138 int m_currentValue, m_min, m_max, m_sum;
139 float m_avg, m_calories;
140
141 // Statistics
142 QDateTime m_start;
143 QDateTime m_stop;
144
145 QVector<int> m_measurements;
146 QLowEnergyController::RemoteAddressType m_addressType = QLowEnergyController::PublicAddress;
147
148#ifdef SIMULATOR
149 QTimer m_demoTimer;
150#endif
151};
152
153#endif // DEVICEHANDLER_H
154

source code of qtconnectivity/examples/bluetooth/heartrate-game/devicehandler.h