From 1843ec089cf149d940909841d436aa3bb3d79cc0 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 23 Mar 2023 13:40:28 +0100 Subject: LowEnergyScanner example: general clean-up This patch introduces non-QML clean-ups to the example: * add Connectivity category to the docs * fix includes * consistently use Qt::StringLiterals * split some too long lines QML part requires a huge refactoring, which is done in a follow-up commit. Task-number: QTBUG-111972 Change-Id: I053b1c564d9dc2e05dfdb8879821615391e4be35 Reviewed-by: Juha Vuolle (cherry picked from commit b72c83bfecc5d259ef0fcf20cb05d2464a7fd0ab) --- .../lowenergyscanner/characteristicinfo.cpp | 37 +++++++------- .../lowenergyscanner/characteristicinfo.h | 8 +-- examples/bluetooth/lowenergyscanner/device.cpp | 58 +++++++++++----------- examples/bluetooth/lowenergyscanner/device.h | 24 +++++---- examples/bluetooth/lowenergyscanner/deviceinfo.cpp | 10 ++-- examples/bluetooth/lowenergyscanner/deviceinfo.h | 9 ++-- .../lowenergyscanner/doc/src/lowenergyscanner.qdoc | 3 +- examples/bluetooth/lowenergyscanner/main.cpp | 8 +-- .../bluetooth/lowenergyscanner/serviceinfo.cpp | 19 ++++--- examples/bluetooth/lowenergyscanner/serviceinfo.h | 7 ++- 10 files changed, 101 insertions(+), 82 deletions(-) diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp index a2b405b0..34bf82ce 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp @@ -4,7 +4,10 @@ #include "characteristicinfo.h" #include "qbluetoothuuid.h" -#include + +#include + +using namespace Qt::StringLiterals; CharacteristicInfo::CharacteristicInfo(const QLowEnergyCharacteristic &characteristic): m_characteristic(characteristic) @@ -35,7 +38,7 @@ QString CharacteristicInfo::getName() const //! [les-get-descriptors] if (name.isEmpty()) - name = "Unknown"; + name = u"Unknown"_s; return name; } @@ -46,13 +49,13 @@ QString CharacteristicInfo::getUuid() const bool success = false; quint16 result16 = uuid.toUInt16(&success); if (success) - return QStringLiteral("0x") + QString::number(result16, 16); + return u"0x"_s + QString::number(result16, 16); quint32 result32 = uuid.toUInt32(&success); if (success) - return QStringLiteral("0x") + QString::number(result32, 16); + return u"0x"_s + QString::number(result32, 16); - return uuid.toString().remove(QLatin1Char('{')).remove(QLatin1Char('}')); + return uuid.toString().remove('{'_L1).remove('}'_L1); } QString CharacteristicInfo::getValue() const @@ -61,12 +64,12 @@ QString CharacteristicInfo::getValue() const QByteArray a = m_characteristic.value(); QString result; if (a.isEmpty()) { - result = QStringLiteral(""); + result = u""_s; return result; } result = a; - result += QLatin1Char('\n'); + result += '\n'_L1; result += a.toHex(); return result; @@ -74,25 +77,25 @@ QString CharacteristicInfo::getValue() const QString CharacteristicInfo::getPermission() const { - QString properties = "( "; + QString properties = u"( "_s; uint permission = m_characteristic.properties(); if (permission & QLowEnergyCharacteristic::Read) - properties += QStringLiteral(" Read"); + properties += u" Read"_s; if (permission & QLowEnergyCharacteristic::Write) - properties += QStringLiteral(" Write"); + properties += u" Write"_s; if (permission & QLowEnergyCharacteristic::Notify) - properties += QStringLiteral(" Notify"); + properties += u" Notify"_s; if (permission & QLowEnergyCharacteristic::Indicate) - properties += QStringLiteral(" Indicate"); + properties += u" Indicate"_s; if (permission & QLowEnergyCharacteristic::ExtendedProperty) - properties += QStringLiteral(" ExtendedProperty"); + properties += u" ExtendedProperty"_s; if (permission & QLowEnergyCharacteristic::Broadcasting) - properties += QStringLiteral(" Broadcast"); + properties += u" Broadcast"_s; if (permission & QLowEnergyCharacteristic::WriteNoResponse) - properties += QStringLiteral(" WriteNoResp"); + properties += u" WriteNoResp"_s; if (permission & QLowEnergyCharacteristic::WriteSigned) - properties += QStringLiteral(" WriteSigned"); - properties += " )"; + properties += u" WriteSigned"_s; + properties += u" )"_s; return properties; } diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.h b/examples/bluetooth/lowenergyscanner/characteristicinfo.h index bb07fa1c..7a258fa7 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.h +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.h @@ -4,9 +4,11 @@ #ifndef CHARACTERISTICINFO_H #define CHARACTERISTICINFO_H -#include -#include -#include + +#include + +#include +#include class CharacteristicInfo: public QObject { diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp index d645931c..459afb87 100644 --- a/examples/bluetooth/lowenergyscanner/device.cpp +++ b/examples/bluetooth/lowenergyscanner/device.cpp @@ -4,35 +4,33 @@ #include "device.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include + +#include +#include +#include + +using namespace Qt::StringLiterals; Device::Device() { //! [les-devicediscovery-1] - discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); + discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); discoveryAgent->setLowEnergyDiscoveryTimeout(25000); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Device::addDevice); - connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, this, - &Device::deviceScanError); - connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished); + connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, + this, &Device::deviceScanError); + connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, + this, &Device::deviceScanFinished); //! [les-devicediscovery-1] - setUpdate("Search"); + setUpdate(u"Search"_s); } Device::~Device() { - delete discoveryAgent; - delete controller; qDeleteAll(devices); qDeleteAll(m_services); qDeleteAll(m_characteristics); @@ -47,7 +45,7 @@ void Device::startDeviceDiscovery() devices.clear(); emit devicesUpdated(); - setUpdate("Scanning for devices ..."); + setUpdate(u"Scanning for devices ..."_s); //! [les-devicediscovery-2] discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); //! [les-devicediscovery-2] @@ -62,7 +60,7 @@ void Device::startDeviceDiscovery() void Device::addDevice(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) - setUpdate("Last device added: " + info.name()); + setUpdate(u"Last device added: "_s + info.name()); } //! [les-devicediscovery-3] @@ -77,9 +75,9 @@ void Device::deviceScanFinished() m_deviceScanState = false; emit stateChanged(); if (devices.isEmpty()) - setUpdate("No Low Energy devices found..."); + setUpdate(u"No Low Energy devices found..."_s); else - setUpdate("Done! Scan Again!"); + setUpdate(u"Done! Scan Again!"_s); } QVariant Device::getDevices() @@ -108,7 +106,7 @@ void Device::scanServices(const QString &address) for (auto d: std::as_const(devices)) { if (auto device = qobject_cast(d)) { - if (device->getAddress() == address ) { + if (device->getAddress() == address) { currentDevice.setDevice(device->getDevice()); break; } @@ -127,7 +125,7 @@ void Device::scanServices(const QString &address) m_services.clear(); emit servicesUpdated(); - setUpdate("Back\n(Connecting to device...)"); + setUpdate(u"Back\n(Connecting to device...)"_s); if (controller && m_previousAddress != currentDevice.getAddress()) { controller->disconnectFromDevice(); @@ -138,7 +136,7 @@ void Device::scanServices(const QString &address) //! [les-controller-1] if (!controller) { // Connecting signals and slots for connecting to LE services. - controller = QLowEnergyController::createCentral(currentDevice.getDevice()); + controller = QLowEnergyController::createCentral(currentDevice.getDevice(), this); connect(controller, &QLowEnergyController::connected, this, &Device::deviceConnected); connect(controller, &QLowEnergyController::errorOccurred, this, &Device::errorReceived); @@ -178,7 +176,7 @@ void Device::addLowEnergyService(const QBluetoothUuid &serviceUuid) void Device::serviceScanDone() { - setUpdate("Back\n(Service scan done!)"); + setUpdate(u"Back\n(Service scan done!)"_s); // force UI in case we didn't find anything if (m_services.isEmpty()) emit servicesUpdated(); @@ -210,7 +208,7 @@ void Device::connectToService(const QString &uuid) connect(service, &QLowEnergyService::stateChanged, this, &Device::serviceDetailsDiscovered); service->discoverDetails(); - setUpdate("Back\n(Discovering details...)"); + setUpdate(u"Back\n(Discovering details...)"_s); //! [les-service-3] return; } @@ -227,7 +225,7 @@ void Device::connectToService(const QString &uuid) void Device::deviceConnected() { - setUpdate("Back\n(Discovering services...)"); + setUpdate(u"Back\n(Discovering services...)"_s); connected = true; //! [les-service-2] controller->discoverServices(); @@ -237,7 +235,7 @@ void Device::deviceConnected() void Device::errorReceived(QLowEnergyController::Error /*error*/) { qWarning() << "Error: " << controller->errorString(); - setUpdate(QString("Back\n(%1)").arg(controller->errorString())); + setUpdate(u"Back\n(%1)"_s.arg(controller->errorString())); } void Device::setUpdate(const QString &message) @@ -299,13 +297,13 @@ void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState) void Device::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error) { if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError) - setUpdate("The Bluetooth adaptor is powered off, power it on before doing discovery."); + setUpdate(u"The Bluetooth adaptor is powered off, power it on before doing discovery."_s); else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError) - setUpdate("Writing or reading from the device resulted in an error."); + setUpdate(u"Writing or reading from the device resulted in an error."_s); else { static QMetaEnum qme = discoveryAgent->metaObject()->enumerator( discoveryAgent->metaObject()->indexOfEnumerator("Error")); - setUpdate("Error: " + QLatin1String(qme.valueToKey(error))); + setUpdate(u"Error: "_s + QLatin1StringView(qme.valueToKey(error))); } m_deviceScanState = false; diff --git a/examples/bluetooth/lowenergyscanner/device.h b/examples/bluetooth/lowenergyscanner/device.h index 65f864ad..6a1d2b9c 100644 --- a/examples/bluetooth/lowenergyscanner/device.h +++ b/examples/bluetooth/lowenergyscanner/device.h @@ -4,20 +4,22 @@ #ifndef DEVICE_H #define DEVICE_H -#include -#include -#include -#include -#include -#include -#include -#include +#include "characteristicinfo.h" #include "deviceinfo.h" #include "serviceinfo.h" -#include "characteristicinfo.h" -QT_FORWARD_DECLARE_CLASS (QBluetoothDeviceInfo) -QT_FORWARD_DECLARE_CLASS (QBluetoothServiceInfo) +#include +#include +#include + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QBluetoothDeviceInfo; +class QBluetoothUuid; +QT_END_NAMESPACE class Device: public QObject { diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp index 1102012b..24ab5661 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp @@ -2,10 +2,14 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include - #include "deviceinfo.h" +#ifdef Q_OS_DARWIN +# include +#else +# include +#endif + DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &d) { device = d; @@ -13,7 +17,7 @@ DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &d) QString DeviceInfo::getAddress() const { -#ifdef Q_OS_MAC +#ifdef Q_OS_DARWIN // On OS X and iOS we do not have addresses, // only unique UUIDs generated by Core Bluetooth. return device.deviceUuid().toString(); diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.h b/examples/bluetooth/lowenergyscanner/deviceinfo.h index d76bf4a3..78854490 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.h @@ -5,11 +5,10 @@ #ifndef DEVICEINFO_H #define DEVICEINFO_H -#include -#include -#include -#include -#include "deviceinfo.h" +#include + +#include +#include class DeviceInfo: public QObject { diff --git a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc index bba28796..2ac19ddb 100644 --- a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc +++ b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc @@ -4,7 +4,8 @@ /*! \example lowenergyscanner - \title Bluetooth Low Energy Scanner Example + \title Bluetooth Low Energy Scanner + \meta category {Connectivity} \brief An application designed to browse the content of Bluetooth Low Energy peripheral devices. The example demonstrates the use of all Qt Bluetooth Low Energy classes. diff --git a/examples/bluetooth/lowenergyscanner/main.cpp b/examples/bluetooth/lowenergyscanner/main.cpp index eaeba0c2..7fbb8c99 100644 --- a/examples/bluetooth/lowenergyscanner/main.cpp +++ b/examples/bluetooth/lowenergyscanner/main.cpp @@ -2,12 +2,12 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include -#include -#include -#include #include "device.h" +#include +#include +#include +#include int main(int argc, char *argv[]) { diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp index a00a4802..9032c7f2 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp @@ -4,6 +4,11 @@ #include "serviceinfo.h" +#include +#include + +using namespace Qt::StringLiterals; + ServiceInfo::ServiceInfo(QLowEnergyService *service): m_service(service) { @@ -30,14 +35,14 @@ QString ServiceInfo::getType() const QString result; if (m_service->type() & QLowEnergyService::PrimaryService) - result += QStringLiteral("primary"); + result += u"primary"_s; else - result += QStringLiteral("secondary"); + result += u"secondary"_s; if (m_service->type() & QLowEnergyService::IncludedService) - result += QStringLiteral(" included"); + result += u" included"_s; - result.prepend('<').append('>'); + result.prepend('<'_L1).append('>'_L1); return result; } @@ -51,11 +56,11 @@ QString ServiceInfo::getUuid() const bool success = false; quint16 result16 = uuid.toUInt16(&success); if (success) - return QStringLiteral("0x") + QString::number(result16, 16); + return u"0x"_s + QString::number(result16, 16); quint32 result32 = uuid.toUInt32(&success); if (success) - return QStringLiteral("0x") + QString::number(result32, 16); + return u"0x"_s + QString::number(result32, 16); - return uuid.toString().remove(QLatin1Char('{')).remove(QLatin1Char('}')); + return uuid.toString().remove('{'_L1).remove('}'_L1); } diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.h b/examples/bluetooth/lowenergyscanner/serviceinfo.h index 57c5dcfe..f89b37a0 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.h @@ -4,7 +4,12 @@ #ifndef SERVICEINFO_H #define SERVICEINFO_H -#include + +#include + +QT_BEGIN_NAMESPACE +class QLowEnergyService; +QT_END_NAMESPACE class ServiceInfo: public QObject { -- cgit v1.2.1