InetSpeedWinRT 1.4.2

dotnet add package InetSpeedWinRT --version 1.4.2                
NuGet\Install-Package InetSpeedWinRT -Version 1.4.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InetSpeedWinRT" Version="1.4.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InetSpeedWinRT --version 1.4.2                
#r "nuget: InetSpeedWinRT, 1.4.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InetSpeedWinRT as a Cake Addin
#addin nuget:?package=InetSpeedWinRT&version=1.4.2

// Install InetSpeedWinRT as a Cake Tool
#tool nuget:?package=InetSpeedWinRT&version=1.4.2                

This is a convenient Windows and Windows Phone multi-language API for making Internet connection state decisions in real time, using the speed result to decide if/when to run your network-intensive code.

This API does not need to eat a bunch of network bandwidth to do it's job.

This API fills a small gap in the Windows platform networking APIs by providing developers with a very easy to use, simple abstraction to measure current Internet connection speed, which most apps today are not doing. When apps make connectivity state assumptions based only on the existence of an active Internet connection, a potentially poor experience may result for your customers.  

This API will benefit the people using your app, who should always be shielded from network latency under all circumstances. Developers will also benefit as you can be confident that:

a) This easy-to-use API will return a result very quickly, as any API call should

b) This API is accurate and reliable across Windows and Windows Phone

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.2 384 12/11/2014

In this release: refined accuracy across connection types, minor bug fixes.

Usage:

