Thursday, December 18, 2014

Check your Network Connectivity WindowsPhone 8

In the nutshell you all you have to do is check the NetworkInterface.NetworkInterfaceType property which is a enum with values (none,MobileBroadBandCdma,MobileBroadBandGsm,Ethernet,Wireless80211) .

bool IsConnected = NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None;

you can subscribe to the NetworkChange.NetworkAddressChanged event to the Monitor the connection changes .

as simple as it looks there are a couple of gotcha's that you might want to take care of

  1. you  will want to  move the code block to monitor the network connection to a background thread a it might block the UI thread when checking  for the connection status .
  2. if the phone switches  the connection several times the NetworkAddressChanged is raised many times and it can again be issue if you are performing some time consuming task ,The Microsoft's Reactive Extension(Rx) comes in handy here , its a great framework with lot of goodies , in this case it lets to control the minimum interval  time before the NetworkAddressChanged, have used the Rx when implementing the search Contract in windows store apps where as a developer you  want to react to input typed into the search box in a timely manner (Link to Rx).
Below is the code Snippet which will check the current connection status and raises events when the connection status .
public class NetworkConnectionMonitor 
 {
  const int sampleRateMs = 1000;
  IDisposable subscription;

  public event EventHandler NetworkConnectionChanged;
  public NetworkConnectionType NetworkConnectionType  { get; private set; }

  public bool Connected
  {
   get
   {
    return NetworkConnectionType != NetworkConnectionType.None;
    //return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
   }
  }

  public NetworkConnectionMonitor()
  {
   Update();

   var observable 
    = Observable.FromEvent(
     handler => new NetworkAddressChangedEventHandler(handler),
     handler => NetworkChange.NetworkAddressChanged += handler,
     handler => NetworkChange.NetworkAddressChanged -= handler);

   IObservable> sampler 
    = observable.Sample(TimeSpan.FromMilliseconds(sampleRateMs));

   subscription = sampler.ObserveOn(Scheduler.ThreadPool).Subscribe(
                args => Update());
  }
  
  void Update()
  {
   switch (NetworkInterface.NetworkInterfaceType)
   {
    case NetworkInterfaceType.None:
     NetworkConnectionType = NetworkConnectionType.None;
     break;
    case NetworkInterfaceType.MobileBroadbandCdma:
    case NetworkInterfaceType.MobileBroadbandGsm:
     NetworkConnectionType = NetworkConnectionType.MobileBroadband;
     break;
    case NetworkInterfaceType.Ethernet:
    case NetworkInterfaceType.Wireless80211:
    default:
     NetworkConnectionType = NetworkConnectionType.Lan;
     break;
   }

   Deployment.Current.Dispatcher.BeginInvoke(new Action(
    () => NetworkConnectionChanged.Raise(this, EventArgs.Empty)));
  }
 }
** Along with the above you might want to consdier one more important paramter when downloading/uploading content  from you app, that is the datacost when the user is connected to the mobile network , The DataSense API  allows you to query if the data connection in roaming ,is the user reaching the data plan limit / exceeded the limit, based on the result the app can decided if it can download the data or not .

ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); \
ConnectionCost = connectionProfile.GetConnectionCost(); 
if (connectionProfile.NetworkAdapter.IanaInterfaceType = = (int) IanaInterfaceTypes.Wifi 
| | connectionCost.NetworkCostType = = NetworkCostType.Unrestricted)
{
// no need to restrict data Transfer
}
if (connectionCost.Roaming | | connectionCost.OverDataLimit)
 { // Dont tranfer any data } 
 
 if (connectionCost.ApproachingDataLimit)
 { // may be you want to warn user }

0 comments:

Post a Comment