d.underhill
d.underhill

Reputation: 11

Windows store and phone detecting Internet available

I have a problem with an edge case that I hope some one can help with. I have a Windows 8.1 universal app and have both a store and phone package being created and trying to share as much code between them as possible. Since our app requires internet access in order to do some functions we needed a reliable way to determine that before we tried and failed a function.

So originally my code just looked at NetworkInformation.GetInternetConnectionProfile() and then compared GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess. This works in most cases but we found a case on phone where the device was on a data plan by a carrier but then removed so it can only do wifi data now.In this case, it appears the phone continues to reported true even if wifi was turned off and then fail to connect but never report that there was no internet available.

So we changed the logic to below.

       var internetConnection = NetworkInformation.GetInternetConnectionProfile();

        // If null then no internet and internet connection is null
        // If it is not null, then you have to look for any GetLanIdentfiers.  This means there is a lan adapter setup.
        // lastly you have to make sure the interface type is either an Ethernet or IEEE 802.11 interface.
        if ( internetConnection != null
           && internetConnection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess
           && ( ( internetConnection.NetworkAdapter.IanaInterfaceType == IEEE_802_11_INTERFACE 
           && NetworkInformation.GetLanIdentifiers().Any() )
           || internetConnection.NetworkAdapter.IanaInterfaceType == ETHERNET_INTERFACE ) )
        {
           return true;
        }

        return false;

As the comments say, if the return from the GetInternetConnectionProfile() is null then there isn't a connection available. If there is one then I check for IneternetAccess. Then I check for the IanaInterfaceType of 802.11 && any LanIdentifier or the ethernet interface.

On a phone that was on a carrier data plan but no longer, this does properly detect that wifi is not available. However in the field we now have some store app customers reporting that the app thinks there is no wifi with the above code. We can certainly add a #If to wrap the logic one way or the other but at some point we want to move to the new Universal app project where using the #if should be very rare so I would like to avoid that.

We have not be able to duplicate this problem on any of our test devices so this may be a local environmental configuration that we just don't have in our lab. Has anyone else figured out a reliable way to determine if wifi is available or not on a phone and store application?

Upvotes: 1

Views: 65

Answers (2)

PESMITH_MSFT
PESMITH_MSFT

Reputation: 358

Short answer: the only way to tell if you can reach your servers is to try to reach your servers. Absolutely no other method is foolproof.

For example: the "connected to internet" can be fooled by proxies, by captive portals, and by firewalls either on your machine or anywhere on your path.

Source: I was the PM for a bunch of the UWP network APIs

Upvotes: 0

CodeNoob
CodeNoob

Reputation: 757

There are two properties in NetworkInformation class itself called IsWwanConnectionProfile and IsWlanConnectionProfile.

The first property gives you the value that indicates if connection profile is a WWAN (mobile) connection and second property gets a value that indicates if connection profile is a WLAN (WiFi) connection. Hope this helps.

Upvotes: 0

Related Questions