3

I am working with a Windows Universal app (shared backend between Windows 8.1 and Windows Phone 8.1, not Silverlight). The app connects to Azure with Azure Mobile Services. In the settings for the app I would like to have an option for synchronisation to only occur over a WiFi network.

How can I determine if the phone is connected to WiFi or a mobile network? Although from my research I have found ways to do this with older versions of Windows Phone and with Silverlight, it seems I can only determine if the device is connected to the internet in a Windows Universal app.

Matthew Fitch
  • 245
  • 1
  • 9
  • 13
  • Maybe [this thread](https://social.msdn.microsoft.com/forums/windowsapps/en-us/d8e76732-19d3-47b3-840f-70d87c75ce9f/network-checking-in-winrt) or [this blog post](http://www.guruumeditation.net/blog/internet-connection-type-detection-in-winrt) will help. – Romasz Mar 04 '15 at 12:18

2 Answers2

11

I believe you can determine this information from the ConnectionProfile using something akin to:

using Windows.Networking.Connectivity;

var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
// connectionProfile can be null (e.g. airplane mode)
if (connectionProfile != null && connectionProfile.IsWlanConnectionProfile) {
    // do something over WiFi;
}

There is also the IsWwanConnectionProfile property, which is used to determine if the connection is via a 'mobile' connection (3g, etc).

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Please note: This applies to Windows Phone 8 and Windows Phone Silverlight 8.1 only! see this link: https://msdn.microsoft.com/en-us/library/windows/apps/jj207005(v=vs.105).aspx – Utsav Dawn Mar 04 '15 at 12:12
  • @UtsavDawn I threw this code into a new universal application (windows phone & windows store app), and it worked without issue. The [documentation for the Windows.Networking.Connectivity](https://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.connectivity.aspx) makes no mention of it being a silverlight API. – Anya Shenanigans Mar 04 '15 at 13:50
  • At first I didn't think this was working, but the "connectionProfile != null" is important! Thanks for your help. – Matthew Fitch Mar 05 '15 at 09:11
  • I modified it after I wrote it to deal with the null connectionProfile because it crashed when using airplane mode. – Anya Shenanigans Mar 05 '15 at 12:42
0

I think that this is the only way to check for internet availability :

bool IsConnected = NetworkInterface.GetIsNetworkAvailable();
        if (IsConnected)
        {
            // Do Something
        }
        else
        {
            // Do something different
        }

This Link is my reference.

Abdullah El-Menawy
  • 388
  • 1
  • 5
  • 17