From 8d93772285f06f3e5239183a7fd8306c9d090930 Mon Sep 17 00:00:00 2001 From: Gatis Paeglis Date: Wed, 3 Dec 2014 09:40:48 +0100 Subject: Wifi - c++/qml getting started guide and wifi doc update c++ getting started guide qml getting started guide documenting qml elements documenting c++ classes Task-number: QTEE-810 Change-Id: I669d11c65e5359fc9ec863b03b8b56ce2ef1151b Reviewed-by: Venugopal Shivashankar Reviewed-by: Kalle Viironen --- examples/examples.pro | 3 + examples/wifi/wifi-cpp/doc/images/wifi-cpp.jpg | Bin 0 -> 41866 bytes examples/wifi/wifi-cpp/doc/src/wifi-cpp.qdoc | 97 +++++++++ examples/wifi/wifi-cpp/main.cpp | 228 ++++++++++++++++++++ examples/wifi/wifi-cpp/wifi-cpp.pro | 9 + examples/wifi/wifi-qml/WifiConnectionHandler.qml | 51 +++++ examples/wifi/wifi-qml/WifiScanner.qml | 251 +++++++++++++++++++++++ examples/wifi/wifi-qml/doc/images/wifi-qml.jpg | Bin 0 -> 62077 bytes examples/wifi/wifi-qml/doc/src/wifi-qml.qdoc | 85 ++++++++ examples/wifi/wifi-qml/main.cpp | 30 +++ examples/wifi/wifi-qml/main.qml | 32 +++ examples/wifi/wifi-qml/qml.qrc | 7 + examples/wifi/wifi-qml/wifi-qml.pro | 11 + examples/wifi/wifi.pro | 4 + 14 files changed, 808 insertions(+) create mode 100644 examples/examples.pro create mode 100644 examples/wifi/wifi-cpp/doc/images/wifi-cpp.jpg create mode 100644 examples/wifi/wifi-cpp/doc/src/wifi-cpp.qdoc create mode 100644 examples/wifi/wifi-cpp/main.cpp create mode 100644 examples/wifi/wifi-cpp/wifi-cpp.pro create mode 100644 examples/wifi/wifi-qml/WifiConnectionHandler.qml create mode 100644 examples/wifi/wifi-qml/WifiScanner.qml create mode 100644 examples/wifi/wifi-qml/doc/images/wifi-qml.jpg create mode 100644 examples/wifi/wifi-qml/doc/src/wifi-qml.qdoc create mode 100644 examples/wifi/wifi-qml/main.cpp create mode 100644 examples/wifi/wifi-qml/main.qml create mode 100644 examples/wifi/wifi-qml/qml.qrc create mode 100644 examples/wifi/wifi-qml/wifi-qml.pro create mode 100644 examples/wifi/wifi.pro (limited to 'examples') diff --git a/examples/examples.pro b/examples/examples.pro new file mode 100644 index 0000000..18801a2 --- /dev/null +++ b/examples/examples.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS += wifi diff --git a/examples/wifi/wifi-cpp/doc/images/wifi-cpp.jpg b/examples/wifi/wifi-cpp/doc/images/wifi-cpp.jpg new file mode 100644 index 0000000..90feb20 Binary files /dev/null and b/examples/wifi/wifi-cpp/doc/images/wifi-cpp.jpg differ diff --git a/examples/wifi/wifi-cpp/doc/src/wifi-cpp.qdoc b/examples/wifi/wifi-cpp/doc/src/wifi-cpp.qdoc new file mode 100644 index 0000000..334dba7 --- /dev/null +++ b/examples/wifi/wifi-cpp/doc/src/wifi-cpp.qdoc @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +/*! + \title Getting Started with B2Qt.Wifi in C++ + \example wifi/wifi-cpp + \ingroup wifi-examples + \brief Guide to getting started with B2Qt.Wifi using C++. + + \section1 Preparing the Application + + Use the following \c include statement to access the C++ classes: + + \code + #include + \endcode + + Before building your application, add the following statement to your + \c .pro file to link against the B2Qt.Wifi library: + + \code + QT += b2qtwifi + \endcode + + This guide will demonstrate how to create a Qt Widget-based application + that utilizes the B2Qt.Wifi API to set up a wifi network connection. We + will start by looking at how to scan for wifi access points, and how to + display and process this data in the application. At the end of the guide + we will show how to connect directly to a known wifi network configuration. + + \image wifi-cpp.jpg + + \section1 Listing Wifi Networks + + First we need to set up QListView widget which we will use to list wifi + networks that can be detected by the device. The detected network access + points are packed as a list-based data model and can be retrieved using + QWifiManager::networks. Here we also set a custom item delegate and + connect to two QWifiManager signals. + + \snippet wifi/wifi-cpp/main.cpp 0 + + \section1 Creating a Delegate + + The Wifi network model has many data roles that describe the different + properties of Wifi network. This data can be used by an application to list + detailed network information and/or to create QWifiConfiguration objects. + In this example we are interested in the network name. In the paint() + method we check if the network name is equal to the currently active + network connection, and append appropriate network state information. + + \snippet wifi/wifi-cpp/main.cpp 1 + + \section1 Connecting to a Selected Network + + On press of the \uicontrol Connect button, connetToNetwork() slot gets + invoked. In this slot we query network properties for the selected network + and create a QWifiConfiguration object, which we later pass to the + QWifiManager::connect function to set up a connection. During this + operation any changes in the network state is reported by QWifiManager + asynchronously. + + \snippet wifi/wifi-cpp/main.cpp 2 + + We use QWifiManager::NetworkState change event handler to trigger the + repainting of the delegate. This way, we can present a current network + state to the user. + + \snippet wifi/wifi-cpp/main.cpp 4 + + \section1 Connecting To a Known Network + + If you already know the network configuration beforehand, you can skip the + network scanning, listing and selection steps. This can be a valid use-case + for devices that do not change their physical location. + + QWifiManager::BackendState change events are reported asynchronously, so we + must connect the signal to a slot that connects to the network access point + after the backend initialization is complete. + + \snippet wifi/wifi-cpp/main.cpp 3 + */ diff --git a/examples/wifi/wifi-cpp/main.cpp b/examples/wifi/wifi-cpp/main.cpp new file mode 100644 index 0000000..54a35e9 --- /dev/null +++ b/examples/wifi/wifi-cpp/main.cpp @@ -0,0 +1,228 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#include +#include +#include + +class NetworkDelegate : public QStyledItemDelegate +{ + Q_OBJECT +//! [1] +public: + NetworkDelegate(QObject *parent = 0) + : QStyledItemDelegate(parent) + { + m_wifiManager = QWifiManager::instance(); + } + + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const + { + QStyledItemDelegate::paint(painter, option, index); + painter->save(); + QString ssid = qvariant_cast(index.data(QWifiManager::SSID)); + if (ssid == m_wifiManager->currentSSID()) + ssid += networkStateText(); + painter->drawText(option.rect, Qt::AlignVCenter, ssid); + painter->restore(); + } + + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const + { + QFont font = QApplication::font(); + QFontMetrics fm(font); + return QSize(option.rect.width(), fm.height() * 2); + } + +protected: + QString networkStateText() const + { + QWifiManager::NetworkState networkState = m_wifiManager->networkState(); + switch (networkState) { + case QWifiManager::Disconnected: + return QStringLiteral(""); + break; + case QWifiManager::Authenticating: + return QStringLiteral(" (authenticating)"); + break; + case QWifiManager::HandshakeFailed: + return QStringLiteral(" (handshake failed)"); + break; + case QWifiManager::ObtainingIPAddress: + return QStringLiteral(" (obtaining IP address)"); + break; + case QWifiManager::DhcpRequestFailed: + return QStringLiteral(" (dhcp request failed)"); + break; + case QWifiManager::Connected: + return QStringLiteral(" (connected)"); + break; + } + } +//! [1] +private: + QWifiManager *m_wifiManager; +}; + +class WifiSettings : public QWidget +{ + Q_OBJECT +public: + //! [0] + WifiSettings(QWidget *parent = 0) + : QWidget(parent) + , m_listView(new QListView(this)) + , m_networkDelegate(new NetworkDelegate(this)) + { + m_wifiManager = QWifiManager::instance(); + m_listView->setModel(m_wifiManager->networks()); + m_listView->setItemDelegate(m_networkDelegate); + + connect(m_wifiManager, &QWifiManager::backendStateChanged, + this, &WifiSettings::handleBackendStateChanged); + connect(m_wifiManager, &QWifiManager::networkStateChanged, + this, &WifiSettings::handleNetworkStateChanged); + + setupWidgets(); + } + //! [0] +protected: + void setupWidgets() + { + m_backendStateReporter = new QLabel(this); + handleBackendStateChanged(m_wifiManager->backendState()); + m_passwordInput = new QLineEdit(this); + m_passwordInput->setPlaceholderText("Enter Password"); + + QPushButton *connectButton = new QPushButton("Connect", this); + QPushButton *disconnectButton = new QPushButton("Disconnect", this); + QPushButton *startBackendButton = new QPushButton("Switch On", this); + QPushButton *stopBackendButton = new QPushButton("Switch Off", this); + + connect(startBackendButton, &QPushButton::clicked, m_wifiManager, &QWifiManager::start); + connect(stopBackendButton, &QPushButton::clicked, m_wifiManager, &QWifiManager::stop); + connect(connectButton, &QPushButton::clicked, this, &WifiSettings::connectToNetwork); + connect(disconnectButton, &QPushButton::clicked, m_wifiManager, &QWifiManager::disconnect); + + QGridLayout *grid = new QGridLayout(this); + grid->addWidget(connectButton, 0, 0); + grid->addWidget(disconnectButton, 0, 1); + grid->addWidget(startBackendButton, 1, 0); + grid->addWidget(stopBackendButton, 1, 1); + grid->addWidget(m_listView, 2, 0, 1, 2); + grid->addWidget(m_passwordInput, 3, 0, 1, 2); + grid->addWidget(m_backendStateReporter, 4, 0, 1, 2); + setLayout(grid); + } + + +protected slots: + void handleBackendStateChanged(QWifiManager::BackendState state) + { + switch (state) { + case QWifiManager::Running: + m_wifiManager->setScanning(true); + m_backendStateReporter->setText("wifi backend state: running<\b>"); + break; + case QWifiManager::NotRunning: + m_wifiManager->setScanning(false); + m_backendStateReporter->setText("wifi backend state: stopped<\b>"); + break; + case QWifiManager::Initializing: + m_backendStateReporter->setText("wifi backend state: initializing<\b>"); + break; + case QWifiManager::Terminating: + m_backendStateReporter->setText("wifi backend state: terminating<\b>"); + break; + } + } + //! [4] + void handleNetworkStateChanged(QWifiManager::NetworkState state) + { + m_listView->viewport()->repaint(); + } + //! [4] + //! [2] + void connectToNetwork() + { + QModelIndex index = m_listView->currentIndex(); + QWifiConfiguration config; + if (index.isValid()) { + QString ssid = qvariant_cast(index.data(QWifiManager::SSID)); + config.setSsid(ssid); + config.setPassphrase(m_passwordInput->text()); + m_wifiManager->connect(&config); + } + } + //! [2] +private: + QWifiManager *m_wifiManager; + QListView *m_listView; + NetworkDelegate *m_networkDelegate; + QLabel *m_backendStateReporter; + QLineEdit *m_passwordInput; +}; + +//! [3] +class WifiConnectionHandler : public QObject +{ + Q_OBJECT +public: + WifiConnectionHandler() + { + // replace with a valid network configuration + m_config.setSsid("my-local-wifi"); + m_config.setPassphrase("helloworld123"); + m_config.setProtocol("WPA"); + m_manager = QWifiManager::instance(); + if (m_manager->backendState() == QWifiManager::Running) { + m_manager->connect(&m_config); + } else { + connect(m_manager, &QWifiManager::backendStateChanged, + this, &WifiConnectionHandler::connectToNetwork); + m_manager->start(); + } + } + +protected slots: + void connectToNetwork(QWifiManager::BackendState state) + { + if (state == QWifiManager::Running) + m_manager->connect(&m_config); + } + +private: + QWifiManager *m_manager; + QWifiConfiguration m_config; +}; +//! [3] + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + WifiSettings wifiSettingsWindow; + wifiSettingsWindow.show(); + + // disable the above 2 lines before enabling this + // WifiConnectionHandler connectionHandler; + + return a.exec(); +} + +#include "main.moc" diff --git a/examples/wifi/wifi-cpp/wifi-cpp.pro b/examples/wifi/wifi-cpp/wifi-cpp.pro new file mode 100644 index 0000000..f457df3 --- /dev/null +++ b/examples/wifi/wifi-cpp/wifi-cpp.pro @@ -0,0 +1,9 @@ +QT += core widgets b2qtwifi + +TARGET = wifi-cpp +TEMPLATE = app + +target.path = /data/user/qt/$$TARGET +INSTALLS += target + +SOURCES += main.cpp diff --git a/examples/wifi/wifi-qml/WifiConnectionHandler.qml b/examples/wifi/wifi-qml/WifiConnectionHandler.qml new file mode 100644 index 0000000..f3f2c10 --- /dev/null +++ b/examples/wifi/wifi-qml/WifiConnectionHandler.qml @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +import QtQuick 2.3 +import B2Qt.Wifi 1.0 + +//! [0] +Item { + WifiConfiguration { + id: localConfig + ssid: "network-for-my-device" + passphrase: "password123" + protocol: "WPA2" + } + + Connections { + target: WifiManager + onBackendStateChanged: { + if (WifiManager.backendState === WifiManager.Running) + WifiManager.connect(localConfig) + } + onNetworkStateChanged: { + if (WifiManager.networkState === WifiManager.Connected) + print("successfully connected to: " + WifiManager.currentSSID) + } + } + + Component.onCompleted: { + if (WifiManager.backendState === WifiManager.Running) { + WifiManager.connect(localConfig) + } else { + WifiManager.start() + } + } +} +//! [0] diff --git a/examples/wifi/wifi-qml/WifiScanner.qml b/examples/wifi/wifi-qml/WifiScanner.qml new file mode 100644 index 0000000..f5ebec2 --- /dev/null +++ b/examples/wifi/wifi-qml/WifiScanner.qml @@ -0,0 +1,251 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +import QtQuick 2.3 +import QtQuick.Controls 1.2 +import B2Qt.Wifi 1.0 + +Item { + anchors.fill: parent + + Binding { + target: WifiManager + property: "scanning" + value: networkView.visible + } + + Button { + id: wifiOnOffButton + anchors.top: parent.top + anchors.topMargin: 20 + anchors.left: parent.left + anchors.right: parent.right + onClicked: { + if (WifiManager.backendState === WifiManager.Running) { + if (networkView.visible) + networkView.visible = false + WifiManager.stop() + } else if (WifiManager.backendState === WifiManager.NotRunning) { + WifiManager.start() + } + } + + Component.onCompleted: updateButtonText(WifiManager.backendState) + Connections { + target: WifiManager + onBackendStateChanged: wifiOnOffButton.updateButtonText(backendState) + } + + function updateButtonText(backendState) + { + if (backendState === WifiManager.Initializing) + wifiOnOffButton.text = "Initializing..." + if (backendState === WifiManager.Terminating) + wifiOnOffButton.text = "Terminating..." + if (backendState === WifiManager.NotRunning) + wifiOnOffButton.text = "Switch On" + if (backendState === WifiManager.Running) + wifiOnOffButton.text = "Switch Off" + } + } + + Button { + id: listNetworksButton + anchors.top: wifiOnOffButton.bottom + anchors.topMargin: 30 + anchors.left: parent.left + anchors.right: parent.right + visible: WifiManager.backendState === WifiManager.Running + text: networkView.visible ? "Hide wifi networks" + : "List available wifi networks" + onClicked: networkView.visible = !networkView.visible + } + //! [0] + ListView { + id: networkView + model: WifiManager.networks + delegate: listDelegate + implicitHeight: 800 + anchors.top: listNetworksButton.bottom + anchors.topMargin: 30 + anchors.left: parent.left + anchors.right: parent.right + visible: false + clip: true + + property string networkStateText: "" + property QtObject expandedNetworkBox: null + property bool hasExpandedNetworkBox: expandedNetworkBox !== null + + function setNetworkStateText(networkState) { + if (networkState === WifiManager.ObtainingIPAddress) + networkView.networkStateText = " (obtaining ip..)" + else if (networkState === WifiManager.DhcpRequestFailed) + networkView.networkStateText = " (dhcp request failed)" + else if (networkState === WifiManager.Connected) + networkView.networkStateText = " (connected)" + else if (networkState === WifiManager.Authenticating) + networkView.networkStateText = " (authenticating..)" + else if (networkState === WifiManager.HandshakeFailed) + networkView.networkStateText = " (wrong password)" + else if (networkState === WifiManager.Disconnected) + networkView.networkStateText = "" + } + + Connections { + target: WifiManager + onNetworkStateChanged: networkView.setNetworkStateText(networkState) + } + } + //! [0] + //! [2] + WifiConfiguration { id: config } + //! [2] + Component { + id: listDelegate + Rectangle { + id: networkBox + property bool expanded: false + property bool isCurrentNetwork: WifiManager.currentSSID === ssid + property bool connected: isCurrentNetwork && WifiManager.networkState === WifiManager.Connected + property int notExpandedHeight: ssidLabel.height + bssidLabel.height + 20 + property int expandedHeight: notExpandedHeight + passwordInput.height + connectionButton.height + 54 + property int connectedExpandedHeight: notExpandedHeight + connectionButton.height + 30 + height: expanded ? (connected ? connectedExpandedHeight : expandedHeight) : notExpandedHeight + width: parent.width + clip: true + color: "#5C5C5C" + border.color: "black" + border.width: 1 + + Component.onDestruction: if (expanded) networkView.expandedNetworkBox = null + onHeightChanged: if (expanded) networkView.positionViewAtIndex(index, ListView.Contain) + + Behavior on height { NumberAnimation { duration: 500; easing.type: Easing.InOutCubic } } + //! [1] + Text { + id: ssidLabel + anchors.top: parent.top + anchors.left: parent.left + anchors.margins: 5 + anchors.leftMargin: 10 + font.pixelSize: 26 + font.bold: true + color: "#E6E6E6" + text: isCurrentNetwork ? ssid + networkView.networkStateText : ssid + Component.onCompleted: networkView.setNetworkStateText(WifiManager.networkState) + } + + Text { + id: bssidLabel + anchors.top: ssidLabel.bottom + anchors.left: parent.left + anchors.margins: 5 + anchors.leftMargin: 30 + text: bssid + color: "#E6E6E6" + font.pixelSize: ssidLabel.font.pixelSize * 0.8 + } + + Text { + id: flagsLabel + anchors.top: bssidLabel.top + anchors.left: bssidLabel.right + anchors.leftMargin: 35 + text: (supportsWPA2 ? "WPA2 " : "") + + (supportsWPA ? "WPA " : "") + + (supportsWEP ? "WEP " : "") + + (supportsWPS ? "WPS " : ""); + color: "#E6E6E6" + font.pixelSize: ssidLabel.font.pixelSize * 0.8 + font.italic: true + } + + Rectangle { + id: signalStrengthBar + height: 15 + radius: 20 + antialiasing: true + anchors.margins: 10 + anchors.right: parent.right + anchors.top: parent.top + color: "#BF8888" + border.color: "#212126" + property int strengthBarWidth: Math.max(100 + signalStrength, 0) / 100 * parent.width + onStrengthBarWidthChanged: { + if (strengthBarWidth > parent.width * 0.55) + signalStrengthBar.width = parent.width * 0.55 + else + signalStrengthBar.width = strengthBarWidth + } + } + //! [1] + MouseArea { + anchors.fill: parent + onClicked: handleNetworkBoxExpanding() + } + + function handleNetworkBoxExpanding() + { + expanded = !expanded + if (expanded) { + if (networkView.hasExpandedNetworkBox) + networkView.expandedNetworkBox.expanded = false + networkView.expandedNetworkBox = networkBox + } else { + networkView.expandedNetworkBox = null + } + } + + TextField { + id: passwordInput + anchors.top: flagsLabel.bottom + anchors.topMargin: 15 + anchors.horizontalCenter: parent.horizontalCenter + width: parent.width * 0.36 + height: connectionButton.height * 1.1 + placeholderText: "Enter Password" + visible: !connected + font.pixelSize: 16 + echoMode: TextInput.Password + inputMethodHints: Qt.ImhNoPredictiveText + } + + Button { + id: connectionButton + y: connected ? passwordInput.y + : passwordInput.y + passwordInput.height + 10 + width: passwordInput.width + anchors.horizontalCenter: parent.horizontalCenter + text: connected ? "Disconnect" : "Connect" + //! [3] + onClicked: { + if (connected) { + WifiManager.disconnect() + } else { + config.ssid = ssid; + config.passphrase = passwordInput.text + if (!WifiManager.connect(config)) + print("failed to connect: " + WifiManager.lastError) + } + } + //! [3] + } + } + } +} diff --git a/examples/wifi/wifi-qml/doc/images/wifi-qml.jpg b/examples/wifi/wifi-qml/doc/images/wifi-qml.jpg new file mode 100644 index 0000000..a0780c2 Binary files /dev/null and b/examples/wifi/wifi-qml/doc/images/wifi-qml.jpg differ diff --git a/examples/wifi/wifi-qml/doc/src/wifi-qml.qdoc b/examples/wifi/wifi-qml/doc/src/wifi-qml.qdoc new file mode 100644 index 0000000..ba77c1e --- /dev/null +++ b/examples/wifi/wifi-qml/doc/src/wifi-qml.qdoc @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +/*! + + \title Getting Started with B2Qt.Wifi in QML + \example wifi/wifi-qml + \ingroup wifi-examples + \brief Guide to getting started with B2Qt.Wifi using QML. + + \section1 Preparing the Application + + Use the following \c import statement in the QML files to access the B2Qt.Wifi QML types: + + \code + import B2Qt.Wifi 1.0 + \endcode + + This guide will demonstrate how to create a QML based application that utilizes + B2Qt.Wifi API to set up a wifi network connection. We will start by looking at how to scan the + surroundings for wifi access points and how to display and process this data in the application. + At the end of the guide we will show how to connect directly to a known wifi network configuration. + + \image wifi-qml.jpg + + \section1 Listing Wifi Networks + + First we need to set up ListView which we will use to list wifi networks + that can be sensed by the device. The sensed network access points are packed as a list-based + data model and can be retrieved from WifiManager::networks. Here we also set a custom item + delegate and connect to WifiManager::networkStateChanged signal. + + \snippet wifi/wifi-qml/WifiScanner.qml 0 + + \section1 Creating a Delegate + + The wifi network model has many data roles that describe the different properties of wifi networks. + This data can be used by an application to list detailed network information and/or to set up + WifiConfiguration objects. We use these network data roles in our delegate for listing + ssid, bssid, supported security protocols and for signal strengh representation. + + \snippet wifi/wifi-qml/WifiScanner.qml 1 + + \section1 Connecting To a Selected Network + + WifiConfiguration element will be used to describe the network that we want to connect to, + selected from the network list. + + \snippet wifi/wifi-qml/WifiScanner.qml 2 + + When \uicontrol Connect button is clicked we set the network name and password properties on + the \c config and pass it to WifiManager::connect, which sets up a wifi connection behind-the-scenes. + During this operation or whenever there are changes in the network state, QWifiManager provides + asynchronous QWifiManager::NetworkState change events. + + \snippet wifi/wifi-qml/WifiScanner.qml 3 + + \section1 Connecting To a Known Network + + If you are not interested in scanning the environment for wifi network access points and you already + know the network configuration beforehand, the network scanning, listing and selection steps can be + entirely skipped. This can be a valid use-case for devices that won't be changing their physical location. + + QWifiManager::BackendState change events are delivered asynchronously. Therefore we have to add a signal handler + that will connect to the network access point after the backend initialization process has been completed, + if the backend is not already in the initialized state at the time of running this code. + + \snippet wifi/wifi-qml/WifiConnectionHandler.qml 0 + + */ diff --git a/examples/wifi/wifi-qml/main.cpp b/examples/wifi/wifi-qml/main.cpp new file mode 100644 index 0000000..32c6b74 --- /dev/null +++ b/examples/wifi/wifi-qml/main.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#include +#include + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + + return app.exec(); +} diff --git a/examples/wifi/wifi-qml/main.qml b/examples/wifi/wifi-qml/main.qml new file mode 100644 index 0000000..9c8726d --- /dev/null +++ b/examples/wifi/wifi-qml/main.qml @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +import QtQuick 2.3 +import QtQuick.Window 2.2 + +Window { + visible: true + width: Screen.width + height: Screen.height + color: "#D9D9D9" + + WifiScanner {} + + // disable the above line before enabling WifiConnectionHandler + // WifiConnectionHandler {} +} diff --git a/examples/wifi/wifi-qml/qml.qrc b/examples/wifi/wifi-qml/qml.qrc new file mode 100644 index 0000000..3c36973 --- /dev/null +++ b/examples/wifi/wifi-qml/qml.qrc @@ -0,0 +1,7 @@ + + + main.qml + WifiScanner.qml + WifiConnectionHandler.qml + + diff --git a/examples/wifi/wifi-qml/wifi-qml.pro b/examples/wifi/wifi-qml/wifi-qml.pro new file mode 100644 index 0000000..70fc76a --- /dev/null +++ b/examples/wifi/wifi-qml/wifi-qml.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +QT += qml quick + +SOURCES += main.cpp + +RESOURCES += qml.qrc + +target.path = /data/user/qt +export(target.path) +INSTALLS += target diff --git a/examples/wifi/wifi.pro b/examples/wifi/wifi.pro new file mode 100644 index 0000000..9e03f0e --- /dev/null +++ b/examples/wifi/wifi.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS += wifi-cpp \ + wifi-qml -- cgit v1.2.3