Search code examples
network-programmingwindows-phoneconnectionuwp

Is there an equivalent of DeviceNetworkInformation.NetworkAvailabilityChanged event on UWP?


I'm converting my Windows Phone 8 Silverlight to UWP and I can't find an equivalent to DeviceNetworkInformation.NetworkAvailabilityChanged event in UWP

I know that on UWP we have to use ConnectionProfile to get info about user's connection (Wifi, 3G, etc...)

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

But it seems that there are no events to check if the Internet becomes unavailable in ConnectionProfile object.

Does anyone know how to do this in UWP?

Thanks


Solution

  • There might be no equivalent of DeviceNetworkInformation.NetworkAvailabilityChanged Event in UWP APIs by now. But we can achieve this by combining NetworkInformation.NetworkStatusChanged event with ConnectionProfile.GetNetworkConnectivityLevel method.

    Ref Remarks in ConnectionProfile.GetNetworkConnectivityLevel:

    The recommended process for determining the network connectivity level is to register a handler for the NetworkStatusChanged event on the NetworkInformation class. When a notification is received of a network status change, obtain the new connectivity level by calling the GetNetworkConnectivityLevel method on the profile returned by the GetInternetConnectionProfile method. The returned network connectivity level can then be stored for later use when needed. This also ensures that the correct ConnectionProfile is checked.

    Following is a simple sample:

    NetworkInformation.NetworkStatusChanged += (s) =>
    {
        var profile = NetworkInformation.GetInternetConnectionProfile();
        var isInternetConnected = profile != null && profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    };
    

    Also you can encapsulate this into an event like in this blog: How to react to network availability changes in Windows Store apps.