Roman Meyer
Roman Meyer

Reputation: 2872

Windows Phone 8.1 emulator check Internet connection

I need to test my app in emulator, check Internet connection. I am using following class (from here http://www.jayway.com/2015/04/23/how-to-verify-internet-access-in-universal-apps/):

public class Connection
{
    public static bool HasInternetAccess { get; private set; }

    public Connection() {
        NetworkInformation.NetworkStatusChanged += NetworkInformationOnNetworkStatusChanged;
        CheckInternetAccess();
    }

    private void NetworkInformationOnNetworkStatusChanged(object sender) {
        CheckInternetAccess();
    }

    private void CheckInternetAccess() {
        var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        HasInternetAccess = (connectionProfile != null &&
                             connectionProfile.GetNetworkConnectivityLevel() ==
                             NetworkConnectivityLevel.InternetAccess);
    }
}

and checking connection by using class:

Connection conn = new Connection();
if (Connection.HasInternetAccess) {
      // some code
} else {
      MessageDialog msgbox = new MessageDialog("no internet");
      await msgbox.ShowAsync();
}

Problem is I get true every time, in emulator. I tried solution from here How to disconnect Windows Phone 8.1 emulator from network?, even rebooted PC, but nothing changed.

Testing on the device was correct. True if connection enabled, false if connection disabled.

P.S. Sorry for my terrible English.

Upvotes: 3

Views: 305

Answers (1)

Vel
Vel

Reputation: 200

The emulator always returns NetworkInformation.GetInternetConnectionProfile() as true, even if you emulate network conditions as having no network.

Use physical device to test the app.(it will works fine).

Upvotes: 1

Related Questions