我们知道对于很多的网路应用来说,网路的连接信息对于我们来说非常重要。我们有必要对网路的连接信息进行监测。一旦网路连接断开,我们需要提醒用户或做一些处理。尽管目前在我们的官方网站上,还没有有关它的正式的资料,但是我们还是可以通下面的方法来得到它的API。
我们可以利用SDK的模版来创建一个最简单的QML应用。因为我们要使用connectivity,所以我们必须加入“connectivity”的security policy。
在我们的开发者网站上虽然也有NetworkStatus的介绍,但是可能并不是很全,我们可以使用如下的命令来得到更多的信息:
$qmlplugindump Ubuntu.Connectivity 1.0
import QtQuick.tooling 1.1
// This file describes the plugin-supplied types contained in the library.
// It is used for QML tooling purposes only.
//
// This file was auto-generated by:
// 'qmlplugindump Ubuntu.Connectivity 1.0'
Module {
Component {
name: "connectivityqt::Connectivity"
prototype: "QObject"
exports: ["Connectivity 1.0", "NetworkingStatus 1.0"]
isCreatable: false
isSingleton: true
exportMetaObjectRevisions: [0, 0]
Enum {
name: "Limitations"
values: {
"Bandwith": 0
}
}
Enum {
name: "Status"
values: {
"Offline": 0,
"Connecting": 1,
"Online": 2
}
}
Property { name: "flightMode"; type: "bool" }
Property { name: "FlightMode"; type: "bool" }
Property { name: "online"; type: "bool"; isReadonly: true }
Property { name: "limitedBandwith"; type: "bool"; isReadonly: true }
Property { name: "Limitations"; type: "QVector<Limitations>"; isReadonly: true }
Property { name: "Status"; type: "connectivityqt::Connectivity::Status"; isReadonly: true }
Property { name: "wifiEnabled"; type: "bool" }
Property { name: "WifiEnabled"; type: "bool" }
Property { name: "UnstoppableOperationHappening"; type: "bool"; isReadonly: true }
Property { name: "FlightModeSwitchEnabled"; type: "bool"; isReadonly: true }
Property { name: "flightModeSwitchEnabled"; type: "bool"; isReadonly: true }
Property { name: "WifiSwitchEnabled"; type: "bool"; isReadonly: true }
Property { name: "wifiSwitchEnabled"; type: "bool"; isReadonly: true }
Property { name: "HotspotSwitchEnabled"; type: "bool"; isReadonly: true }
Property { name: "hotspotSwitchEnabled"; type: "bool"; isReadonly: true }
Property { name: "hotspotSsid"; type: "QByteArray" }
Property { name: "hotspotPassword"; type: "string" }
Property { name: "hotspotEnabled"; type: "bool" }
Property { name: "hotspotMode"; type: "string" }
Property { name: "hotspotStored"; type: "bool"; isReadonly: true }
Property { name: "Initialized"; type: "bool"; isReadonly: true }
Signal {
name: "flightModeUpdated"
Parameter { type: "bool" }
}
Signal {
name: "onlineUpdated"
Parameter { name: "value"; type: "bool" }
}
Signal {
name: "limitedBandwithUpdated"
Parameter { name: "value"; type: "bool" }
}
Signal {
name: "limitationsUpdated"
Parameter { type: "QVector<Limitations>" }
}
Signal {
name: "statusUpdated"
Parameter { name: "value"; type: "connectivityqt::Connectivity::Status" }
}
Signal {
name: "wifiEnabledUpdated"
Parameter { type: "bool" }
}
Signal {
name: "unstoppableOperationHappeningUpdated"
Parameter { type: "bool" }
}
Signal {
name: "flightModeSwitchEnabledUpdated"
Parameter { type: "bool" }
}
Signal {
name: "wifiSwitchEnabledUpdated"
Parameter { type: "bool" }
}
Signal {
name: "hotspotSwitchEnabledUpdated"
Parameter { type: "bool" }
}
Signal {
name: "hotspotSsidUpdated"
Parameter { name: "name"; type: "QByteArray" }
}
Signal {
name: "hotspotPasswordUpdated"
Parameter { name: "password"; type: "string" }
}
Signal {
name: "hotspotEnabledUpdated"
Parameter { type: "bool" }
}
Signal {
name: "hotspotModeUpdated"
Parameter { name: "mode"; type: "string" }
}
Signal {
name: "hotspotStoredUpdated"
Parameter { type: "bool" }
}
Signal {
name: "reportError"
Parameter { name: "reason"; type: "int" }
}
Signal { name: "initialized" }
Method {
name: "setFlightMode"
Parameter { name: "enabled"; type: "bool" }
}
Method {
name: "setwifiEnabled"
Parameter { name: "enabled"; type: "bool" }
}
Method {
name: "setHotspotEnabled"
Parameter { name: "active"; type: "bool" }
}
Method {
name: "setHotspotSsid"
Parameter { name: "ssid"; type: "QByteArray" }
}
Method {
name: "setHotspotPassword"
Parameter { name: "password"; type: "string" }
}
Method {
name: "setHotspotMode"
Parameter { name: "mode"; type: "string" }
}
}
}
import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Connectivity 1.0
import Ubuntu.Components.ListItems 0.1 as ListItem
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
id: root
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "networkstatus.ubuntu"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(50)
height: units.gu(75)
property real margins: units.gu(2)
property real buttonWidth: units.gu(9)
Connections {
target: NetworkingStatus
// full status can be retrieved from the base C++ class
// status property
onStatusUpdated: {
if (value === NetworkingStatus.Offline) {
console.log("Status: Offline")
}
if (value === NetworkingStatus.Connecting) {
console.log("Status: Connecting")
}
if (value === NetworkingStatus.Online) {
console.log("Status: Online")
}
online.subText = NetworkingStatus.online ? "yes" : "no";
}
onWifiEnabledUpdated: {
console.log("wifiEnabledUpdated: " + NetworkingStatus.WifiEnabled);
}
onFlightModeUpdated: {
console.log("FlightModeUpdated: " + NetworkingStatus.flightMode);
flightMode.subText = NetworkingStatus.flightMode ? "yes" : "no"
}
onHotspotEnabledUpdated: {
console.log("hotspotEnabledUpdated: " + NetworkingStatus.hotspotEnabled)
hotspotEnabled.subText = etworkingStatus.hotspotEnabled ? "yes" : "no"
}
onUnstoppableOperationHappeningUpdated: {
console.log("unstoppableOperationHappeningUpdated: " + NetworkingStatus.UnstoppableOperationHappening);
}
onWifiSwitchEnabledUpdated: {
console.log("wifiSwitchEnabledUpdated: " + NetworkingStatus.wifiSwitchEnabled);
}
onFlightModeSwitchEnabledUpdated: {
console.log("flightModeSwitchEnabledUpdated: " + NetworkingStatus.flightModeSwitchEnabled);
}
onHotspotSsidUpdated: {
console.log("hotspotSsidUpdated: " + NetworkingStatus.hotspotSsid);
}
onHotspotPasswordUpdated: {
console.log("hotspotPasswordUpdated: " + NetworkingStatus.password);
hotspotPassword.subText = NetworkingStatus.password;
}
onHotspotStoredUpdated: {
console.log("hotspotStoredUpdated: " + NetworkingStatus.hotspotStored);
}
onInitialized: {
console.log("initialized");
}
}
function getStatus(status) {
console.log("status: " + status);
switch(status) {
case 0:
return "Offline";
case 1:
return "Connecting";
case 2:
return "Online"
}
}
Page {
title: i18n.tr("Networking Status")
ListModel {
id: mymodel
}
Flickable {
id: scrollWidget
anchors.fill: parent
contentHeight: contentItem.childrenRect.height
boundsBehavior: (contentHeight > root.height) ? Flickable.DragAndOvershootBounds :
Flickable.StopAtBounds
flickableDirection: Flickable.VerticalFlick
Column {
id: layout
anchors.left: parent.left
anchors.right: parent.right
spacing: units.gu(1)
ListItem.Subtitled {
id: online
text: i18n.tr("online")
subText: NetworkingStatus.online ? "yes" : "no"
}
ListItem.Subtitled {
id: bandwidth
text: i18n.tr("Bandwith")
subText: NetworkingStatus.limitedBandwith ? "Bandwith limited" : "Bandwith not limited"
}
ListItem.Subtitled {
id: flightMode
text: i18n.tr("flight mode")
subText: NetworkingStatus.flightMode ? "yes" : "no"
}
ListItem.Subtitled {
id: status
text: i18n.tr("Status")
subText: getStatus(NetworkingStatus.Status)
}
ListItem.Subtitled {
id: wifiEnabled
text: i18n.tr("wifiEnabled")
subText: NetworkingStatus.wifiEnabled ? "yes" : "no"
}
ListItem.Subtitled {
id: flightModeSwichEnabled
text: i18n.tr("FlightModeSwitchEnabled")
subText: NetworkingStatus.FlightModeSwitchEnabled ? "yes" : "no"
}
ListItem.Subtitled {
text: i18n.tr("WifiSwitchEnabled")
subText: NetworkingStatus.WifiSwitchEnabled ? "yes" : "no"
}
ListItem.Subtitled {
id: hotspotSwitchEnabled
text: i18n.tr("HotspotSwitchEnabled")
subText: NetworkingStatus.HotspotSwitchEnabled ? "yes" : "no"
}
ListItem.Subtitled {
id: hotspotEnabled
text: i18n.tr("hotspotEnabled")
subText: NetworkingStatus.hotspotEnabled ? "yes" : "no"
}
ListItem.Subtitled {
id: hotspotPassword
text: i18n.tr("hotspotPassword")
subText: NetworkingStatus.hotspotPassword
}
ListItem.Subtitled {
id: hotspotStored
text: i18n.tr("hotspotStored")
subText: NetworkingStatus.hotspotStored ? "yes" : "no"
}
ListItem.Subtitled {
id: initialized
text: i18n.tr("Initialized")
subText: NetworkingStatus.Initialized ? "yes" : "no"
}
}
}
}
}
最终运行我们的应用,我们可以看到在wifi断开和连接上时的状态变化:
测试代码在:https://github.com/liu-xiao-guo/networkstatus