How is the property 'type' property set in the Network plugin

I am using the Ionic Network plugin to notify, and let me know when a device is online/offline.

Specifically I use the `type’ property…

public isOnline(): boolean {
    let netWorkType = this.network.type;
    this.logger.debug(`Network type: ${netWorkType}`);

    // Use this when running on browser (eg Ionic serve) where plugin does not work
    let isOnline = netWorkType == null ? navigator.onLine : netWorkType != "none"
     return isOnline;
  }

This has always seemed to have worked for me, and does always seem to work for my in my environment. However, I have a case, where a Windows version, on a particular corporate network I have network.type; return none.

Looking into at thesource, I see

@CordovaProperty() type: string;

but I cannot see where this is set anywhere.

Looking at the plugin it wraps source, I can see the

function getCurrrentConnectionType ()

but I cannot see how this is hooked up to the type property if the Ionic wrapper.

Does any one know how the type is set, and does is set by the above getCurrrentConnectionType ?

I want to be able try and diagnose what why I am getting none where the device is actually connected (and http calls are working)

I have added logging code such as

 this.networkInfo = w.Windows.Networking.Connectivity.NetworkInformation;      

// get the ConnectionProfile that is currently used to connect to the Internet
const internetProfile = this.networkInfo.getInternetConnectionProfile();
 const message =
        "--*** logInternetConnectionProfileInfo ***--\n\r"
        + " ------------ Log single profile -----------------\n\r"
        + this.getConnectionProfileInfo(internetProfile);
      this.logger.info(message);
      ...

 /**
   * Helper as per https://docs.microsoft.com/en-au/previous-versions/windows/apps/hh452990(v=win.10)   
   */
  private getConnectionProfileInfo(connectionProfile) {
    if (connectionProfile == null) {
      return "";
    }

    try {
      var returnString = "getConnectionProfileInfo" + "\n\r" + "ProfileName: " + connectionProfile.profileName + "\n\r";

      switch (connectionProfile.getNetworkConnectivityLevel()) {
        case NetworkConnectivityInfo.None:
          returnString += "Connectivity Level: None\n\r";
          break;
        case NetworkConnectivityInfo.LocalAccess:
          returnString += "Connectivity Level: Local Access\n\r";
          break;
        case NetworkConnectivityInfo.ConstrainedInternetAccess:
          returnString += "Connectivity Level: Constrained Internet Access\n\r";
          break;
        case NetworkConnectivityInfo.InternetAccess:
          returnString += "Connectivity Level: Internet Access\n\r";
          break;
      }
...

and the logging sent to me I see…

 ------------ Log single profile -----------------

getConnectionProfileInfo

ProfileName: .

Connectivity Level: Internet Access

Connection Cost Information:

===============

So I can see Connectivity Level: Internet Access.

So, just wondering first of all were type is set so I can start to try to see why I am not getting the correct value.

Thanks in advance for any help!

1 Like

This is an awesome question, which really exposes how horrendous a language JavaScript is.

The short answer to your question is “yes”.

The longer answer is here, where type is set by the result of calling getInfo once Cordova is ready. getInfo is “defined” (scare quotes for the ability to “define” functions by hacking Object.prototype) here as sending the getConnectionInfo message across the barrier. On the native side of the barrier, Windows picks the ball up here, which does indeed call getCurrrentConnectionType.

1 Like

@rapropos - thankyou very much! That all makes sense. I should have looked into the www folder for the missing bits/glue.

Now to find out why I am having problems, everything there (ie in getCurrrentConnectionType) looks correct from what I know so far.

Cheers .

If anyone else interested, my problem was that the Windows API called by the plugin at line 43, required NetworkConnectivityLevel.internetAccess to be considered online

    // 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;
    }

In my case, on a locked down corp network, I was only getting back NetworkConnectivityLevel.LocalAccess, but this was enough to get to the lock server. I have had to just wrap these calls myself rather than use Network.type (at least for Windows).

Logged an issue here.

1 Like