summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth/heartlistener/heartrate.cpp
blob: 1a4e3d4753f5b44d5961dcc974c5a1bc0bb57d5e (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/***************************************************************************
**
** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "heartrate.h"

#include <qbluetoothaddress.h>
#include <qbluetoothservicediscoveryagent.h>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothlocaldevice.h>
#include <qlowenergyserviceinfo.h>
#include <qlowenergycharacteristicinfo.h>
#include <qbluetoothuuid.h>
#include <QTimer>

HeartRate::HeartRate():
    m_currentDevice(QBluetoothDeviceInfo()), foundHeartRateService(false), foundHeartRateCharacteristic(false), m_HRMeasurement(0), m_max(0), m_min(0), calories(0), m_leInfo(0), timer(0)
{
    m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent();
    m_serviceDiscoveryAgent = new QBluetoothServiceDiscoveryAgent(QBluetoothAddress());

    connect(m_deviceDiscoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
            this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
    connect(m_deviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
            this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
    connect(m_deviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));

    connect(m_serviceDiscoveryAgent, SIGNAL(serviceDiscovered(const QLowEnergyServiceInfo&)),
            this, SLOT(addLowEnergyService(const QLowEnergyServiceInfo&)));
    connect(m_serviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(serviceScanDone()));
    connect(m_serviceDiscoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)),
            this, SLOT(serviceScanError(QBluetoothServiceDiscoveryAgent::Error)));
}

HeartRate::~HeartRate()
{
    delete m_deviceDiscoveryAgent;
    delete m_serviceDiscoveryAgent;
    delete m_leInfo;
    delete timer;
    qDeleteAll(m_devices);
    m_devices.clear();
}

void HeartRate::deviceSearch()
{
    m_devices.clear();
    m_deviceDiscoveryAgent->start();
    setMessage("Scanning for devices...");
}

void HeartRate::addDevice(const QBluetoothDeviceInfo &device)
{
    if (device.coreConfiguration() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
        QBluetoothLocalDevice localDevice;
        QBluetoothLocalDevice::Pairing pairingStatus = localDevice.pairingStatus(device.address());
        if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired )
            qWarning() << "Discovered LE Device name: " << device.name() << " Address: " << device.address().toString() << " Paired";
        else
            qWarning() << "Discovered LE Device name: " << device.name() << " Address: " << device.address().toString() << " not Paired";
        DeviceInfo *dev = new DeviceInfo(device);
        m_devices.append(dev);
        setMessage("Low Energy device found. Scanning for more...");
    }
}

void HeartRate::scanFinished()
{
    if (m_devices.size() == 0)
        setMessage("No Low Energy devices found");
    Q_EMIT nameChanged();
}

void HeartRate::setMessage(QString message)
{
    m_info = message;
    Q_EMIT messageChanged();
}

QString HeartRate::message() const
{
    return m_info;
}

QVariant HeartRate::name()
{
    return QVariant::fromValue(m_devices);
}

void HeartRate::connectToService(const QString &address)
{
    bool deviceHere = false;
    for (int i = 0; i<m_devices.size(); i++) {
        if (((DeviceInfo*)m_devices.at(i))->getAddress() == address ) {
            m_currentDevice.setDevice(((DeviceInfo*)m_devices.at(i))->getDevice());
            setMessage("Device selected.");
            deviceHere = true;
        }
    }
    // This in case we are running demo mode
    if (!deviceHere)
        startDemo();
    else {
        QBluetoothDeviceInfo device = m_currentDevice.getDevice();
        //! [Connect signals]
        m_serviceDiscoveryAgent->setRemoteAddress(device.address());
        m_serviceDiscoveryAgent->start();
        if (!m_leInfo) {
            m_leInfo = new QLowEnergyController();
            connect(m_leInfo, SIGNAL(connected(QLowEnergyServiceInfo)), this, SLOT(serviceConnected(QLowEnergyServiceInfo)));
            connect(m_leInfo, SIGNAL(disconnected(QLowEnergyServiceInfo)), this, SLOT(serviceDisconnected(QLowEnergyServiceInfo)));
            connect(m_leInfo, SIGNAL(error(QLowEnergyServiceInfo)), this, SLOT(errorReceived(QLowEnergyServiceInfo)));
            connect(m_leInfo, SIGNAL(error(QLowEnergyCharacteristicInfo)), this, SLOT(errorReceivedCharacteristic(QLowEnergyCharacteristicInfo)));
            connect(m_leInfo, SIGNAL(valueChanged(QLowEnergyCharacteristicInfo)), this, SLOT(receiveMeasurement(QLowEnergyCharacteristicInfo)));
        }
        //! [Connect signals]
    }
}

void HeartRate::addLowEnergyService(const QLowEnergyServiceInfo &gatt)
{
    if (gatt.serviceUuid() == QBluetoothUuid::HeartRate) {
        setMessage("Heart Rate service discovered. Waiting for service scan to be done...");
        m_heartRateService = QLowEnergyServiceInfo(gatt);
        foundHeartRateService = true;
    }
}

//! [Connecting to service]
void HeartRate::serviceScanDone()
{
    //If HeartBelt is not connected (installed on the body) this message will stay.
    setMessage("Connecting to service... Be patient...");
    //It is not advisable to connect to BLE device right after scanning.
    if (foundHeartRateService)
        QTimer::singleShot(3000, this, SLOT(startConnection()));
    else
        setMessage("Heart Rate Service not found. Make sure your device is paired.");
}

void HeartRate::startConnection()
{
    // HeartRate belt that this application was using had a random device address. This is only needed
    // for Linux platform.
    // m_heartRateService.setRandomAddress();
    m_leInfo->connectToService(m_heartRateService);
}

void HeartRate::serviceConnected(const QLowEnergyServiceInfo &leService)
{
    setMessage("Connected to service. Waiting for updates");
    if (leService.serviceUuid() == QBluetoothUuid::HeartRate) {
        for ( int i = 0; i<leService.characteristics().size(); i++) {
            if (leService.characteristics().at(i).uuid() == QBluetoothUuid::HeartRateMeasurement) {
                m_start = QDateTime::currentDateTime();
                m_heartRateCharacteristic = QLowEnergyCharacteristicInfo(leService.characteristics().at(i));
                m_leInfo->enableNotifications(m_heartRateCharacteristic);
                foundHeartRateCharacteristic = true;
            }
        }
    }
}
//! [Connecting to service]

void HeartRate::receiveMeasurement(const QLowEnergyCharacteristicInfo &characteristic)
{
    m_heartRateCharacteristic = QLowEnergyCharacteristicInfo(characteristic);
    //! [Reading value]
    QString val;
    qint16 energy_expended = 0;
    int flags = 0;
    int index = 0;
    val[0] = m_heartRateCharacteristic.value().at(index++);
    // Each Heart Belt has its own settings and fetarues, besides heart rate measurement
    // By checking the flags we can determine whether it has energy feature. We will go through the array
    // of the characters in the characteristic value.
    if (val.toUInt(0, 16) > 3)
        flags = val.toUInt(0, 16);
    else {
        val[1] = m_heartRateCharacteristic.value().at(index++);
        flags = val.toUInt(0, 16);
    }
    QString value;
    value[0] = m_heartRateCharacteristic.value().at(index++);
    value[1] = m_heartRateCharacteristic.value().at(index++);
    m_HRMeasurement = value.toUInt(0, 16);
    //! [Reading value]
    m_measurements.append(m_HRMeasurement);
    // The following flags are used to determine what kind of value is being sent from the device.
    // FLAGS field bit mask values
    qint8 heartRateValueFormat = 1;// 0
    qint8 energyExpendedFeature = 8;// 3
    bool hrDataFormat = false;
    bool energyExpendedFeatureSupported = false;

    hrDataFormat = ((flags & heartRateValueFormat) != heartRateValueFormat); // 0 means 8 bit, 1 means 16 bit
    energyExpendedFeatureSupported = ((flags & energyExpendedFeature) == 0);
    if (!hrDataFormat)
        qWarning() << "XXXX 16 bit heart rate measurement data encountered!";
    if (energyExpendedFeatureSupported) {
        QString energy;
        int counter1 = 0;
        for (int i = index; i < (m_heartRateCharacteristic.value().size() - 1); i++) {
            if (counter1 > 3)
                break;
            energy[i] = m_heartRateCharacteristic.value().at(i);
            counter1 ++;
        }
        energy_expended = energy.toUInt(0, 16);
        index = index + 2;
    }
    qWarning() << "Used energy: " << energy_expended;
    Q_EMIT hrChanged();
}

int HeartRate::hR() const
{
    return m_HRMeasurement;
}

//! [Error handling]
void HeartRate::errorReceived(const QLowEnergyServiceInfo &leService)
{
    qWarning() << "Error: " << leService.serviceUuid() << m_leInfo->errorString();
    setMessage(QStringLiteral("Error: ") + m_leInfo->errorString());
}

void HeartRate::errorReceivedCharacteristic(const QLowEnergyCharacteristicInfo &leCharacteristic)
{
    qWarning() << "Error: " << leCharacteristic.uuid() << m_leInfo->errorString();
    setMessage(QStringLiteral("Error: ") + m_leInfo->errorString());
}
//! [Error handling]

void HeartRate::disconnectService()
{
    if (foundHeartRateCharacteristic) {
        m_stop = QDateTime::currentDateTime();
        //! [Disconnecting from service]
        m_leInfo->disableNotifications(m_heartRateCharacteristic);
        m_leInfo->disconnectFromService();
        //! [Disconnecting from service]
    }
    else if (m_devices.size() == 0) {
        m_stop = QDateTime::currentDateTime();
        timer->stop();
        timer = 0;
    }
    foundHeartRateCharacteristic = false;
    foundHeartRateService = false;
}

void HeartRate::obtainResults()
{
    Q_EMIT timeChanged();
    Q_EMIT averageChanged();
    Q_EMIT caloriesChanged();
}

int HeartRate::time()
{
    return m_start.secsTo(m_stop);
}

int HeartRate::maxHR() const
{
    return m_max;
}

int HeartRate::minHR() const
{
    return m_min;
}

float HeartRate::average()
{
    if (m_measurements.size() == 0)
        return 0;
    else {
        m_max = 0;
        m_min = 1000;
        int sum = 0;
        for (int i=0; i< m_measurements.size(); i++) {
            sum += (int) m_measurements.value(i);
            if (((int)m_measurements.value(i)) > m_max)
                m_max = (int)m_measurements.value(i);
            if (((int)m_measurements.value(i)) < m_min)
                m_min = (int)m_measurements.value(i);
        }
        return sum/m_measurements.size();
    }
}

int HeartRate::measurements(int index)
{
    if (index> m_measurements.size())
        return 0;
    else
        return (int)m_measurements.value(index);
}

int HeartRate::measurementsSize()
{
    return m_measurements.size();
}

QString HeartRate::deviceAddress()
{
    return m_currentDevice.getDevice().address().toString();
}

float HeartRate::caloriesCalculation()
{
    calories = ((-55.0969 + (0.6309 * average()) + (0.1988 * 94) + (0.2017 * 24)) / 4.184) * 60 * time()/3600 ;
    return calories;
}

void HeartRate::serviceDisconnected(const QLowEnergyServiceInfo &service)
{
    setMessage("Heart Rate service disconnected");
    qWarning() << "Service disconnected: " << service.serviceUuid();
}

int HeartRate::numDevices() const
{
    return m_devices.size();
}

void HeartRate::startDemo()
{
    m_start = QDateTime::currentDateTime();
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(receiveDemo()));
    timer->start(1000);
    setMessage("This is Demo mode");
}

void HeartRate::receiveDemo()
{
    m_HRMeasurement = 60;
    m_measurements.append(m_HRMeasurement);
    Q_EMIT hrChanged();
}

void HeartRate::serviceScanError(QBluetoothServiceDiscoveryAgent::Error error)
{
    if (error == QBluetoothServiceDiscoveryAgent::PoweredOffError)
        setMessage("The Bluetooth adaptor is powered off, power it on before doing discovery.");
    else if (error == QBluetoothServiceDiscoveryAgent::InputOutputError)
        setMessage("Writing or reading from the device resulted in an error.");
    else
        setMessage("An unknown error has occurred.");
}

void HeartRate::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
    if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
        setMessage("The Bluetooth adaptor is powered off, power it on before doing discovery.");
    else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
        setMessage("Writing or reading from the device resulted in an error.");
    else
        setMessage("An unknown error has occurred.");
}