Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 check if internet is available

My app WP7 was not accepted because it fails to load if the internet is not available. I looked for a way to check it and found this command

NetworkInterface.GetIsNetworkAvailable()

But it isn't working on the emulator and I do not have any device to test it. Could someone tell me if it returns false if the device is in Airplane mode? If not, how can I check for it?

Thanks, Oscar

Edit: I also tried with this code:

try
{
    wsClient.CurrenciesCompleted += new EventHandler<CurrencyConversion.CurrenciesCompletedEventArgs>(wsClient_CurrenciesCompleted);
    wsClient.CurrenciesAsync(null);
}
catch
{
     NetworkNotAvailable();
}

But I am not able to catch the exception, I also tried in the wsClient_CurrenciesCompleted method, but also no good.

Where could I test it?

like image 435
JSBach Avatar asked Dec 07 '10 15:12

JSBach


3 Answers

Don't test for "the internet in general" - test for the service you'll actually be connecting to. Test for it by trying to connect to it - make some simple, non-destructive request on start-up. Yes, that will take a tiny bit of the user's data allowance, but:

  • You will be warming up the networking stack and making a connection which should end up being kept alive automatically, so future latency will be reduced.
  • You could warn the user that they may have limited functionality if the connection fails.
like image 119
Jon Skeet Avatar answered Nov 02 '22 01:11

Jon Skeet


An Alternative to Jon's suggestion is to check which network interface is available. This is very handy in cases were you need to adjust which service you call based on network speed. For example the switch statement below could be modified to return an Enum to represent the quality of the network.

public class NetworkMonitorClass 
{
   private Timer timer;
   private NetworkInterfaceType _currNetType = null; 
   private volatile bool _valueRetrieved = false;

   public NetworkMonitorClass()
   {
       //using a timer to poll the network type.
       timer = new Timer(new TimerCallBack((o)=>
       {
           //Copied comment from Microsoft Example:
           //  Checking the network type is not instantaneous
           //  so it is advised to always do it on a background thread.
           _currNetType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;
           _valueRetrieved = true;
       }), null, 200, 3000); // update the network type every 3 seconds.
   }

   public NetworkInterfaceType CurrentNetworkType 
   { 
       get
       {  
           if(false == _valueRetrieved ) return NetworkInterfaceType.Unknown;  
           return _currNetType; 
       } 
       private set { ;} 
   }

   public bool isNetworkReady()
   {
       if(false == _valueRetrieved ) return false;

       switch (_currentNetworkType)
       {
          //Low speed networks
          case NetworkInterfaceType.MobileBroadbandCdma:
          case NetworkInterfaceType.MobileBroadbandGsm:
            return true;
          //High speed networks
          case NetworkInterfaceType.Wireless80211:
          case NetworkInterfaceType.Ethernet:
            return true;
          //No Network
          case NetworkInterfaceType.None:
          default:
             return false;
      } 
   }
}

See http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.networkinterface.networkinterfacetype(VS.92).aspx

like image 22
eSniff Avatar answered Nov 02 '22 01:11

eSniff


GetIsNetworkAvailable() will always return true in the emulator. For testing in the emulator you'll need to work round this in code.

This can be a useful quick check but you also (as Jon pointed out) need to handle the scenario of not being able to connect to your specific server.

Handling this can be done by catching the WebException when you try and get the response in the callback.

private static void DownloadInfoCallback(IAsyncResult asynchronousResult)
{
    try
    {
        var webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

        // This will cause an error if the request failed
        var webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

        .....
    }
    catch (WebException exc)
    {
        // Handle error here
    }
}
like image 2
Matt Lacey Avatar answered Nov 02 '22 00:11

Matt Lacey