summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2023-03-27 11:46:33 +0200
committerIvan Solovev <ivan.solovev@qt.io>2023-03-28 14:44:16 +0200
commita1f43b9dcd722527d52ceb0c7a138bd2994c1a7b (patch)
tree827d2ee2e5e3a1edaec5b8ce7c4089d45ac3163b
parent6f155ca7df89c649e178dac57f69574290cb516b (diff)
downloadqtconnectivity-a1f43b9dcd722527d52ceb0c7a138bd2994c1a7b.tar.gz
HeartRate Game example: general clean-up
C++ code cleanup: * Fix include headers * Minor code-style improvements * Silence the warning about an uncreatable type by explicitly using QML_UNCREATABLE. We do not create the type in QML anyway. * Do not limit Windows platform to simulator mode. The example works perfectly on Windows. Build-system improvements: * Use qt_standard_project_setup() and PRIVATE linking in CMake Docs: * Add Connectivity category * Link to the documentation page which gives a full example overview Task-number: QTBUG-111972 Fixes: QTBUG-112194 Pick-to: 6.5 6.5.0 Change-Id: I6e50d1a3e9219afbf010d6471e8f7eb802c2ef00 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
-rw-r--r--examples/bluetooth/heartrate-game/CMakeLists.txt8
-rw-r--r--examples/bluetooth/heartrate-game/bluetoothbaseclass.h2
-rw-r--r--examples/bluetooth/heartrate-game/connectionhandler.cpp7
-rw-r--r--examples/bluetooth/heartrate-game/connectionhandler.h6
-rw-r--r--examples/bluetooth/heartrate-game/devicefinder.cpp17
-rw-r--r--examples/bluetooth/heartrate-game/devicefinder.h16
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.cpp19
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.h14
-rw-r--r--examples/bluetooth/heartrate-game/deviceinfo.cpp12
-rw-r--r--examples/bluetooth/heartrate-game/deviceinfo.h6
-rw-r--r--examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc5
-rw-r--r--examples/bluetooth/heartrate-game/main.cpp33
12 files changed, 81 insertions, 64 deletions
diff --git a/examples/bluetooth/heartrate-game/CMakeLists.txt b/examples/bluetooth/heartrate-game/CMakeLists.txt
index 1db4bd86..0d2a7c84 100644
--- a/examples/bluetooth/heartrate-game/CMakeLists.txt
+++ b/examples/bluetooth/heartrate-game/CMakeLists.txt
@@ -1,11 +1,9 @@
-# Copyright (C) 2022 The Qt Company Ltd.
+# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(heartrate-game LANGUAGES CXX)
-set(CMAKE_AUTOMOC ON)
-
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
@@ -14,6 +12,8 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/bluetooth/heartrate-game")
find_package(Qt6 REQUIRED COMPONENTS Bluetooth Core Gui Qml Quick)
+qt_standard_project_setup(REQUIRES 6.5)
+
qt_add_executable(heartrate-game
bluetoothbaseclass.cpp bluetoothbaseclass.h
connectionhandler.cpp connectionhandler.h
@@ -29,7 +29,7 @@ set_target_properties(heartrate-game PROPERTIES
MACOSX_BUNDLE TRUE
)
-target_link_libraries(heartrate-game PUBLIC
+target_link_libraries(heartrate-game PRIVATE
Qt::Bluetooth
Qt::Core
Qt::Gui
diff --git a/examples/bluetooth/heartrate-game/bluetoothbaseclass.h b/examples/bluetooth/heartrate-game/bluetoothbaseclass.h
index 42545686..2d25c546 100644
--- a/examples/bluetooth/heartrate-game/bluetoothbaseclass.h
+++ b/examples/bluetooth/heartrate-game/bluetoothbaseclass.h
@@ -4,7 +4,7 @@
#ifndef BLUETOOTHBASECLASS_H
#define BLUETOOTHBASECLASS_H
-#include <QObject>
+#include <QtCore/qobject.h>
class BluetoothBaseClass : public QObject
{
diff --git a/examples/bluetooth/heartrate-game/connectionhandler.cpp b/examples/bluetooth/heartrate-game/connectionhandler.cpp
index ba9bbf13..039b94d3 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.cpp
+++ b/examples/bluetooth/heartrate-game/connectionhandler.cpp
@@ -1,9 +1,11 @@
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include "heartrate-global.h"
#include "connectionhandler.h"
+#include "heartrate-global.h"
+
#include <QtBluetooth/qtbluetooth-config.h>
+
#include <QtCore/qsystemdetection.h>
ConnectionHandler::ConnectionHandler(QObject *parent) : QObject(parent)
@@ -21,7 +23,8 @@ bool ConnectionHandler::alive() const
#else
if (simulator)
return true;
- return m_localDevice.isValid() && m_localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff;
+ return m_localDevice.isValid()
+ && m_localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff;
#endif
}
diff --git a/examples/bluetooth/heartrate-game/connectionhandler.h b/examples/bluetooth/heartrate-game/connectionhandler.h
index 052def9f..eea1036c 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.h
+++ b/examples/bluetooth/heartrate-game/connectionhandler.h
@@ -4,11 +4,11 @@
#ifndef CONNECTIONHANDLER_H
#define CONNECTIONHANDLER_H
-#include <QBluetoothLocalDevice>
+#include <QtBluetooth/qbluetoothlocaldevice.h>
-#include <qqml.h>
+#include <QtCore/qobject.h>
-#include <QObject>
+#include <QtQmlIntegration/qqmlintegration.h>
class ConnectionHandler : public QObject
{
diff --git a/examples/bluetooth/heartrate-game/devicefinder.cpp b/examples/bluetooth/heartrate-game/devicefinder.cpp
index 1b29b443..2d306d0c 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.cpp
+++ b/examples/bluetooth/heartrate-game/devicefinder.cpp
@@ -6,6 +6,8 @@
#include "deviceinfo.h"
#include "heartrate-global.h"
+#include <QtBluetooth/qbluetoothdeviceinfo.h>
+
DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
BluetoothBaseClass(parent),
m_deviceHandler(handler)
@@ -14,12 +16,15 @@ DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(15000);
- connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &DeviceFinder::addDevice);
- connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, this,
- &DeviceFinder::scanError);
+ connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
+ this, &DeviceFinder::addDevice);
+ connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred,
+ this, &DeviceFinder::scanError);
- connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &DeviceFinder::scanFinished);
- connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &DeviceFinder::scanFinished);
+ connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished,
+ this, &DeviceFinder::scanFinished);
+ connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled,
+ this, &DeviceFinder::scanFinished);
//! [devicediscovery-1]
@@ -106,7 +111,7 @@ void DeviceFinder::connectToService(const QString &address)
DeviceInfo *currentDevice = nullptr;
for (QObject *entry : std::as_const(m_devices)) {
auto device = qobject_cast<DeviceInfo *>(entry);
- if (device && device->getAddress() == address ) {
+ if (device && device->getAddress() == address) {
currentDevice = device;
break;
}
diff --git a/examples/bluetooth/heartrate-game/devicefinder.h b/examples/bluetooth/heartrate-game/devicefinder.h
index 860e6066..3766ee5f 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.h
+++ b/examples/bluetooth/heartrate-game/devicefinder.h
@@ -6,13 +6,16 @@
#include "bluetoothbaseclass.h"
-#include <QBluetoothDeviceDiscoveryAgent>
-#include <QBluetoothDeviceInfo>
+#include <QtBluetooth/qbluetoothdevicediscoveryagent.h>
-#include <qqml.h>
+#include <QtCore/qtimer.h>
+#include <QtCore/qvariant.h>
-#include <QTimer>
-#include <QVariant>
+#include <QtQmlIntegration/qqmlintegration.h>
+
+QT_BEGIN_NAMESPACE
+class QBluetoothDeviceInfo;
+QT_END_NAMESPACE
class DeviceInfo;
class DeviceHandler;
@@ -25,6 +28,7 @@ class DeviceFinder: public BluetoothBaseClass
Q_PROPERTY(QVariant devices READ devices NOTIFY devicesChanged)
QML_ELEMENT
+ QML_UNCREATABLE("This class is not intended to be created directly")
public:
DeviceFinder(DeviceHandler *handler, QObject *parent = nullptr);
@@ -38,7 +42,7 @@ public slots:
void connectToService(const QString &address);
private slots:
- void addDevice(const QBluetoothDeviceInfo&);
+ void addDevice(const QBluetoothDeviceInfo &device);
void scanError(QBluetoothDeviceDiscoveryAgent::Error error);
void scanFinished();
diff --git a/examples/bluetooth/heartrate-game/devicehandler.cpp b/examples/bluetooth/heartrate-game/devicehandler.cpp
index 16413088..fa9da637 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.cpp
+++ b/examples/bluetooth/heartrate-game/devicehandler.cpp
@@ -1,12 +1,12 @@
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include "heartrate-global.h"
#include "devicehandler.h"
#include "deviceinfo.h"
+#include "heartrate-global.h"
-#include <QtEndian>
-#include <QRandomGenerator>
+#include <QtCore/qendian.h>
+#include <QtCore/qrandom.h>
DeviceHandler::DeviceHandler(QObject *parent) :
BluetoothBaseClass(parent)
@@ -159,7 +159,8 @@ void DeviceHandler::serviceStateChanged(QLowEnergyService::ServiceState s)
{
setInfo(tr("Service discovered."));
- const QLowEnergyCharacteristic hrChar = m_service->characteristic(QBluetoothUuid(QBluetoothUuid::CharacteristicType::HeartRateMeasurement));
+ const QLowEnergyCharacteristic hrChar =
+ m_service->characteristic(QBluetoothUuid(QBluetoothUuid::CharacteristicType::HeartRateMeasurement));
if (!hrChar.isValid()) {
setError("HR Data not found.");
break;
@@ -204,13 +205,12 @@ void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, cons
void DeviceHandler::updateDemoHR()
{
int randomValue = 0;
- if (m_currentValue < 30) { // Initial value
+ if (m_currentValue < 30) // Initial value
randomValue = 55 + QRandomGenerator::global()->bounded(30);
- } else if (!m_measuring) { // Value when relax
+ else if (!m_measuring) // Value when relax
randomValue = qBound(55, m_currentValue - 2 + QRandomGenerator::global()->bounded(5), 75);
- } else { // Measuring
+ else // Measuring
randomValue = m_currentValue + QRandomGenerator::global()->bounded(10) - 2;
- }
addMeasurement(randomValue);
}
@@ -302,7 +302,8 @@ void DeviceHandler::addMeasurement(int value)
m_max = qMax(value, m_max);
m_sum += value;
m_avg = (double)m_sum / m_measurements.size();
- m_calories = ((-55.0969 + (0.6309 * m_avg) + (0.1988 * 94) + (0.2017 * 24)) / 4.184) * 60 * time()/3600;
+ m_calories = ((-55.0969 + (0.6309 * m_avg) + (0.1988 * 94) + (0.2017 * 24)) / 4.184)
+ * 60 * time() / 3600;
}
emit statsChanged();
diff --git a/examples/bluetooth/heartrate-game/devicehandler.h b/examples/bluetooth/heartrate-game/devicehandler.h
index 7c8aa346..fd670f43 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.h
+++ b/examples/bluetooth/heartrate-game/devicehandler.h
@@ -6,14 +6,14 @@
#include "bluetoothbaseclass.h"
-#include <QLowEnergyController>
-#include <QLowEnergyService>
+#include <QtBluetooth/qlowenergycontroller.h>
+#include <QtBluetooth/qlowenergyservice.h>
-#include <qqml.h>
+#include <QtCore/qdatetime.h>
+#include <QtCore/qlist.h>
+#include <QtCore/qtimer.h>
-#include <QDateTime>
-#include <QList>
-#include <QTimer>
+#include <QtQmlIntegration/qqmlintegration.h>
class DeviceInfo;
@@ -78,7 +78,7 @@ private:
void updateHeartRateValue(const QLowEnergyCharacteristic &c,
const QByteArray &value);
void confirmedDescriptorWrite(const QLowEnergyDescriptor &d,
- const QByteArray &value);
+ const QByteArray &value);
void updateDemoHR();
diff --git a/examples/bluetooth/heartrate-game/deviceinfo.cpp b/examples/bluetooth/heartrate-game/deviceinfo.cpp
index e34d0993..55dcf0e8 100644
--- a/examples/bluetooth/heartrate-game/deviceinfo.cpp
+++ b/examples/bluetooth/heartrate-game/deviceinfo.cpp
@@ -1,11 +1,13 @@
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include "heartrate-global.h"
#include "deviceinfo.h"
+#include "heartrate-global.h"
+
+#include <QtBluetooth/qbluetoothaddress.h>
+#include <QtBluetooth/qbluetoothuuid.h>
-#include <QBluetoothAddress>
-#include <QBluetoothUuid>
+using namespace Qt::StringLiterals;
DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &info):
m_device(info)
@@ -20,14 +22,14 @@ QBluetoothDeviceInfo DeviceInfo::getDevice() const
QString DeviceInfo::getName() const
{
if (simulator)
- return "Demo device";
+ return u"Demo device"_s;
return m_device.name();
}
QString DeviceInfo::getAddress() const
{
if (simulator)
- return "00:11:22:33:44:55";
+ return u"00:11:22:33:44:55"_s;
#ifdef Q_OS_DARWIN
// workaround for Core Bluetooth:
return m_device.deviceUuid().toString();
diff --git a/examples/bluetooth/heartrate-game/deviceinfo.h b/examples/bluetooth/heartrate-game/deviceinfo.h
index 19dd7afb..32010f56 100644
--- a/examples/bluetooth/heartrate-game/deviceinfo.h
+++ b/examples/bluetooth/heartrate-game/deviceinfo.h
@@ -4,10 +4,10 @@
#ifndef DEVICEINFO_H
#define DEVICEINFO_H
-#include <QBluetoothDeviceInfo>
+#include <QtBluetooth/qbluetoothdeviceinfo.h>
-#include <QObject>
-#include <QString>
+#include <QtCore/qobject.h>
+#include <QtCore/qstring.h>
class DeviceInfo: public QObject
{
diff --git a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
index d8cbb6bc..256ff243 100644
--- a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
+++ b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
@@ -4,6 +4,7 @@
/*!
\example heartrate-game
\title Bluetooth Low Energy Heart Rate Game
+ \meta category {Connectivity}
\brief A game demonstrating the interaction with a Bluetooth Low Energy Heart Rate
device/service.
@@ -34,6 +35,10 @@
The goal of the game is to increase the measured heart rate as much as possible.
+ A detailed explanation of the APIs used in this example is given in the
+ \l {Using Qt Bluetooth Low Energy API} section of the Qt Bluetooth module
+ documentation.
+
The \l {lowenergyscanner}{Bluetooth Low Energy Scanner} example might be more suitable
if a heart rate device is not available. The scanner example works with any type of Bluetooth
Low Energy peripheral device.
diff --git a/examples/bluetooth/heartrate-game/main.cpp b/examples/bluetooth/heartrate-game/main.cpp
index 5e853a4f..11573512 100644
--- a/examples/bluetooth/heartrate-game/main.cpp
+++ b/examples/bluetooth/heartrate-game/main.cpp
@@ -6,38 +6,35 @@
#include "devicehandler.h"
#include "heartrate-global.h"
-#include <QQmlApplicationEngine>
-#include <QQmlContext>
+#include <QtCore/qcommandlineoption.h>
+#include <QtCore/qcommandlineparser.h>
+#include <QtCore/qloggingcategory.h>
-#include <QGuiApplication>
+#include <QtGui/qguiapplication.h>
-#include <QCommandLineParser>
-#include <QCommandLineOption>
-#include <QLoggingCategory>
+#include <QtQml/qqmlapplicationengine.h>
+
+using namespace Qt::StringLiterals;
-#ifndef Q_OS_WIN
bool simulator = false;
-#else
-bool simulator = true;
-#endif
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QCommandLineParser parser;
- parser.setApplicationDescription("Bluetooth Low Energy Heart Rate Game");
+ parser.setApplicationDescription(u"Bluetooth Low Energy Heart Rate Game"_s);
parser.addHelpOption();
parser.addVersionOption();
- QCommandLineOption simulatorOption("simulator", "Simulator");
+ QCommandLineOption simulatorOption(u"simulator"_s, u"Simulator"_s);
parser.addOption(simulatorOption);
- QCommandLineOption verboseOption("verbose", "Verbose mode");
+ QCommandLineOption verboseOption(u"verbose"_s, u"Verbose mode"_s);
parser.addOption(verboseOption);
parser.process(app);
if (parser.isSet(verboseOption))
- QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
+ QLoggingCategory::setFilterRules(u"qt.bluetooth* = true"_s);
simulator = parser.isSet(simulatorOption);
ConnectionHandler connectionHandler;
@@ -46,12 +43,12 @@ int main(int argc, char *argv[])
QQmlApplicationEngine engine;
engine.setInitialProperties({
- {"connectionHandler", QVariant::fromValue(&connectionHandler)},
- {"deviceFinder", QVariant::fromValue(&deviceFinder)},
- {"deviceHandler", QVariant::fromValue(&deviceHandler)}
+ {u"connectionHandler"_s, QVariant::fromValue(&connectionHandler)},
+ {u"deviceFinder"_s, QVariant::fromValue(&deviceFinder)},
+ {u"deviceHandler"_s, QVariant::fromValue(&deviceHandler)}
});
- engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
+ engine.load(QUrl(u"qrc:/qml/main.qml"_s));
if (engine.rootObjects().isEmpty())
return -1;