summaryrefslogtreecommitdiff
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 14:09:19 +0000
commit69da3068dda618b522e7552dd45bcc38d1dc7339 (patch)
treedaefebe4e3e60342405faa934d30de9442470b97
parent6ff04b182453f7a8cf6e73dd90b1a8282fc821d1 (diff)
downloadqtconnectivity-69da3068dda618b522e7552dd45bcc38d1dc7339.tar.gz
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>
-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 3388c0a8..a1a5af0c 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.cpp
+++ b/examples/bluetooth/heartrate-game/connectionhandler.cpp
@@ -61,9 +61,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 fdb536b7..81c4db82 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.cpp
+++ b/examples/bluetooth/heartrate-game/devicefinder.cpp
@@ -51,6 +51,7 @@
#include "devicefinder.h"
#include "devicehandler.h"
#include "deviceinfo.h"
+#include "heartrate-global.h"
DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
BluetoothBaseClass(parent),
@@ -69,11 +70,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()
@@ -91,13 +92,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..."));
}
@@ -129,11 +131,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."));
@@ -165,11 +167,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 2624b3b8..c8c5bd3c 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.h
+++ b/examples/bluetooth/heartrate-game/devicefinder.h
@@ -51,7 +51,6 @@
#ifndef DEVICEFINDER_H
#define DEVICEFINDER_H
-#include "heartrate-global.h"
#include "bluetoothbaseclass.h"
#include <QBluetoothDeviceDiscoveryAgent>
@@ -95,9 +94,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 4a51d159..4c3851ee 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.cpp
+++ b/examples/bluetooth/heartrate-game/devicehandler.cpp
@@ -58,13 +58,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)
@@ -92,10 +92,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) {
@@ -248,7 +248,6 @@ void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, cons
}
//! [Reading value]
-#ifdef SIMULATOR
void DeviceHandler::updateDemoHR()
{
int randomValue = 0;
@@ -262,7 +261,6 @@ void DeviceHandler::updateDemoHR()
addMeasurement(randomValue);
}
-#endif
void DeviceHandler::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
{
@@ -298,9 +296,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 2dfc69e1..7f97c1b2 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.h
+++ b/examples/bluetooth/heartrate-game/devicehandler.h
@@ -122,9 +122,8 @@ private:
void confirmedDescriptorWrite(const QLowEnergyDescriptor &d,
const QByteArray &value);
-#ifdef SIMULATOR
void updateDemoHR();
-#endif
+
private:
void addMeasurement(int value);
@@ -145,9 +144,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 0996336f..6514d1fe 100644
--- a/examples/bluetooth/heartrate-game/deviceinfo.cpp
+++ b/examples/bluetooth/heartrate-game/deviceinfo.cpp
@@ -66,18 +66,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 dc710e56..2cd6b2c2 100644
--- a/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
+++ b/examples/bluetooth/heartrate-game/doc/src/heartrate-game.qdoc
@@ -53,8 +53,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 ad16ca74..e3eab8d3 100644
--- a/examples/bluetooth/heartrate-game/heartrate-global.h
+++ b/examples/bluetooth/heartrate-game/heartrate-global.h
@@ -51,11 +51,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 bb122ed7..d0013c47 100644
--- a/examples/bluetooth/heartrate-game/main.cpp
+++ b/examples/bluetooth/heartrate-game/main.cpp
@@ -51,19 +51,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);
@@ -76,6 +99,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();
}