diff options
Diffstat (limited to 'examples/bluetooth/lowenergyscanner')
9 files changed, 42 insertions, 49 deletions
diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp index 58f91f5d..2402f4d1 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp @@ -53,10 +53,6 @@ #include "qbluetoothuuid.h" #include <QByteArray> -CharacteristicInfo::CharacteristicInfo() -{ -} - CharacteristicInfo::CharacteristicInfo(const QLowEnergyCharacteristic &characteristic): m_characteristic(characteristic) { @@ -76,7 +72,8 @@ QString CharacteristicInfo::getName() const return name; // find descriptor with CharacteristicUserDescription - foreach (const QLowEnergyDescriptor &descriptor, m_characteristic.descriptors()) { + const QList<QLowEnergyDescriptor> descriptors = m_characteristic.descriptors(); + for (const QLowEnergyDescriptor &descriptor : descriptors) { if (descriptor.type() == QBluetoothUuid::CharacteristicUserDescription) { name = descriptor.value(); break; @@ -130,7 +127,7 @@ QString CharacteristicInfo::getHandle() const QString CharacteristicInfo::getPermission() const { QString properties = "( "; - int permission = m_characteristic.properties(); + uint permission = m_characteristic.properties(); if (permission & QLowEnergyCharacteristic::Read) properties += QStringLiteral(" Read"); if (permission & QLowEnergyCharacteristic::Write) diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.h b/examples/bluetooth/lowenergyscanner/characteristicinfo.h index ccc280e8..3f3600ab 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.h +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.h @@ -65,7 +65,7 @@ class CharacteristicInfo: public QObject Q_PROPERTY(QString characteristicPermission READ getPermission NOTIFY characteristicChanged) public: - CharacteristicInfo(); + CharacteristicInfo() = default; CharacteristicInfo(const QLowEnergyCharacteristic &characteristic); void setCharacteristic(const QLowEnergyCharacteristic &characteristic); QString getName() const; diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp index d70d27a2..82179db1 100644 --- a/examples/bluetooth/lowenergyscanner/device.cpp +++ b/examples/bluetooth/lowenergyscanner/device.cpp @@ -61,8 +61,7 @@ #include <QMetaEnum> #include <QTimer> -Device::Device(): - connected(false), controller(0), m_deviceScanState(false), randomAddress(false) +Device::Device() { //! [les-devicediscovery-1] discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); @@ -110,7 +109,7 @@ void Device::startDeviceDiscovery() void Device::addDevice(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { - DeviceInfo *d = new DeviceInfo(info); + auto d = new DeviceInfo(info); devices.append(d); setUpdate("Last device added: " + d->getName()); } @@ -152,9 +151,13 @@ void Device::scanServices(const QString &address) { // We need the current device for service discovery. - for (int i = 0; i < devices.size(); i++) { - if (((DeviceInfo*)devices.at(i))->getAddress() == address ) - currentDevice.setDevice(((DeviceInfo*)devices.at(i))->getDevice()); + for (auto d: qAsConst(devices)) { + auto device = qobject_cast<DeviceInfo *>(d); + if (!device) + continue; + + if (device->getAddress() == address ) + currentDevice.setDevice(device->getDevice()); } if (!currentDevice.getDevice().isValid()) { @@ -174,13 +177,13 @@ void Device::scanServices(const QString &address) if (controller && m_previousAddress != currentDevice.getAddress()) { controller->disconnectFromDevice(); delete controller; - controller = 0; + controller = nullptr; } //! [les-controller-1] if (!controller) { // Connecting signals and slots for connecting to LE services. - controller = new QLowEnergyController(currentDevice.getDevice()); + controller = QLowEnergyController::createCentral(currentDevice.getDevice()); connect(controller, &QLowEnergyController::connected, this, &Device::deviceConnected); connect(controller, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error), @@ -212,7 +215,7 @@ void Device::addLowEnergyService(const QBluetoothUuid &serviceUuid) return; } //! [les-service-1] - ServiceInfo *serv = new ServiceInfo(service); + auto serv = new ServiceInfo(service); m_services.append(serv); emit servicesUpdated(); @@ -229,9 +232,12 @@ void Device::serviceScanDone() void Device::connectToService(const QString &uuid) { - QLowEnergyService *service = 0; - for (int i = 0; i < m_services.size(); i++) { - ServiceInfo *serviceInfo = (ServiceInfo*)m_services.at(i); + QLowEnergyService *service = nullptr; + for (auto s: qAsConst(m_services)) { + auto serviceInfo = qobject_cast<ServiceInfo *>(s); + if (!serviceInfo) + continue; + if (serviceInfo->getUuid() == uuid) { service = serviceInfo->service(); break; @@ -257,8 +263,8 @@ void Device::connectToService(const QString &uuid) //discovery already done const QList<QLowEnergyCharacteristic> chars = service->characteristics(); - foreach (const QLowEnergyCharacteristic &ch, chars) { - CharacteristicInfo *cInfo = new CharacteristicInfo(ch); + for (const QLowEnergyCharacteristic &ch : chars) { + auto cInfo = new CharacteristicInfo(ch); m_characteristics.append(cInfo); } @@ -280,7 +286,7 @@ void Device::errorReceived(QLowEnergyController::Error /*error*/) setUpdate(QString("Back\n(%1)").arg(controller->errorString())); } -void Device::setUpdate(QString message) +void Device::setUpdate(const QString &message) { m_message = message; emit updateChanged(); @@ -319,7 +325,7 @@ void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState) return; } - QLowEnergyService *service = qobject_cast<QLowEnergyService *>(sender()); + auto service = qobject_cast<QLowEnergyService *>(sender()); if (!service) return; @@ -327,8 +333,8 @@ void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState) //! [les-chars] const QList<QLowEnergyCharacteristic> chars = service->characteristics(); - foreach (const QLowEnergyCharacteristic &ch, chars) { - CharacteristicInfo *cInfo = new CharacteristicInfo(ch); + for (const QLowEnergyCharacteristic &ch : chars) { + auto cInfo = new CharacteristicInfo(ch); m_characteristics.append(cInfo); } //! [les-chars] @@ -360,9 +366,7 @@ bool Device::state() bool Device::hasControllerError() const { - if (controller && controller->error() != QLowEnergyController::NoError) - return true; - return false; + return (controller && controller->error() != QLowEnergyController::NoError); } bool Device::isRandomAddress() const diff --git a/examples/bluetooth/lowenergyscanner/device.h b/examples/bluetooth/lowenergyscanner/device.h index a30cf9b6..ef94c7f9 100644 --- a/examples/bluetooth/lowenergyscanner/device.h +++ b/examples/bluetooth/lowenergyscanner/device.h @@ -122,18 +122,18 @@ Q_SIGNALS: void randomAddressChanged(); private: - void setUpdate(QString message); + void setUpdate(const QString &message); QBluetoothDeviceDiscoveryAgent *discoveryAgent; DeviceInfo currentDevice; - QList<QObject*> devices; - QList<QObject*> m_services; - QList<QObject*> m_characteristics; + QList<QObject *> devices; + QList<QObject *> m_services; + QList<QObject *> m_characteristics; QString m_previousAddress; QString m_message; - bool connected; - QLowEnergyController *controller; - bool m_deviceScanState; - bool randomAddress; + bool connected = false; + QLowEnergyController *controller = nullptr; + bool m_deviceScanState = false; + bool randomAddress = false; }; #endif // DEVICE_H diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp index 69fedcea..23acc440 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp @@ -53,10 +53,6 @@ #include "deviceinfo.h" -DeviceInfo::DeviceInfo() -{ -} - DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &d) { device = d; diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.h b/examples/bluetooth/lowenergyscanner/deviceinfo.h index 8b20fa76..249afad2 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.h @@ -64,7 +64,7 @@ class DeviceInfo: public QObject Q_PROPERTY(QString deviceName READ getName NOTIFY deviceChanged) Q_PROPERTY(QString deviceAddress READ getAddress NOTIFY deviceChanged) public: - DeviceInfo(); + DeviceInfo() = default; DeviceInfo(const QBluetoothDeviceInfo &d); QString getAddress() const; QString getName() const; diff --git a/examples/bluetooth/lowenergyscanner/main.cpp b/examples/bluetooth/lowenergyscanner/main.cpp index b3fac945..351ab13a 100644 --- a/examples/bluetooth/lowenergyscanner/main.cpp +++ b/examples/bluetooth/lowenergyscanner/main.cpp @@ -62,11 +62,11 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); Device d; - QQuickView *view = new QQuickView; + auto view = new QQuickView; view->rootContext()->setContextProperty("device", &d); view->setSource(QUrl("qrc:/assets/main.qml")); view->setResizeMode(QQuickView::SizeRootObjectToView); view->show(); - return app.exec(); + return QGuiApplication::exec(); } diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp index 844c9cec..720619d0 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp @@ -51,10 +51,6 @@ #include "serviceinfo.h" -ServiceInfo::ServiceInfo() -{ -} - ServiceInfo::ServiceInfo(QLowEnergyService *service): m_service(service) { diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.h b/examples/bluetooth/lowenergyscanner/serviceinfo.h index e360fde7..6709be69 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.h @@ -60,7 +60,7 @@ class ServiceInfo: public QObject Q_PROPERTY(QString serviceUuid READ getUuid NOTIFY serviceChanged) Q_PROPERTY(QString serviceType READ getType NOTIFY serviceChanged) public: - ServiceInfo(); + ServiceInfo() = default; ServiceInfo(QLowEnergyService *service); QLowEnergyService *service() const; QString getUuid() const; @@ -71,7 +71,7 @@ Q_SIGNALS: void serviceChanged(); private: - QLowEnergyService *m_service; + QLowEnergyService *m_service = nullptr; }; #endif // SERVICEINFO_H |