mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-19 05:52:49 +08:00
CB-8384 Network status change support on Windows
This commit is contained in:
parent
d8a714757f
commit
9710603feb
@ -24,67 +24,65 @@
|
|||||||
var cordova = require('cordova');
|
var cordova = require('cordova');
|
||||||
var Connection = require('./Connection');
|
var Connection = require('./Connection');
|
||||||
|
|
||||||
|
var winNetConn = Windows.Networking.Connectivity;
|
||||||
|
var networkInfo = winNetConn.NetworkInformation;
|
||||||
|
var networkCostInfo = winNetConn.NetworkCostType;
|
||||||
|
var networkConnectivityInfo = winNetConn.NetworkConnectivityLevel;
|
||||||
|
var networkAuthenticationInfo = winNetConn.NetworkAuthenticationType;
|
||||||
|
var networkEncryptionInfo = winNetConn.NetworkEncryptionType;
|
||||||
|
|
||||||
|
function getCurrrentConnectionType() {
|
||||||
|
|
||||||
|
var profile = networkInfo.getInternetConnectionProfile();
|
||||||
|
|
||||||
|
if (!profile) {
|
||||||
|
return Connection.NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
var conLevel = profile.getNetworkConnectivityLevel();
|
||||||
|
var interfaceType = profile.networkAdapter.ianaInterfaceType;
|
||||||
|
|
||||||
|
// since we use this to detect whether we are online or offline we do check agains InternetAccess
|
||||||
|
// localAccess (airplane mode as an example) or constrainedInternetAccess mean there is no access to the internet available
|
||||||
|
// https://msdn.microsoft.com/library/windows/apps/windows.networking.connectivity.networkconnectivitylevel.aspx
|
||||||
|
if (conLevel != Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess) {
|
||||||
|
return Connection.NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
var connectionType;
|
||||||
|
|
||||||
|
switch (interfaceType) {
|
||||||
|
case 71:
|
||||||
|
connectionType = Connection.WIFI;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
connectionType = Connection.ETHERNET;
|
||||||
|
break;
|
||||||
|
case 243: // (3GPP WWAN) // Fallthrough is intentional
|
||||||
|
case 244: // (3GPP2 WWAN)
|
||||||
|
connectionType = Connection.CELL_3G;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
connectionType = Connection.UNKNOWN;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return connectionType;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
getConnectionInfo:function(win,fail,args)
|
getConnectionInfo:function(win,fail,args)
|
||||||
{
|
{
|
||||||
console.log("NetworkStatusProxy::getConnectionInfo");
|
var reportConnectionInfoOnce = function () {
|
||||||
var winNetConn = Windows.Networking.Connectivity;
|
win(getCurrrentConnectionType(), { keepCallback: true });
|
||||||
var networkInfo = winNetConn.NetworkInformation;
|
|
||||||
var networkCostInfo = winNetConn.NetworkCostType;
|
|
||||||
var networkConnectivityInfo = winNetConn.NetworkConnectivityLevel;
|
|
||||||
var networkAuthenticationInfo = winNetConn.NetworkAuthenticationType;
|
|
||||||
var networkEncryptionInfo = winNetConn.NetworkEncryptionType;
|
|
||||||
|
|
||||||
var connectionType;
|
|
||||||
|
|
||||||
var profile = Windows.Networking.Connectivity.NetworkInformation.getInternetConnectionProfile();
|
|
||||||
if(profile) {
|
|
||||||
var conLevel = profile.getNetworkConnectivityLevel();
|
|
||||||
var interfaceType = profile.networkAdapter.ianaInterfaceType;
|
|
||||||
|
|
||||||
// since we use this to detect whether we are online or offline we do check agains InternetAccess
|
|
||||||
// localAccess (airplane mode as an example) or constrainedInternetAccess mean there is no access to the internet available
|
|
||||||
// https://msdn.microsoft.com/library/windows/apps/windows.networking.connectivity.networkconnectivitylevel.aspx
|
|
||||||
if (conLevel != Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess) {
|
|
||||||
connectionType = Connection.NONE;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
switch (interfaceType) {
|
|
||||||
case 71:
|
|
||||||
connectionType = Connection.WIFI;
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
connectionType = Connection.ETHERNET;
|
|
||||||
break;
|
|
||||||
case 243: // (3GPP WWAN) // Fallthrough is intentional
|
|
||||||
case 244: // (3GPP2 WWAN)
|
|
||||||
connectionType = Connection.CELL_3G;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
connectionType = Connection.UNKNOWN;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// FYI
|
|
||||||
//Connection.UNKNOWN 'Unknown connection';
|
|
||||||
//Connection.ETHERNET 'Ethernet connection';
|
|
||||||
//Connection.WIFI 'WiFi connection';
|
|
||||||
//Connection.CELL_2G 'Cell 2G connection';
|
|
||||||
//Connection.CELL_3G 'Cell 3G connection';
|
|
||||||
//Connection.CELL_4G 'Cell 4G connection';
|
|
||||||
//Connection.NONE 'No network connection';
|
|
||||||
|
|
||||||
setTimeout(function () {
|
// report current connection type
|
||||||
if (connectionType) {
|
setTimeout(reportConnectionInfoOnce, 0);
|
||||||
win(connectionType);
|
// start traking future changes
|
||||||
} else {
|
networkInfo.addEventListener("networkstatuschanged", reportConnectionInfoOnce);
|
||||||
win(Connection.NONE);
|
|
||||||
}
|
|
||||||
},0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
require("cordova/exec/proxy").add("NetworkStatus",module.exports);
|
require("cordova/exec/proxy").add("NetworkStatus",module.exports);
|
||||||
|
Loading…
Reference in New Issue
Block a user