January 27, 2011

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:  }



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

January 23, 2011

Bandwidth friendly data access in WP7 app

Recently, I heard lot of fuss about too much bandwidth usage by some Windows Phone 7 devices and Microsoft has blamed some third party applications. I don’t know the exact and real reason. But, there can be different ways to minimize the data usage. And I’m not going to talk about ‘turning off data connection’ :)

Most of the time, we (application developers) need to access data from remote locations. And this can add to data usage. For this reason, I found JSON very helpful. One way to reduce this usage is by opting for JSON. Well, there are some good reasons for it. Both XML & JSON has their own pros and cons. And many veterans has mentioned about it.

Now, if you’ve got XML Web Services, then its pretty easy to work with them on WP7 platform, just like any other Silverlight application consuming XML Web Services.

However, when it comes to JSON, there are different options & tools available:

  • DataContractJsonSerializer
    This class is available for JSON serialization and deserialization, which is available within base class library
  • Json.NET
    The above library is good for normal JSON de/serialization. However, as mentioned over here, Json.NET is open source library, which is much more easier and powerful (in terms of performance considering large amount of JSON data)
  • JSON Class Generator
    The best part with XML Web Services or WCF Services is: there are tools which allows you to generate proxy classes for easy access to types at client side (remember svcutil.exe?). It would have been really great if Microsoft has provided one utility to generate classes from JSON. Not to worry though. This utility does the same thing for you. 

Now, as a Mobile Application Developer, consuming less bandwidth is one of our challenge. And JSON can help us with it (with adding some more challenges). However, you need to think of the problem and choose between XML services or JSON services, whichever fits the bill.

Namaste

Mayur Tendulkar | www.mayurtendulkar.com