Network Detection in WP7

It is always better to detect the network connectivity and network type before performing any task on mobile devices. You can use GSM/EDGE/CDMA network for normal login functionality, updating status messages or perform normal task which doesn’t require huge bandwidth. Whereas, you can rely on WiFi or Ethernet (USB Connection) for synchronizing huge data like pictures, videos or audio files.

For checking network and its type, Windows Phone 7 consists two assemblies/namespaces.

System.Net.NetworkInformation
Microsoft.Phone.Net.NetworkInformation

However, you should use the second one (Microsoft.Phone.Net.NetworkInformation) for performing the tasks on devices. You can enumerate through network like:

   1:  switch (NetworkInterface.NetworkInterfaceType)
   2:  {
   3:      case NetworkInterfaceType.Ethernet:
   4:          //Do something when Ethernet or 
   5:          //USB connectivity is available
   6:          break;
   7:      case NetworkInterfaceType.MobileBroadbandCdma:
   8:          //Do something when CDMA network
   9:          //is available
  10:          break;
  11:      case NetworkInterfaceType.MobileBroadbandGsm:
  12:          //Do something when CDMA 
  13:          //network is available
  14:          break;
  15:      case NetworkInterfaceType.None:
  16:          //The is no network
  17:          break;
  18:      case NetworkInterfaceType.Unknown:
  19:          //Connected network type is unknown
  20:          break;
  21:      case NetworkInterfaceType.Wireless80211:
  22:          //When device is connected to WiFi
  23:          break;
  24:      default:
  25:          break;
  26:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Using this code you can execute your code depending on network interface type.

Find related post on MSDN: Click here 

Namaste

Mayur Tendulkar | www.mayurtendulkar.com

Published by

Mayur Tendulkar

Struggling Juggler at Dyte

3 thoughts on “Network Detection in WP7”

  1. I know, Switch.. Case means killing object oriented concepts. But here I used it for the sake of example. You can jolly well go ahead and use NetworkInterfaceType enum depending on your needs and in OOP way :)

    Like

  2. Thanks for sharing. One caveat is in some cases the network detection may take 20+ seconds, which will halt the UI if you call it normally. I found a good practice is to check the network with its own thread so the UI can continue to work while the network is being checked. I have heard this latency is most prevelant on HTC phones. Thanks again for the great post.

    Here is my page with a couple FREE games I made for WP7, feel free to dowload and enjoy:

    http://wp7freegames.blogspot.com/

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.