/usr/share/qt5/doc/qtbluetooth/qtbluetooth-heartrate-game-devicehandler-cpp.html is in qtconnectivity5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 400 401 402 403 404 405 406 407 408 409 410 411 412 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>devicehandler.cpp Example File | Qt Bluetooth 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.9</td><td ><a href="qtbluetooth-index.html">Qt Bluetooth</a></td><td ><a href="qtbluetooth-heartrate-game-example.html">Bluetooth Low Energy Heart Rate Game</a></td><td >devicehandler.cpp Example File</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">devicehandler.cpp Example File</h1>
<span class="subtitle">heartrate-game/devicehandler.cpp</span>
<!-- $$$heartrate-game/devicehandler.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">
<span class="comment">/***************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, 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 The Qt Company Ltd 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$
**
****************************************************************************/</span>
<span class="preprocessor">#include "heartrate-global.h"</span>
<span class="preprocessor">#include "devicehandler.h"</span>
<span class="preprocessor">#include "deviceinfo.h"</span>
<span class="preprocessor">#include <QtEndian></span>
DeviceHandler<span class="operator">::</span>DeviceHandler(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent) :
BluetoothBaseClass(parent)<span class="operator">,</span>
m_control(<span class="number">0</span>)<span class="operator">,</span>
m_service(<span class="number">0</span>)<span class="operator">,</span>
m_currentDevice(<span class="number">0</span>)<span class="operator">,</span>
m_foundHeartRateService(<span class="keyword">false</span>)<span class="operator">,</span>
m_measuring(<span class="keyword">false</span>)<span class="operator">,</span>
m_currentValue(<span class="number">0</span>)<span class="operator">,</span>
m_min(<span class="number">0</span>)<span class="operator">,</span> m_max(<span class="number">0</span>)<span class="operator">,</span> m_sum(<span class="number">0</span>)<span class="operator">,</span> m_avg(<span class="number">0</span>)<span class="operator">,</span> m_calories(<span class="number">0</span>)
{
<span class="preprocessor">#ifdef SIMULATOR</span>
m_demoTimer<span class="operator">.</span>setSingleShot(<span class="keyword">false</span>);
m_demoTimer<span class="operator">.</span>setInterval(<span class="number">2000</span>);
connect(<span class="operator">&</span>m_demoTimer<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span><span class="operator">::</span>timeout<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DeviceHandler<span class="operator">::</span>updateDemoHR);
m_demoTimer<span class="operator">.</span>start();
updateDemoHR();
<span class="preprocessor">#endif</span>
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>setAddressType(AddressType type)
{
<span class="keyword">switch</span> (type) {
<span class="keyword">case</span> DeviceHandler<span class="operator">::</span>AddressType<span class="operator">::</span>PublicAddress:
m_addressType <span class="operator">=</span> <span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>PublicAddress;
<span class="keyword">break</span>;
<span class="keyword">case</span> DeviceHandler<span class="operator">::</span>AddressType<span class="operator">::</span>RandomAddress:
m_addressType <span class="operator">=</span> <span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>RandomAddress;
<span class="keyword">break</span>;
}
}
DeviceHandler<span class="operator">::</span>AddressType DeviceHandler<span class="operator">::</span>addressType() <span class="keyword">const</span>
{
<span class="keyword">if</span> (m_addressType <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>RandomAddress)
<span class="keyword">return</span> DeviceHandler<span class="operator">::</span>AddressType<span class="operator">::</span>RandomAddress;
<span class="keyword">return</span> DeviceHandler<span class="operator">::</span>AddressType<span class="operator">::</span>PublicAddress;
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>setDevice(DeviceInfo <span class="operator">*</span>device)
{
clearMessages();
m_currentDevice <span class="operator">=</span> device;
<span class="preprocessor">#ifdef SIMULATOR</span>
setInfo(tr(<span class="string">"Demo device connected."</span>));
<span class="keyword">return</span>;
<span class="preprocessor">#endif</span>
<span class="comment">// Disconnect and delete old connection</span>
<span class="keyword">if</span> (m_control) {
m_control<span class="operator">-</span><span class="operator">></span>disconnectFromDevice();
<span class="keyword">delete</span> m_control;
m_control <span class="operator">=</span> <span class="number">0</span>;
}
<span class="comment">// Create new controller and connect it if device available</span>
<span class="keyword">if</span> (m_currentDevice) {
<span class="comment">// Make connections</span>
m_control <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span>(m_currentDevice<span class="operator">-</span><span class="operator">></span>getDevice()<span class="operator">,</span> <span class="keyword">this</span>);
m_control<span class="operator">-</span><span class="operator">></span>setRemoteAddressType(m_addressType);
connect(m_control<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>serviceDiscovered<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DeviceHandler<span class="operator">::</span>serviceDiscovered);
connect(m_control<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>discoveryFinished<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DeviceHandler<span class="operator">::</span>serviceScanDone);
connect(m_control<span class="operator">,</span> <span class="keyword">static_cast</span><span class="operator"><</span><span class="type">void</span> (<span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span><span class="operator">*</span>)(<span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>Error)<span class="operator">></span>(<span class="operator">&</span><span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>error)<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">[</span><span class="keyword">this</span><span class="operator">]</span>(<span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>Error error) {
Q_UNUSED(error);
setError(<span class="string">"Cannot connect to remote device."</span>);
});
connect(m_control<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>connected<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">[</span><span class="keyword">this</span><span class="operator">]</span>() {
setInfo(<span class="string">"Controller connected. Search services..."</span>);
m_control<span class="operator">-</span><span class="operator">></span>discoverServices();
});
connect(m_control<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergycontroller.html">QLowEnergyController</a></span><span class="operator">::</span>disconnected<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">[</span><span class="keyword">this</span><span class="operator">]</span>() {
setError(<span class="string">"LowEnergy controller disconnected"</span>);
});
<span class="comment">// Connect</span>
m_control<span class="operator">-</span><span class="operator">></span>connectToDevice();
}
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>startMeasurement()
{
<span class="keyword">if</span> (alive()) {
m_start <span class="operator">=</span> <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span><span class="operator">::</span>currentDateTime();
m_min <span class="operator">=</span> <span class="number">0</span>;
m_max <span class="operator">=</span> <span class="number">0</span>;
m_avg <span class="operator">=</span> <span class="number">0</span>;
m_sum <span class="operator">=</span> <span class="number">0</span>;
m_calories <span class="operator">=</span> <span class="number">0</span>;
m_measuring <span class="operator">=</span> <span class="keyword">true</span>;
m_measurements<span class="operator">.</span>clear();
<span class="keyword">emit</span> measuringChanged();
}
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>stopMeasurement()
{
m_measuring <span class="operator">=</span> <span class="keyword">false</span>;
<span class="keyword">emit</span> measuringChanged();
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>serviceDiscovered(<span class="keyword">const</span> <span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span> <span class="operator">&</span>gatt)
{
<span class="keyword">if</span> (gatt <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span><span class="operator">::</span>HeartRate)) {
setInfo(<span class="string">"Heart Rate service discovered. Waiting for service scan to be done..."</span>);
m_foundHeartRateService <span class="operator">=</span> <span class="keyword">true</span>;
}
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>serviceScanDone()
{
setInfo(<span class="string">"Service scan done."</span>);
<span class="comment">// Delete old service if available</span>
<span class="keyword">if</span> (m_service) {
<span class="keyword">delete</span> m_service;
m_service <span class="operator">=</span> <span class="number">0</span>;
}
<span class="comment">// If heartRateService found, create new service</span>
<span class="keyword">if</span> (m_foundHeartRateService)
m_service <span class="operator">=</span> m_control<span class="operator">-</span><span class="operator">></span>createServiceObject(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span><span class="operator">::</span>HeartRate)<span class="operator">,</span> <span class="keyword">this</span>);
<span class="keyword">if</span> (m_service) {
connect(m_service<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>stateChanged<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DeviceHandler<span class="operator">::</span>serviceStateChanged);
connect(m_service<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>characteristicChanged<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DeviceHandler<span class="operator">::</span>updateHeartRateValue);
connect(m_service<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>descriptorWritten<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DeviceHandler<span class="operator">::</span>confirmedDescriptorWrite);
m_service<span class="operator">-</span><span class="operator">></span>discoverDetails();
} <span class="keyword">else</span> {
setError(<span class="string">"Heart Rate Service not found."</span>);
}
}
<span class="comment">// Service functions</span>
<span class="type">void</span> DeviceHandler<span class="operator">::</span>serviceStateChanged(<span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>ServiceState s)
{
<span class="keyword">switch</span> (s) {
<span class="keyword">case</span> <span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>DiscoveringServices:
setInfo(tr(<span class="string">"Discovering services..."</span>));
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>ServiceDiscovered:
{
setInfo(tr(<span class="string">"Service discovered."</span>));
<span class="keyword">const</span> <span class="type"><a href="qlowenergycharacteristic.html">QLowEnergyCharacteristic</a></span> hrChar <span class="operator">=</span> m_service<span class="operator">-</span><span class="operator">></span>characteristic(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span><span class="operator">::</span>HeartRateMeasurement));
<span class="keyword">if</span> (<span class="operator">!</span>hrChar<span class="operator">.</span>isValid()) {
setError(<span class="string">"HR Data not found."</span>);
<span class="keyword">break</span>;
}
m_notificationDesc <span class="operator">=</span> hrChar<span class="operator">.</span>descriptor(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span><span class="operator">::</span>ClientCharacteristicConfiguration);
<span class="keyword">if</span> (m_notificationDesc<span class="operator">.</span>isValid())
m_service<span class="operator">-</span><span class="operator">></span>writeDescriptor(m_notificationDesc<span class="operator">,</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">::</span>fromHex(<span class="string">"0100"</span>));
<span class="keyword">break</span>;
}
<span class="keyword">default</span>:
<span class="comment">//nothing for now</span>
<span class="keyword">break</span>;
}
<span class="keyword">emit</span> aliveChanged();
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>updateHeartRateValue(<span class="keyword">const</span> <span class="type"><a href="qlowenergycharacteristic.html">QLowEnergyCharacteristic</a></span> <span class="operator">&</span>c<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&</span>value)
{
<span class="comment">// ignore any other characteristic change -> shouldn't really happen though</span>
<span class="keyword">if</span> (c<span class="operator">.</span>uuid() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span><span class="operator">::</span>HeartRateMeasurement))
<span class="keyword">return</span>;
<span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#quint8-typedef">quint8</a></span> <span class="operator">*</span>data <span class="operator">=</span> <span class="keyword">reinterpret_cast</span><span class="operator"><</span><span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#quint8-typedef">quint8</a></span> <span class="operator">*</span><span class="operator">></span>(value<span class="operator">.</span>constData());
<span class="type"><a href="../qtcore/qtglobal.html#quint8-typedef">quint8</a></span> flags <span class="operator">=</span> data<span class="operator">[</span><span class="number">0</span><span class="operator">]</span>;
<span class="comment">//Heart Rate</span>
<span class="type">int</span> hrvalue <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">if</span> (flags <span class="operator">&</span> <span class="number">0x1</span>) <span class="comment">// HR 16 bit? otherwise 8 bit</span>
hrvalue <span class="operator">=</span> (<span class="type">int</span>)<a href="../qtcore/qtendian.html#qFromLittleEndian">qFromLittleEndian</a><span class="operator"><</span><span class="type"><a href="../qtcore/qtglobal.html#quint16-typedef">quint16</a></span><span class="operator">></span>(data<span class="operator">[</span><span class="number">1</span><span class="operator">]</span>);
<span class="keyword">else</span>
hrvalue <span class="operator">=</span> (<span class="type">int</span>)data<span class="operator">[</span><span class="number">1</span><span class="operator">]</span>;
addMeasurement(hrvalue);
}
<span class="preprocessor">#ifdef SIMULATOR</span>
<span class="type">void</span> DeviceHandler<span class="operator">::</span>updateDemoHR()
{
<span class="type">int</span> randomValue <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">if</span> (m_currentValue <span class="operator"><</span> <span class="number">30</span>) { <span class="comment">// Initial value</span>
randomValue <span class="operator">=</span> <span class="number">55</span> <span class="operator">+</span> qrand()<span class="operator">%</span><span class="number">30</span>;
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="operator">!</span>m_measuring) { <span class="comment">// Value when relax</span>
randomValue <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qBound">qBound</a>(<span class="number">55</span><span class="operator">,</span> m_currentValue <span class="operator">-</span> <span class="number">2</span> <span class="operator">+</span> qrand()<span class="operator">%</span><span class="number">5</span><span class="operator">,</span> <span class="number">75</span>);
} <span class="keyword">else</span> { <span class="comment">// Measuring</span>
randomValue <span class="operator">=</span> m_currentValue <span class="operator">+</span> qrand()<span class="operator">%</span><span class="number">10</span> <span class="operator">-</span> <span class="number">2</span>;
}
addMeasurement(randomValue);
}
<span class="preprocessor">#endif</span>
<span class="type">void</span> DeviceHandler<span class="operator">::</span>confirmedDescriptorWrite(<span class="keyword">const</span> <span class="type"><a href="qlowenergydescriptor.html">QLowEnergyDescriptor</a></span> <span class="operator">&</span>d<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&</span>value)
{
<span class="keyword">if</span> (d<span class="operator">.</span>isValid() <span class="operator">&</span><span class="operator">&</span> d <span class="operator">=</span><span class="operator">=</span> m_notificationDesc <span class="operator">&</span><span class="operator">&</span> value <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">::</span>fromHex(<span class="string">"0000"</span>)) {
<span class="comment">//disabled notifications -> assume disconnect intent</span>
m_control<span class="operator">-</span><span class="operator">></span>disconnectFromDevice();
<span class="keyword">delete</span> m_service;
m_service <span class="operator">=</span> <span class="number">0</span>;
}
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>disconnectService()
{
m_foundHeartRateService <span class="operator">=</span> <span class="keyword">false</span>;
<span class="comment">//disable notifications</span>
<span class="keyword">if</span> (m_notificationDesc<span class="operator">.</span>isValid() <span class="operator">&</span><span class="operator">&</span> m_service
<span class="operator">&</span><span class="operator">&</span> m_notificationDesc<span class="operator">.</span>value() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">::</span>fromHex(<span class="string">"0100"</span>)) {
m_service<span class="operator">-</span><span class="operator">></span>writeDescriptor(m_notificationDesc<span class="operator">,</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">::</span>fromHex(<span class="string">"0000"</span>));
} <span class="keyword">else</span> {
<span class="keyword">if</span> (m_control)
m_control<span class="operator">-</span><span class="operator">></span>disconnectFromDevice();
<span class="keyword">delete</span> m_service;
m_service <span class="operator">=</span> <span class="number">0</span>;
}
}
bool DeviceHandler<span class="operator">::</span>measuring() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_measuring;
}
bool DeviceHandler<span class="operator">::</span>alive() <span class="keyword">const</span>
{
<span class="preprocessor">#ifdef SIMULATOR</span>
<span class="keyword">return</span> <span class="keyword">true</span>;
<span class="preprocessor">#endif</span>
<span class="keyword">if</span> (m_service)
<span class="keyword">return</span> m_service<span class="operator">-</span><span class="operator">></span>state() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qlowenergyservice.html">QLowEnergyService</a></span><span class="operator">::</span>ServiceDiscovered;
<span class="keyword">return</span> <span class="keyword">false</span>;
}
<span class="type">int</span> DeviceHandler<span class="operator">::</span>hr() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_currentValue;
}
<span class="type">int</span> DeviceHandler<span class="operator">::</span>time() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_start<span class="operator">.</span>secsTo(m_stop);
}
<span class="type">int</span> DeviceHandler<span class="operator">::</span>maxHR() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_max;
}
<span class="type">int</span> DeviceHandler<span class="operator">::</span>minHR() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_min;
}
<span class="type">float</span> DeviceHandler<span class="operator">::</span>average() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_avg;
}
<span class="type">float</span> DeviceHandler<span class="operator">::</span>calories() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_calories;
}
<span class="type">void</span> DeviceHandler<span class="operator">::</span>addMeasurement(<span class="type">int</span> value)
{
m_currentValue <span class="operator">=</span> value;
<span class="comment">// If measuring and value is appropriate</span>
<span class="keyword">if</span> (m_measuring <span class="operator">&</span><span class="operator">&</span> value <span class="operator">></span> <span class="number">30</span> <span class="operator">&</span><span class="operator">&</span> value <span class="operator"><</span> <span class="number">250</span>) {
m_stop <span class="operator">=</span> <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span><span class="operator">::</span>currentDateTime();
m_measurements <span class="operator"><</span><span class="operator"><</span> value;
m_min <span class="operator">=</span> m_min <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span> <span class="operator">?</span> value : <a href="../qtcore/qtglobal.html#qMin">qMin</a>(value<span class="operator">,</span> m_min);
m_max <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(value<span class="operator">,</span> m_max);
m_sum <span class="operator">+</span><span class="operator">=</span> value;
m_avg <span class="operator">=</span> (<span class="type">double</span>)m_sum <span class="operator">/</span> m_measurements<span class="operator">.</span>size();
m_calories <span class="operator">=</span> ((<span class="operator">-</span><span class="number">55.0969</span> <span class="operator">+</span> (<span class="number">0.6309</span> <span class="operator">*</span> m_avg) <span class="operator">+</span> (<span class="number">0.1988</span> <span class="operator">*</span> <span class="number">94</span>) <span class="operator">+</span> (<span class="number">0.2017</span> <span class="operator">*</span> <span class="number">24</span>)) <span class="operator">/</span> <span class="number">4.184</span>) <span class="operator">*</span> <span class="number">60</span> <span class="operator">*</span> time()<span class="operator">/</span><span class="number">3600</span>;
}
<span class="keyword">emit</span> statsChanged();
}
</pre>
</div>
<!-- @@@heartrate-game/devicehandler.cpp -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|