diff options
Diffstat (limited to 'examples')
29 files changed, 185 insertions, 164 deletions
diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp index 9325765e..4cc5f08d 100644 --- a/examples/bluetooth/btchat/chat.cpp +++ b/examples/bluetooth/btchat/chat.cpp @@ -53,34 +53,30 @@ #include "chatserver.h" #include "chatclient.h" -#include <qbluetoothuuid.h> -#include <qbluetoothserver.h> -#include <qbluetoothservicediscoveryagent.h> -#include <qbluetoothdeviceinfo.h> -#include <qbluetoothlocaldevice.h> +#include <QtCore/qdebug.h> + +#include <QtBluetooth/qbluetoothdeviceinfo.h> +#include <QtBluetooth/qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothuuid.h> #ifdef Q_OS_ANDROID #include <QtAndroidExtras/QtAndroid> #endif -#include <QTimer> - -#include <QDebug> - static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); #ifdef Q_OS_ANDROID static const QLatin1String reverseUuid("c8e96402-0102-cf9c-274b-701a950fe1e8"); #endif Chat::Chat(QWidget *parent) - : QDialog(parent), currentAdapterIndex(0), ui(new Ui_Chat) + : QDialog(parent), ui(new Ui_Chat) { //! [Construct UI] ui->setupUi(this); - connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(accept())); - connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectClicked())); - connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendClicked())); + connect(ui->quitButton, &QPushButton::clicked, this, &Chat::accept); + connect(ui->connectButton, &QPushButton::clicked, this, &Chat::connectClicked); + connect(ui->sendButton, &QPushButton::clicked, this, &Chat::sendClicked); //! [Construct UI] localAdapters = QBluetoothLocalDevice::allDevices(); @@ -93,19 +89,21 @@ Chat::Chat(QWidget *parent) arg(localAdapters.at(0).address().toString())); ui->secondAdapter->setText(localAdapters.at(1).address().toString()); ui->firstAdapter->setChecked(true); - connect(ui->firstAdapter, SIGNAL(clicked()), this, SLOT(newAdapterSelected())); - connect(ui->secondAdapter, SIGNAL(clicked()), this, SLOT(newAdapterSelected())); + connect(ui->firstAdapter, &QRadioButton::clicked, this, &Chat::newAdapterSelected); + connect(ui->secondAdapter, &QRadioButton::clicked, this, &Chat::newAdapterSelected); QBluetoothLocalDevice adapter(localAdapters.at(0).address()); adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable); } //! [Create Chat Server] server = new ChatServer(this); - connect(server, SIGNAL(clientConnected(QString)), this, SLOT(clientConnected(QString))); - connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(clientDisconnected(QString))); - connect(server, SIGNAL(messageReceived(QString,QString)), - this, SLOT(showMessage(QString,QString))); - connect(this, SIGNAL(sendMessage(QString)), server, SLOT(sendMessage(QString))); + connect(server, QOverload<const QString &>::of(&ChatServer::clientConnected), + this, &Chat::clientConnected); + connect(server, QOverload<const QString &>::of(&ChatServer::clientDisconnected), + this, QOverload<const QString &>::of(&Chat::clientDisconnected)); + connect(server, &ChatServer::messageReceived, + this, &Chat::showMessage); + connect(this, &Chat::sendMessage, server, &ChatServer::sendMessage); server->startServer(); //! [Create Chat Server] @@ -137,6 +135,7 @@ void Chat::connected(const QString &name) { ui->chat->insertPlainText(QString::fromLatin1("Joined chat with %1.\n").arg(name)); } +//! [connected] void Chat::newAdapterSelected() { @@ -163,7 +162,11 @@ int Chat::adapterFromUserSelection() const } return result; } -//! [connected] + +void Chat::reactOnSocketError(const QString &error) +{ + ui->chat->insertPlainText(error); +} //! [clientDisconnected] void Chat::clientDisconnected() @@ -206,11 +209,15 @@ void Chat::connectClicked() ChatClient *client = new ChatClient(this); qDebug() << "Connecting..."; - connect(client, SIGNAL(messageReceived(QString,QString)), - this, SLOT(showMessage(QString,QString))); - connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); - connect(client, SIGNAL(connected(QString)), this, SLOT(connected(QString))); - connect(this, SIGNAL(sendMessage(QString)), client, SLOT(sendMessage(QString))); + connect(client, &ChatClient::messageReceived, + this, &Chat::showMessage); + connect(client, &ChatClient::disconnected, + this, QOverload<>::of(&Chat::clientDisconnected)); + connect(client, QOverload<const QString &>::of(&ChatClient::connected), + this, &Chat::connected); + connect(client, &ChatClient::socketErrorOccurred, + this, &Chat::reactOnSocketError); + connect(this, &Chat::sendMessage, client, &ChatClient::sendMessage); qDebug() << "Start client"; client->startClient(service); diff --git a/examples/bluetooth/btchat/chat.h b/examples/bluetooth/btchat/chat.h index 57f13257..e4c81b24 100644 --- a/examples/bluetooth/btchat/chat.h +++ b/examples/bluetooth/btchat/chat.h @@ -50,13 +50,9 @@ #include "ui_chat.h" -#include <QDialog> +#include <QtWidgets/qdialog.h> -#include <qbluetoothserviceinfo.h> -#include <qbluetoothsocket.h> -#include <qbluetoothhostinfo.h> - -#include <QDebug> +#include <QtBluetooth/qbluetoothhostinfo.h> QT_USE_NAMESPACE @@ -69,7 +65,7 @@ class Chat : public QDialog Q_OBJECT public: - Chat(QWidget *parent = 0); + explicit Chat(QWidget *parent = nullptr); ~Chat(); signals: @@ -85,12 +81,13 @@ private slots: void clientDisconnected(const QString &name); void clientDisconnected(); void connected(const QString &name); + void reactOnSocketError(const QString &error); void newAdapterSelected(); private: int adapterFromUserSelection() const; - int currentAdapterIndex; + int currentAdapterIndex = 0; Ui_Chat *ui; ChatServer *server; diff --git a/examples/bluetooth/btchat/chatclient.cpp b/examples/bluetooth/btchat/chatclient.cpp index 6682667f..cf3e2331 100644 --- a/examples/bluetooth/btchat/chatclient.cpp +++ b/examples/bluetooth/btchat/chatclient.cpp @@ -50,10 +50,10 @@ #include "chatclient.h" -#include <qbluetoothsocket.h> +#include <QtCore/qmetaobject.h> ChatClient::ChatClient(QObject *parent) -: QObject(parent), socket(0) + : QObject(parent) { } @@ -74,9 +74,12 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService) socket->connectToService(remoteService); qDebug() << "ConnectToService done"; - connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); - connect(socket, SIGNAL(connected()), this, SLOT(connected())); - connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected())); + connect(socket, &QBluetoothSocket::readyRead, this, &ChatClient::readSocket); + connect(socket, &QBluetoothSocket::connected, this, QOverload<>::of(&ChatClient::connected)); + connect(socket, &QBluetoothSocket::disconnected, this, &ChatClient::disconnected); + connect(socket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error), + this, &ChatClient::onSocketErrorOccurred); + } //! [startClient] @@ -84,7 +87,7 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService) void ChatClient::stopClient() { delete socket; - socket = 0; + socket = nullptr; } //! [stopClient] @@ -110,6 +113,18 @@ void ChatClient::sendMessage(const QString &message) } //! [sendMessage] +void ChatClient::onSocketErrorOccurred(QBluetoothSocket::SocketError error) +{ + if (error == QBluetoothSocket::NoSocketError) + return; + + QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketError>(); + QString errorString = socket->peerName() + QLatin1Char(' ') + + metaEnum.valueToKey(error) + QLatin1String(" occurred"); + + emit socketErrorOccurred(errorString); +} + //! [connected] void ChatClient::connected() { diff --git a/examples/bluetooth/btchat/chatclient.h b/examples/bluetooth/btchat/chatclient.h index a2f2eafc..25002f90 100644 --- a/examples/bluetooth/btchat/chatclient.h +++ b/examples/bluetooth/btchat/chatclient.h @@ -51,9 +51,10 @@ #ifndef CHATCLIENT_H #define CHATCLIENT_H -#include <qbluetoothserviceinfo.h> +#include <QtCore/qobject.h> -#include <QtCore/QObject> +#include <QtBluetooth/qbluetoothserviceinfo.h> +#include <QtBluetooth/qbluetoothsocket.h> QT_FORWARD_DECLARE_CLASS(QBluetoothSocket) @@ -65,7 +66,7 @@ class ChatClient : public QObject Q_OBJECT public: - explicit ChatClient(QObject *parent = 0); + explicit ChatClient(QObject *parent = nullptr); ~ChatClient(); void startClient(const QBluetoothServiceInfo &remoteService); @@ -78,13 +79,15 @@ signals: void messageReceived(const QString &sender, const QString &message); void connected(const QString &name); void disconnected(); + void socketErrorOccurred(const QString &errorString); private slots: void readSocket(); void connected(); + void onSocketErrorOccurred(QBluetoothSocket::SocketError); private: - QBluetoothSocket *socket; + QBluetoothSocket *socket = nullptr; }; //! [declaration] diff --git a/examples/bluetooth/btchat/chatserver.cpp b/examples/bluetooth/btchat/chatserver.cpp index e4293c75..d078a32c 100644 --- a/examples/bluetooth/btchat/chatserver.cpp +++ b/examples/bluetooth/btchat/chatserver.cpp @@ -50,16 +50,15 @@ #include "chatserver.h" -#include <qbluetoothserver.h> -#include <qbluetoothsocket.h> -#include <qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothserver.h> +#include <QtBluetooth/qbluetoothsocket.h> //! [Service UUID] static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); //! [Service UUID] ChatServer::ChatServer(QObject *parent) -: QObject(parent), rfcommServer(0) + : QObject(parent) { } @@ -75,7 +74,8 @@ void ChatServer::startServer(const QBluetoothAddress& localAdapter) //! [Create the server] rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); - connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); + connect(rfcommServer, &QBluetoothServer::newConnection, + this, QOverload<>::of(&ChatServer::clientConnected)); bool result = rfcommServer->listen(localAdapter); if (!result) { qWarning() << "Cannot bind chat server to" << localAdapter.toString(); @@ -85,18 +85,19 @@ void ChatServer::startServer(const QBluetoothAddress& localAdapter) //serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); - //! [Class Uuuid must contain at least 1 entry] + QBluetoothServiceInfo::Sequence profileSequence; QBluetoothServiceInfo::Sequence classId; - classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); + classId << QVariant::fromValue(quint16(0x100)); + profileSequence.append(QVariant::fromValue(classId)); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, - classId); + profileSequence); - classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid))); + classId.clear(); + classId << QVariant::fromValue(QBluetoothUuid(serviceUuid)); + classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); - //! [Class Uuuid must contain at least 1 entry] - //! [Service name, description and provider] serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server")); @@ -145,7 +146,7 @@ void ChatServer::stopServer() // Close server delete rfcommServer; - rfcommServer = 0; + rfcommServer = nullptr; } //! [stopServer] @@ -154,7 +155,7 @@ void ChatServer::sendMessage(const QString &message) { QByteArray text = message.toUtf8() + '\n'; - foreach (QBluetoothSocket *socket, clientSockets) + for (QBluetoothSocket *socket : qAsConst(clientSockets)) socket->write(text); } //! [sendMessage] @@ -166,8 +167,8 @@ void ChatServer::clientConnected() if (!socket) return; - connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); - connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); + connect(socket, &QBluetoothSocket::readyRead, this, &ChatServer::readSocket); + connect(socket, &QBluetoothSocket::disconnected, this, QOverload<>::of(&ChatServer::clientDisconnected)); clientSockets.append(socket); emit clientConnected(socket->peerName()); } diff --git a/examples/bluetooth/btchat/chatserver.h b/examples/bluetooth/btchat/chatserver.h index f7117b74..c4191db8 100644 --- a/examples/bluetooth/btchat/chatserver.h +++ b/examples/bluetooth/btchat/chatserver.h @@ -51,11 +51,10 @@ #ifndef CHATSERVER_H #define CHATSERVER_H -#include <qbluetoothserviceinfo.h> -#include <qbluetoothaddress.h> +#include <QtCore/qobject.h> -#include <QtCore/QObject> -#include <QtCore/QList> +#include <QtBluetooth/qbluetoothaddress.h> +#include <QtBluetooth/qbluetoothserviceinfo.h> QT_FORWARD_DECLARE_CLASS(QBluetoothServer) QT_FORWARD_DECLARE_CLASS(QBluetoothSocket) @@ -68,7 +67,7 @@ class ChatServer : public QObject Q_OBJECT public: - explicit ChatServer(QObject *parent = 0); + explicit ChatServer(QObject *parent = nullptr); ~ChatServer(); void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress()); @@ -88,7 +87,7 @@ private slots: void readSocket(); private: - QBluetoothServer *rfcommServer; + QBluetoothServer *rfcommServer = nullptr; QBluetoothServiceInfo serviceInfo; QList<QBluetoothSocket *> clientSockets; }; diff --git a/examples/bluetooth/btchat/main.cpp b/examples/bluetooth/btchat/main.cpp index 84b33ae4..5c7bbf75 100644 --- a/examples/bluetooth/btchat/main.cpp +++ b/examples/bluetooth/btchat/main.cpp @@ -50,7 +50,7 @@ #include "chat.h" -#include <QApplication> +#include <QtWidgets/qapplication.h> //#include <QtCore/QLoggingCategory> int main(int argc, char *argv[]) @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); Chat d; - QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit())); + QObject::connect(&d, &Chat::accepted, &app, &QApplication::quit); #ifdef Q_OS_ANDROID d.showMaximized(); diff --git a/examples/bluetooth/btchat/remoteselector.cpp b/examples/bluetooth/btchat/remoteselector.cpp index c1302f6b..2bd1efcc 100644 --- a/examples/bluetooth/btchat/remoteselector.cpp +++ b/examples/bluetooth/btchat/remoteselector.cpp @@ -51,14 +51,13 @@ #include "remoteselector.h" #include "ui_remoteselector.h" -#include <qbluetoothdeviceinfo.h> -#include <qbluetoothaddress.h> -#include <qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothservicediscoveryagent.h> QT_USE_NAMESPACE RemoteSelector::RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent) -: QDialog(parent), ui(new Ui::RemoteSelector) + : QDialog(parent), ui(new Ui::RemoteSelector) { ui->setupUi(this); diff --git a/examples/bluetooth/btchat/remoteselector.h b/examples/bluetooth/btchat/remoteselector.h index 9174cabd..54649ba9 100644 --- a/examples/bluetooth/btchat/remoteselector.h +++ b/examples/bluetooth/btchat/remoteselector.h @@ -51,13 +51,13 @@ #ifndef REMOTESELECTOR_H #define REMOTESELECTOR_H -#include <QDialog> +#include <QtWidgets/qdialog.h> -#include <qbluetoothuuid.h> -#include <qbluetoothserviceinfo.h> -#include <qbluetoothservicediscoveryagent.h> +#include <QtBluetooth/qbluetoothaddress.h> +#include <QtBluetooth/qbluetoothserviceinfo.h> +#include <QtBluetooth/qbluetoothuuid.h> -QT_FORWARD_DECLARE_CLASS(QModelIndex) +QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent) QT_FORWARD_DECLARE_CLASS(QListWidgetItem) QT_USE_NAMESPACE @@ -73,7 +73,7 @@ class RemoteSelector : public QDialog Q_OBJECT public: - explicit RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent = 0); + explicit RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent = nullptr); ~RemoteSelector(); void startDiscovery(const QBluetoothUuid &uuid); diff --git a/examples/bluetooth/heartrate-game/bluetoothbaseclass.h b/examples/bluetooth/heartrate-game/bluetoothbaseclass.h index 74fe4576..5a043b3d 100644 --- a/examples/bluetooth/heartrate-game/bluetoothbaseclass.h +++ b/examples/bluetooth/heartrate-game/bluetoothbaseclass.h @@ -60,7 +60,7 @@ class BluetoothBaseClass : public QObject Q_PROPERTY(QString info READ info WRITE setInfo NOTIFY infoChanged) public: - explicit BluetoothBaseClass(QObject *parent = 0); + explicit BluetoothBaseClass(QObject *parent = nullptr); QString error() const; void setError(const QString& error); diff --git a/examples/bluetooth/heartrate-game/connectionhandler.h b/examples/bluetooth/heartrate-game/connectionhandler.h index b4280978..9f5a42cc 100644 --- a/examples/bluetooth/heartrate-game/connectionhandler.h +++ b/examples/bluetooth/heartrate-game/connectionhandler.h @@ -63,7 +63,7 @@ class ConnectionHandler : public QObject Q_OBJECT public: - explicit ConnectionHandler(QObject *parent = 0); + explicit ConnectionHandler(QObject *parent = nullptr); bool alive() const; bool requiresAddressType() const; diff --git a/examples/bluetooth/heartrate-game/devicefinder.cpp b/examples/bluetooth/heartrate-game/devicefinder.cpp index 38f538e9..19ebee90 100644 --- a/examples/bluetooth/heartrate-game/devicefinder.cpp +++ b/examples/bluetooth/heartrate-game/devicefinder.cpp @@ -85,7 +85,7 @@ DeviceFinder::~DeviceFinder() void DeviceFinder::startSearch() { clearMessages(); - m_deviceHandler->setDevice(0); + m_deviceHandler->setDevice(nullptr); qDeleteAll(m_devices); m_devices.clear(); @@ -135,7 +135,7 @@ void DeviceFinder::scanFinished() m_devices.append(new DeviceInfo(QBluetoothDeviceInfo())); #endif - if (m_devices.size() == 0) + if (m_devices.isEmpty()) setError(tr("No Low Energy devices found.")); else setInfo(tr("Scanning done.")); @@ -148,10 +148,11 @@ void DeviceFinder::connectToService(const QString &address) { m_deviceDiscoveryAgent->stop(); - DeviceInfo *currentDevice = 0; - for (int i = 0; i < m_devices.size(); i++) { - if (((DeviceInfo*)m_devices.at(i))->getAddress() == address ) { - currentDevice = (DeviceInfo*)m_devices.at(i); + DeviceInfo *currentDevice = nullptr; + for (QObject *entry : qAsConst(m_devices)) { + auto device = qobject_cast<DeviceInfo *>(entry); + 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 2c54f550..6dbb5692 100644 --- a/examples/bluetooth/heartrate-game/devicefinder.h +++ b/examples/bluetooth/heartrate-game/devicefinder.h @@ -55,9 +55,10 @@ #include "bluetoothbaseclass.h" #include <QTimer> +#include <QVariant> #include <QBluetoothDeviceDiscoveryAgent> #include <QBluetoothDeviceInfo> -#include <QVariant> + class DeviceInfo; class DeviceHandler; @@ -70,7 +71,7 @@ class DeviceFinder: public BluetoothBaseClass Q_PROPERTY(QVariant devices READ devices NOTIFY devicesChanged) public: - DeviceFinder(DeviceHandler *handler, QObject *parent = 0); + DeviceFinder(DeviceHandler *handler, QObject *parent = nullptr); ~DeviceFinder(); bool scanning() const; diff --git a/examples/bluetooth/heartrate-game/devicehandler.cpp b/examples/bluetooth/heartrate-game/devicehandler.cpp index 3d263a4c..83a4fbbe 100644 --- a/examples/bluetooth/heartrate-game/devicehandler.cpp +++ b/examples/bluetooth/heartrate-game/devicehandler.cpp @@ -56,9 +56,6 @@ DeviceHandler::DeviceHandler(QObject *parent) : BluetoothBaseClass(parent), - m_control(0), - m_service(0), - m_currentDevice(0), m_foundHeartRateService(false), m_measuring(false), m_currentValue(0), @@ -107,7 +104,7 @@ void DeviceHandler::setDevice(DeviceInfo *device) if (m_control) { m_control->disconnectFromDevice(); delete m_control; - m_control = 0; + m_control = nullptr; } // Create new controller and connect it if device available @@ -115,7 +112,7 @@ void DeviceHandler::setDevice(DeviceInfo *device) // Make connections //! [Connect-Signals-1] - m_control = new QLowEnergyController(m_currentDevice->getDevice(), this); + m_control = QLowEnergyController::createCentral(m_currentDevice->getDevice(), this); //! [Connect-Signals-1] m_control->setRemoteAddressType(m_addressType); //! [Connect-Signals-2] @@ -181,7 +178,7 @@ void DeviceHandler::serviceScanDone() // Delete old service if available if (m_service) { delete m_service; - m_service = 0; + m_service = nullptr; } //! [Filter HeartRate service 2] @@ -240,15 +237,15 @@ void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, cons if (c.uuid() != QBluetoothUuid(QBluetoothUuid::HeartRateMeasurement)) return; - const quint8 *data = reinterpret_cast<const quint8 *>(value.constData()); - quint8 flags = data[0]; + auto data = reinterpret_cast<const quint8 *>(value.constData()); + quint8 flags = *data; //Heart Rate int hrvalue = 0; if (flags & 0x1) // HR 16 bit? otherwise 8 bit - hrvalue = (int)qFromLittleEndian<quint16>(data[1]); + hrvalue = static_cast<int>(qFromLittleEndian<quint16>(data[1])); else - hrvalue = (int)data[1]; + hrvalue = static_cast<int>(data[1]); addMeasurement(hrvalue); } @@ -276,7 +273,7 @@ void DeviceHandler::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, cons //disabled notifications -> assume disconnect intent m_control->disconnectFromDevice(); delete m_service; - m_service = 0; + m_service = nullptr; } } @@ -293,7 +290,7 @@ void DeviceHandler::disconnectService() m_control->disconnectFromDevice(); delete m_service; - m_service = 0; + m_service = nullptr; } } diff --git a/examples/bluetooth/heartrate-game/devicehandler.h b/examples/bluetooth/heartrate-game/devicehandler.h index 05984ea5..4fa2782b 100644 --- a/examples/bluetooth/heartrate-game/devicehandler.h +++ b/examples/bluetooth/heartrate-game/devicehandler.h @@ -54,8 +54,9 @@ #include "bluetoothbaseclass.h" #include <QDateTime> -#include <QVector> #include <QTimer> +#include <QVector> + #include <QLowEnergyController> #include <QLowEnergyService> @@ -82,7 +83,7 @@ public: }; Q_ENUM(AddressType) - DeviceHandler(QObject *parent = 0); + DeviceHandler(QObject *parent = nullptr); void setDevice(DeviceInfo *device); void setAddressType(AddressType type); @@ -127,10 +128,10 @@ private: private: void addMeasurement(int value); - QLowEnergyController *m_control; - QLowEnergyService *m_service; + QLowEnergyController *m_control = nullptr; + QLowEnergyService *m_service = nullptr; QLowEnergyDescriptor m_notificationDesc; - DeviceInfo *m_currentDevice; + DeviceInfo *m_currentDevice = nullptr; bool m_foundHeartRateService; bool m_measuring; diff --git a/examples/bluetooth/heartrate-game/deviceinfo.cpp b/examples/bluetooth/heartrate-game/deviceinfo.cpp index 3925ce66..4ed7f1b1 100644 --- a/examples/bluetooth/heartrate-game/deviceinfo.cpp +++ b/examples/bluetooth/heartrate-game/deviceinfo.cpp @@ -54,7 +54,7 @@ #include <QBluetoothUuid> DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &info): - QObject(), m_device(info) + m_device(info) { } diff --git a/examples/bluetooth/heartrate-game/main.cpp b/examples/bluetooth/heartrate-game/main.cpp index 33760e9d..099f82a7 100644 --- a/examples/bluetooth/heartrate-game/main.cpp +++ b/examples/bluetooth/heartrate-game/main.cpp @@ -49,9 +49,9 @@ ****************************************************************************/ #include <QGuiApplication> +#include <QLoggingCategory> #include <QQmlApplicationEngine> #include <QQmlContext> -#include <QtCore/QLoggingCategory> #include "connectionhandler.h" #include "devicefinder.h" @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) { - //QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); + QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); QGuiApplication app(argc, argv); ConnectionHandler connectionHandler; diff --git a/examples/bluetooth/heartrate-server/main.cpp b/examples/bluetooth/heartrate-server/main.cpp index f51a1694..ea01d07d 100644 --- a/examples/bluetooth/heartrate-server/main.cpp +++ b/examples/bluetooth/heartrate-server/main.cpp @@ -93,7 +93,7 @@ int main(int argc, char *argv[]) //! [Start Advertising] const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral()); - const QScopedPointer<QLowEnergyService> service(leController->addService(serviceData)); + QScopedPointer<QLowEnergyService> service(leController->addService(serviceData)); leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData, advertisingData); //! [Start Advertising] @@ -123,9 +123,12 @@ int main(int argc, char *argv[]) heartbeatTimer.start(1000); //! [Provide Heartbeat] - auto reconnect = [&leController, advertisingData]() { - leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData, - advertisingData); + auto reconnect = [&leController, advertisingData, &service, serviceData]() + { + service.reset(leController->addService(serviceData)); + if (!service.isNull()) + leController->startAdvertising(QLowEnergyAdvertisingParameters(), + advertisingData, advertisingData); }; QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect); 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 2ae30c16..82179db1 100644 --- a/examples/bluetooth/lowenergyscanner/device.cpp +++ b/examples/bluetooth/lowenergyscanner/device.cpp @@ -58,10 +58,10 @@ #include <qbluetoothservicediscoveryagent.h> #include <QDebug> #include <QList> +#include <QMetaEnum> #include <QTimer> -Device::Device(): - connected(false), controller(0), m_deviceScanState(false), randomAddress(false) +Device::Device() { //! [les-devicediscovery-1] discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); @@ -109,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()); } @@ -151,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()) { @@ -173,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), @@ -211,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(); @@ -228,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; @@ -256,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); } @@ -279,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(); @@ -318,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; @@ -326,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] @@ -341,8 +348,11 @@ void Device::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error) setUpdate("The Bluetooth adaptor is powered off, power it on before doing discovery."); else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError) setUpdate("Writing or reading from the device resulted in an error."); - else - setUpdate("An unknown error has occurred."); + else { + static QMetaEnum qme = discoveryAgent->metaObject()->enumerator( + discoveryAgent->metaObject()->indexOfEnumerator("Error")); + setUpdate("Error: " + QLatin1String(qme.valueToKey(error))); + } m_deviceScanState = false; emit devicesUpdated(); @@ -356,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 diff --git a/examples/nfc/annotatedurl/annotatedurl.cpp b/examples/nfc/annotatedurl/annotatedurl.cpp index 0e6b58fa..acd401f2 100644 --- a/examples/nfc/annotatedurl/annotatedurl.cpp +++ b/examples/nfc/annotatedurl/annotatedurl.cpp @@ -143,7 +143,7 @@ void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget * QPixmap pixmap; //! [handleMessage 2] - foreach (const QNdefRecord &record, message) { + for (const QNdefRecord &record : message) { if (record.isRecordType<QNdefNfcTextRecord>()) { QNdefNfcTextRecord textRecord(record); diff --git a/examples/nfc/ndefeditor/mainwindow.cpp b/examples/nfc/ndefeditor/mainwindow.cpp index 38865b88..869f1790 100644 --- a/examples/nfc/ndefeditor/mainwindow.cpp +++ b/examples/nfc/ndefeditor/mainwindow.cpp @@ -266,7 +266,7 @@ void MainWindow::ndefMessageRead(const QNdefMessage &message) { clearMessage(); - foreach (const QNdefRecord &record, message) { + for (const QNdefRecord &record : message) { if (record.isRecordType<QNdefNfcTextRecord>()) { addRecord<TextRecordEditor>(ui, record); } else if (record.isRecordType<QNdefNfcUriRecord>()) { |