Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the internet connection availability in windows phone 8 application

I'm developing Windows Phone 8 application. In this application, I have to connect to the server to get the data.

So Before connecting to the server, I want to check whether the internet connection available or not to the device. If the internet connection is available, then only I'll get the data from the server, Otherwise I'll show an error message.

Please tell me how to do this in Windows Phone 8.

like image 507
user2636874 Avatar asked Dec 19 '13 08:12

user2636874


2 Answers

NetworkInterface.GetIsNetworkAvailable() returns the status of the NICs.

Depending on the status you can ask if the connectivity is established by using:

ConnectionProfile-Class of Windows Phone 8.1 which uses the enum NetworkConnectivityLevel:

  • None
  • Local Access
  • Internet Access

This code should do the trick.

bool isConnected = NetworkInterface.GetIsNetworkAvailable();
if (isConnected)
{
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    NetworkConnectivityLevel connection = InternetConnectionProfile.GetNetworkConnectivityLevel();
    if (connection == NetworkConnectivityLevel.None || connection == NetworkConnectivityLevel.LocalAccess)
    {
        isConnected = false;
    }
}
if(!isConnected)
    await new MessageDialog("No internet connection is avaliable. The full functionality of the app isn't avaliable.").ShowAsync();
like image 112
jAC Avatar answered Oct 13 '22 00:10

jAC


public static bool checkNetworkConnection()
{
    var ni = NetworkInterface.NetworkInterfaceType;

    bool IsConnected = false;
    if ((ni == NetworkInterfaceType.Wireless80211)|| (ni == NetworkInterfaceType.MobileBroadbandCdma)|| (ni == NetworkInterfaceType.MobileBroadbandGsm))
        IsConnected= true;
    else if (ni == NetworkInterfaceType.None)
        IsConnected= false;
    return IsConnected;
}

Call this function and check whether internet connection is available or not.

like image 29
Ramesh Avatar answered Oct 13 '22 00:10

Ramesh