You will need to remove the architecture specific references that don’t apply to your project’s build target after referencing the nuget package via References -> Add Reference… -> Manage Nuget packages. The component is written in C++/CX, so each dll and winmd file is created for a specific machine architecture (ARM for Windows, Windows Phone 8 (ARM), Windows Phone 8.1 (ARM), Windows 8/8.1 (ARM, x86, x64).  

You’ll see these files in your references folder when you install the InetSpeedWinRT nuget package (remove all that don’t apply to your solution…)

InetSpeedWinRT components (by architecture and bitness) ->

InetSpeedWinRTARM80 ARM-based Windows Runtime 8.0
InetSpeedWinRTARM81 ARM-based Windows Runtime 8.1
InetSpeedWinRTPhone80 ARM-based Windows Phone 8.0 (WinRT and Silverlight)
InetSpeedWinRTPhone81 ARM-based Windows Phone 8.1 (WinRT and Silverlight)
InetSpeedWinRTx6480 64-bit Windows Runtime 8.0
InetSpeedWinRTx6481 64-bit Windows Runtime 8.1
InetSpeedWinRTx8680 32-bit Windows Runtime 8.0
InetSpeedWinRTx8681 32-bit Windows Runtime 8.1

Note: You will need to reference the SDK Extension Microsoft Visual C++ 2013 Runtime Package for Windows 8.1 and Windows Phone 8.1 projects, for C# and JavaScript projects.


The Internet Connection Speed API, version 1.4.2

class InternetConnectionState
object that contains the static members you use to determine Internet connection state (connection status and speed/latency/delay)

Properties

static bool Connected
Returns true if the current Internet connection for the device is active, else false.

Methods

static IAsyncOperation<ConnectionSpeed> GetInternetConnectionSpeed();
Asynchronous method that will return a ConnectionSpeed (see below).

static IAsyncOperation<ConnectionSpeed> GetInternetConnectionSpeedWithHostName(HostName hostName);
Asynchronous method that will perform the speed/latency test on a supplied host target and returns a ConnectionSpeed. This is very useful to ensure the Internet resource you’re trying to reach is available at the speed level you require (generally, these would be High and Average…).

enum class ConnectionSpeed
Speed test results are returned as an enum value (For JavaScript consumers, you’ll need to build your own object mapping. See the JavaScript example).

High: Device is currently attached to a high-speed, low-latency Internet connection.

Average: Device is currently attached to an average speed/latency Internet connection (LTE, 3G, etc…).

Low: Device is currently attached to a low-speed, high-latency Internet connection.

Unknown: The current Internet connection speed can't be determined. Proceed with caution. This could mean that there is very high network latency, a problem with an upstream service, etc...

Example (C# consumer):
This example tests for a highspeed network based on a provided HostName (this is the best way to use this API given you really want to know the status of the Internet connection as it pertains to where you need to put/grab data over the network in real time...). Note you should always test for Unknown and then react accordingly (don't proceed with network work. Unknown means you are connected to the Internet, but you can't do network work with acceptable latency.)

High and Average are the two results you should test for before doing network work that involves either downloading or uploading data.

           if (InternetConnectionState.Connected)
           {
               var speed =
                   await InternetConnectionState.GetInternetConnectionSpeedWithHostName(new HostName("targethost.com"));
           
               if (speed == ConnectionSpeed.High)
               {
                   \\Current network speed is High, low latency
               }
               else if (speed == ConnectionSpeed.Average)
               {
                   \\Current network speed is Average, average latency
               }
               else if (speed == ConnectionSpeed.Low)
               {
                   \\Current network speed is Low, high latency
               }
               else
               {
                   \\Current Network Speed is Unknown - use at your own risk...
               }
           }
           else
           {
               ResultsBox.Text = "Not Connected to the Internet!";
           }
       }

Example (JavaScript consumer):

The JavaScript WinRT projection doesn't support C++ enum class named values. Instead, the enum integer values are passed along...

The following makes the code easier to read -> apply named values (JS objects) to numbers (the speed result integer) received from the native API...
 
   var ConnectionSpeed = { High: 0, Average: 1, Low: 2, Unknown: 3 };

 function getConnectionSpeed() {
     if (InetSpeedWinRTx64.InternetConnectionState.connected) {
             InetSpeedWinRTx64.InternetConnectionState.getInternetConnectionSpeedWithHostName(
               new Windows.Networking.HostName("mytargethost.com"))
               .then(function (speed) {
               if (speed === ConnectionSpeed.Unknown) return;
               if (speed === ConnectionSpeed.High) {
                         //Highspeed, low latency Internet connection detected...
               }
               else if  (speed === ConnectionSpeed.Average){
                         //Average speed, average latency Internet connection detected...
               }
               else if (speed ===  ConnectionSpeed.Low) {
                         //High latency, low speed Internet connection detected...
               }
         });
     }
     else {
               //Not connected...
}

Example (C++/CX consumer):

if (InternetConnectionState::Connected)
{
     auto connectionSpeed = InternetConnectionState::GetInternetConnectionSpeedWithHostName(ref new Windows::Networking::HostName("mytargethost.com"));

     create_task(connectionSpeed).then([this](ConnectionSpeed speed)
     {
         if (speed == ConnectionSpeed::Unknown) return;

         if (speed == ConnectionSpeed::High)
         {
             //highspeed, low latency Internet connection...
         }
         else if (speed == ConnectionSpeed::Average)
         {
             //This is the most common connection speed across Cellular and Wifi...
         }
         else if(speed == ConnectionSpeed::Low)
         {
               //Low speed, high latency...
         }
   });
}
else
{
   //Not connected...
}


Known issue with Windows Phone radio when the device resumes from standby

When Windows Phone devices resume from standby, the time it takes for the cellular radio to fully “awaken” can lead to inaccurate results from InetSpeedWinRT (the ConnectionSpeed result in this case is typically Low or Unknown – and this has been verified in extensive testing to be inaccurate as a subsequent call will return the actual state, typically higher…)… This is not a bug in InetSpeedWinRT. This is an issue faced by any app that makes network calls upon device then app resumption.

One simple workaround is to touch the network interface from both Application_Launching and Application_Activated event handlers (typically in your App class implementation file). By doing this before calling GetInternetConnectionSpeed or GetInternetConnectionSpeedWithHostname (or any API that generates network traffic on a cellular connection as part of its implementation) in your network-critical code paths, you can guarantee that the radio is fully functional and therefore an accurate result is returned.

       private void Application_Launching( object sender, LaunchingEventArgs e )
       {
           //This will wake up the radio...
           var networkInterfaceType = NetworkInterface.NetworkInterfaceType;
       }        

 
       private void Application_Activated( object sender, ActivatedEventArgs e )
       {
           //This will wake up the radio...
           var networkInterfaceType = NetworkInterface.NetworkInterfaceType;
       }

We hope this API proves useful to you in your Internet-first applications.