summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-12 11:27:26 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-15 13:32:02 +0000
commit5bacb0fb97819dd337b450456536592ee7b684eb (patch)
tree53069a66bf46a637c1d4e5d3f19478bece9c88b2 /examples
parentc7cdadd88414e1de22bd1aa5b1748c2eef5bfa27 (diff)
Bluetooth heartrate-game example: Make simulation switcheable by command line arguments
Introduce QCommandLineParser for simulation and verbosity settings. Change-Id: I00d01d2fa73db311944a2df68c6b116f0a31811b Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit f2d0f8709d17afac1d7d15ee746397eb17e948f9) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
-rw-r--r--examples/bluetooth/heartrate-game/connectionhandler.cpp6
-rw-r--r--examples/bluetooth/heartrate-game/devicefinder.cpp42
-rw-r--r--examples/bluetooth/heartrate-game/devicefinder.h3
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.cpp29
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.h5
-rw-r--r--examples/bluetooth/heartrate-game/deviceinfo.cpp12
-rw-r--r--examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc4
-rw-r--r--examples/bluetooth/heartrate-game/heartrate-global.h7
-rw-r--r--examples/bluetooth/heartrate-game/main.cpp27
9 files changed, 74 insertions, 61 deletions
diff --git a/examples/bluetooth/heartrate-game/connectionhandler.cpp b/examples/bluetooth/heartrate-game/connectionhandler.cpp
index c0a7859c..ba9bbf13 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.cpp
+++ b/examples/bluetooth/heartrate-game/connectionhandler.cpp
@@ -14,9 +14,13 @@ ConnectionHandler::ConnectionHandler(QObject *parent) : QObject(parent)
bool ConnectionHandler::alive() const
{
-#if defined(SIMULATOR) || defined(QT_PLATFORM_UIKIT)
+
+#ifdef QT_PLATFORM_UIKIT
return true;
+
#else
+ if (simulator)
+ return true;
return m_localDevice.isValid() && m_localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff;
#endif
}
diff --git a/examples/bluetooth/heartrate-game/devicefinder.cpp b/examples/bluetooth/heartrate-game/devicefinder.cpp
index a45f4fba..1b7845c6 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.cpp
+++ b/examples/bluetooth/heartrate-game/devicefinder.cpp
@@ -4,6 +4,7 @@
#include "devicefinder.h"
#include "devicehandler.h"
#include "deviceinfo.h"
+#include "heartrate-global.h"
DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
BluetoothBaseClass(parent),
@@ -22,11 +23,11 @@ DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
//! [devicediscovery-1]
-#ifdef SIMULATOR
- m_demoTimer.setSingleShot(true);
- m_demoTimer.setInterval(2000);
- connect(&m_demoTimer, &QTimer::timeout, this, &DeviceFinder::scanFinished);
-#endif
+ if (simulator) {
+ m_demoTimer.setSingleShot(true);
+ m_demoTimer.setInterval(2000);
+ connect(&m_demoTimer, &QTimer::timeout, this, &DeviceFinder::scanFinished);
+ }
}
DeviceFinder::~DeviceFinder()
@@ -44,13 +45,14 @@ void DeviceFinder::startSearch()
emit devicesChanged();
-#ifdef SIMULATOR
- m_demoTimer.start();
-#else
- //! [devicediscovery-2]
- m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
- //! [devicediscovery-2]
-#endif
+ if (simulator) {
+ m_demoTimer.start();
+ } else {
+ //! [devicediscovery-2]
+ m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
+ //! [devicediscovery-2]
+ }
+
emit scanningChanged();
setInfo(tr("Scanning for devices..."));
}
@@ -82,11 +84,11 @@ void DeviceFinder::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
void DeviceFinder::scanFinished()
{
-#ifdef SIMULATOR
- // Only for testing
- for (int i = 0; i < 4; i++)
- m_devices.append(new DeviceInfo(QBluetoothDeviceInfo()));
-#endif
+ if (simulator) {
+ // Only for testing
+ for (int i = 0; i < 4; i++)
+ m_devices.append(new DeviceInfo(QBluetoothDeviceInfo()));
+ }
if (m_devices.isEmpty())
setError(tr("No Low Energy devices found."));
@@ -118,11 +120,9 @@ void DeviceFinder::connectToService(const QString &address)
bool DeviceFinder::scanning() const
{
-#ifdef SIMULATOR
- return m_demoTimer.isActive();
-#else
+ if (simulator)
+ return m_demoTimer.isActive();
return m_deviceDiscoveryAgent->isActive();
-#endif
}
QVariant DeviceFinder::devices()
diff --git a/examples/bluetooth/heartrate-game/devicefinder.h b/examples/bluetooth/heartrate-game/devicefinder.h
index f5844d6f..6f4226a8 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.h
+++ b/examples/bluetooth/heartrate-game/devicefinder.h
@@ -4,7 +4,6 @@
#ifndef DEVICEFINDER_H
#define DEVICEFINDER_H
-#include "heartrate-global.h"
#include "bluetoothbaseclass.h"
#include <QBluetoothDeviceDiscoveryAgent>
@@ -48,9 +47,7 @@ private:
QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent;
QList<QObject*> m_devices;
-#ifdef SIMULATOR
QTimer m_demoTimer;
-#endif
};
#endif // DEVICEFINDER_H
diff --git a/examples/bluetooth/heartrate-game/devicehandler.cpp b/examples/bluetooth/heartrate-game/devicehandler.cpp
index bbf4e1df..16413088 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.cpp
+++ b/examples/bluetooth/heartrate-game/devicehandler.cpp
@@ -11,13 +11,13 @@
DeviceHandler::DeviceHandler(QObject *parent) :
BluetoothBaseClass(parent)
{
-#ifdef SIMULATOR
- m_demoTimer.setSingleShot(false);
- m_demoTimer.setInterval(2000);
- connect(&m_demoTimer, &QTimer::timeout, this, &DeviceHandler::updateDemoHR);
- m_demoTimer.start();
- updateDemoHR();
-#endif
+ if (simulator) {
+ m_demoTimer.setSingleShot(false);
+ m_demoTimer.setInterval(2000);
+ connect(&m_demoTimer, &QTimer::timeout, this, &DeviceHandler::updateDemoHR);
+ m_demoTimer.start();
+ updateDemoHR();
+ }
}
void DeviceHandler::setAddressType(AddressType type)
@@ -45,10 +45,10 @@ void DeviceHandler::setDevice(DeviceInfo *device)
clearMessages();
m_currentDevice = device;
-#ifdef SIMULATOR
- setInfo(tr("Demo device connected."));
- return;
-#endif
+ if (simulator) {
+ setInfo(tr("Demo device connected."));
+ return;
+ }
// Disconnect and delete old connection
if (m_control) {
@@ -201,7 +201,6 @@ void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, cons
}
//! [Reading value]
-#ifdef SIMULATOR
void DeviceHandler::updateDemoHR()
{
int randomValue = 0;
@@ -215,7 +214,6 @@ void DeviceHandler::updateDemoHR()
addMeasurement(randomValue);
}
-#endif
void DeviceHandler::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
{
@@ -251,9 +249,8 @@ bool DeviceHandler::measuring() const
bool DeviceHandler::alive() const
{
-#ifdef SIMULATOR
- return true;
-#endif
+ if (simulator)
+ return true;
if (m_service)
return m_service->state() == QLowEnergyService::RemoteServiceDiscovered;
diff --git a/examples/bluetooth/heartrate-game/devicehandler.h b/examples/bluetooth/heartrate-game/devicehandler.h
index 1488010a..522f8ee8 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.h
+++ b/examples/bluetooth/heartrate-game/devicehandler.h
@@ -75,9 +75,8 @@ private:
void confirmedDescriptorWrite(const QLowEnergyDescriptor &d,
const QByteArray &value);
-#ifdef SIMULATOR
void updateDemoHR();
-#endif
+
private:
void addMeasurement(int value);
@@ -98,9 +97,7 @@ private:
QList<int> m_measurements;
QLowEnergyController::RemoteAddressType m_addressType = QLowEnergyController::PublicAddress;
-#ifdef SIMULATOR
QTimer m_demoTimer;
-#endif
};
#endif // DEVICEHANDLER_H
diff --git a/examples/bluetooth/heartrate-game/deviceinfo.cpp b/examples/bluetooth/heartrate-game/deviceinfo.cpp
index 79e5cbf8..e34d0993 100644
--- a/examples/bluetooth/heartrate-game/deviceinfo.cpp
+++ b/examples/bluetooth/heartrate-game/deviceinfo.cpp
@@ -19,18 +19,16 @@ QBluetoothDeviceInfo DeviceInfo::getDevice() const
QString DeviceInfo::getName() const
{
-#ifdef SIMULATOR
- return "Demo device";
-#else
+ if (simulator)
+ return "Demo device";
return m_device.name();
-#endif
}
QString DeviceInfo::getAddress() const
{
-#ifdef SIMULATOR
- return "00:11:22:33:44:55";
-#elif defined Q_OS_DARWIN
+ if (simulator)
+ return "00:11:22:33:44:55";
+#ifdef Q_OS_DARWIN
// workaround for Core Bluetooth:
return m_device.deviceUuid().toString();
#else
diff --git a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
index 759fe7e9..aa9487c7 100644
--- a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
+++ b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
@@ -29,8 +29,8 @@
Bluetooth Low Energy device which might simulate the service. You can also use the
\l {heartrate-server} {Heart Rate server} example for that purpose.
If no such device is available, a demo mode is available which creates and displays
- random values. This demo mode is enabled by defining USE_SIMULATOR in
- heartrate-global.h.
+ random values. This demo mode is enabled by passing --simulator on the
+ command line.
The goal of the game is to increase the measured heart rate as much as possible.
diff --git a/examples/bluetooth/heartrate-game/heartrate-global.h b/examples/bluetooth/heartrate-game/heartrate-global.h
index 9d48c1f7..a967e64e 100644
--- a/examples/bluetooth/heartrate-game/heartrate-global.h
+++ b/examples/bluetooth/heartrate-game/heartrate-global.h
@@ -4,11 +4,6 @@
#ifndef HEARTRATEGLOBAL_H
#define HEARTRATEGLOBAL_H
-//#define USE_SIMULATOR
-
-#if defined(Q_OS_WIN32) || defined(USE_SIMULATOR)
-#define SIMULATOR
-#endif
-
+extern bool simulator;
#endif // HEARTRATEGLOBAL_H
diff --git a/examples/bluetooth/heartrate-game/main.cpp b/examples/bluetooth/heartrate-game/main.cpp
index 990387ae..9722a82f 100644
--- a/examples/bluetooth/heartrate-game/main.cpp
+++ b/examples/bluetooth/heartrate-game/main.cpp
@@ -4,19 +4,42 @@
#include "connectionhandler.h"
#include "devicefinder.h"
#include "devicehandler.h"
+#include "heartrate-global.h"
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QGuiApplication>
+#include <QCommandLineParser>
+#include <QCommandLineOption>
#include <QLoggingCategory>
+#ifndef Q_OS_WIN
+bool simulator = false;
+#else
+bool simulator = true;
+#endif
+
int main(int argc, char *argv[])
{
- // QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
QGuiApplication app(argc, argv);
+ QCommandLineParser parser;
+ parser.setApplicationDescription("Bluetooth Low Energy Heart Rate Game");
+ parser.addHelpOption();
+ parser.addVersionOption();
+ QCommandLineOption simulatorOption("simulator", "Simulator");
+ parser.addOption(simulatorOption);
+
+ QCommandLineOption verboseOption("verbose", "Verbose mode");
+ parser.addOption(verboseOption);
+ parser.process(app);
+
+ if (parser.isSet(verboseOption))
+ QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
+ simulator = parser.isSet(simulatorOption);
+
ConnectionHandler connectionHandler;
DeviceHandler deviceHandler;
DeviceFinder deviceFinder(&deviceHandler);
@@ -29,6 +52,8 @@ int main(int argc, char *argv[])
engine.rootContext()->setContextProperty("deviceHandler", &deviceHandler);
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
+ if (engine.rootObjects().isEmpty())
+ return -1;
return app.exec();
}