4

I wanted to check whether my phone can connect to Internet or not. I have seen several questions already. One of those is Question . It says to use NetworkInterface.GetIsNetworkAvailable() this and i tried it. I have disconnected my PC from internet and Also turned off the DataConnection of the emulator but NetworkInterface.GetIsNetworkAvailable() this always returning true. But simultaneouly i also check for NetworkInterfaceType.None and interestingly it is coming null. Can anybody explain where i am missing info ?

Attempt : -

public static void CheckNetworkAvailability()
    {
       // this is coming true even when i disconnected my pc from internet.
       // i also make the dataconnection off of the emulator
        var fg = NetworkInterface.GetIsNetworkAvailable();

        var ni = NetworkInterface.NetworkInterfaceType;
        // this part is coming none  
        if (ni == NetworkInterfaceType.None)
            IsConnected = false;

    }

any help is appreciated :)

Community
  • 1
  • 1
loop
  • 9,002
  • 10
  • 40
  • 76
  • Will you be using the Internet in general, or are you checking if you can connect to the backend server? – Nzall Aug 04 '14 at 11:21

3 Answers3

9

I am using following code to check if the device has access to internet with if it is connected to wifi or data connection..

 public void UpdateNetworkInformation()
    {
        // Get current Internet Connection Profile.
        ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();

        //air plan mode is on...
        if (internetConnectionProfile == null)
        {
            Is_Connected = false;
            return;
        }

        //if true, internet is accessible.
        this.Is_InternetAvailable = internetConnectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;

        // Check the connection details.
        else if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != 71)// Connection is not a Wi-Fi connection. 
        {
            Is_Roaming = internetConnectionProfile.GetConnectionCost().Roaming;

            /// user is Low on Data package only send low data.
            Is_LowOnData = internetConnectionProfile.GetConnectionCost().ApproachingDataLimit;

            //User is over limit do not send data
            Is_OverDataLimit = internetConnectionProfile.GetConnectionCost().OverDataLimit;

        }
        else //Connection is a Wi-Fi connection. Data restrictions are not necessary. 
        {
            Is_Wifi_Connected = true;
        }
    }

Edit: And for simple internet connection you can use below code.

  System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

Hope this helps!

Muhammad Saifullah
  • 4,292
  • 1
  • 29
  • 59
7

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

I faced this problem myself and the only truly way of testing this behaviour is to deploy the application to a physical device running Windows Phone and test it with the data turned off.

meneses.pt
  • 2,588
  • 3
  • 27
  • 38
  • 1
    Is the ans below is right ?. I wanted to check for whether my app can connect to `internet` or not. nothing else.Like you said you have come across so how do you solve it ? – loop Aug 04 '14 at 12:13
  • In windows phone, usually when a device is not connected to the internet, it is also not connected to a network. So I didn't bother going further than that. – meneses.pt Aug 04 '14 at 12:31
  • I don't have device with me yet, have you tested it through cases ? – loop Aug 04 '14 at 12:50
4

NetworkInterface.GetIsNetworkAvailable() checks the network connection and not the internet connection. If you are in any kind of network, then it will return true whether internet is present or not.

You can check the internet connection as following:

using System.Net

private bool IsOnline() 
{
    try
    {
        IPHostEntry iPHostEntry = Dns.GetHostEntry("www.wikipedia.com");
        return true;
    }
    catch (SocketException ex) 
    {
        return false;
    }
}
Earth
  • 3,477
  • 6
  • 37
  • 78