Intel® Mobile Platform SDK: Monitor Connectivity Status

Submit New Article

Last Modified On :   March 12, 2008 1:32 AM PDT
Rate
 



Abstract

This article and sample code demonstrate how to use the Intel Mobile Platform SDK to obtain information concerning a system's network connectivity status.


Introduction

Connectivity is an important factor to consider when developing an application that supports mobility. Since 100% connectivity is not realistic in the real world, applications need to have the capability to determine the current status of connectivity and take appropriate action depending on any change in status. The Intel® Mobile Platform Software Development Kit (Intel® Mobile Platform SDK) provides such a functionality to retrieve/monitor connectivity status.

This article and sample code demonstrate how to use the Intel Mobile Platform SDK to obtain information concerning a system’s network connectivity status. Such information is particularly useful for applications that need to change their behavior based on the availability of the connection.


Intel® Mobile Platform SDK

The Intel Mobile Platform SDK helps you to construct mobile-aware applications. For example, to improve an application behavior over intermittent network connections by getting current system information and monitoring changes in system status without constantly polling for it.

This is a multi-platform multi-runtime development kit. Interfaces for C++, .NET* and Java* are supported. With these, the SDK provides developers with a common and consistent interface for platform devices such as network adapters and battery, and capabilities, such as power and bandwidth, and control of platform interaction and adaptation. The SDK enables developers to control the way in which an application running on a mobile platform reacts to changes in context.


Connectivity Status Tool Description

The sample application will demonstrate how to use the Intel Mobile Platform SDK to obtain system connectivity information. The target platform is an Intel® Centrino® Mobile Technology laptop with the Microsoft Windows* XP Professional* operating system. The sample code was developed using C#.

For simplicity, the connectivity end point (URL) has been defined as: http://www.intel.com.  Figure 1 shows the user interface of the application.

Figure 1. User Interface with no connectivity

Figure 2 shows how the tool displays information when connected.  Note the event log that records Connect/Disconnect events as they occur. In this example, Figure 1 depicts no initial network connection available. When an (active) network cable is plugged in, the status dynamically changes to ‘Connected’ and the ‘ConnectedEvent’ is logged in the ‘Events’ box as shown in Figure 2.

Figure 2. When at least one network connection is available

It also shows if the latency measurement reaches the end point.


Implementation and Initialization

The Intel Mobile Platform SDK provides several properties and APIs to retrieve system information. We’re after‘Connectivity’, which falls into the ‘Context” category within the API.  To get started, you’ll need to instantiate an instance of the ‘ContextClass’. You can then use this to retrieve a ‘Connectivity’ object.

ContextClass  myClass = new ContextClass ();
ConnectivityInstance myConnectivityInstance;
myConnectivityInstance =  (ConnectivityInstance) myClass.GetInstance("Connectivity");

 

Now that we have an initialized connectivity object, we can interrogate its properties to determine connectivity data.


Connectivity Properties

A Connectivity instance provides access to many properties to acquire the current connection status. One such method is the IsReachable() method, which tries to reach a specified destination string. This is a very useful feature, as a network adapter on the system may show a ‘Connected’ state, but if a router has a problem, there is no way the mobile platform will know if the network resource is available. With the help of this API, an application can attempt to reach the destination URL and then report back the result.

Here is the source that shows how to use connectivity properties:

//Get type
val = myConnectivityInstance.GetType();
str = "Type: " + val;
listBox1.Items.Add(str);
//Get key
val = myConnectivityInstance.GetKey();
str = "Key: " + val;
listBox1.Items.Add(str);

//Get value of 'Connected' property
bool var = myConnectivityInstance.IsConnected.GetValue();
str = "Connected(?):              " + var.ToString();
listBox1.Items.Add(str);

//Test if specified destination is reachable
var = myConnectivityInstance.IsReachable(sDestination);
str = "Reachable(?):              " + sDestination + "
;       " + var.ToString();
listBox1.Items.Add(str);

 

More information can be found on Connectivity properties and APIs in the ‘Connectivity’ section of the Intel® Mobile Platform Software Development Kit Programmer’s Guide (.CHM 582KB).


Event Notification

Event notification is a mechanism to obtain changes in the system status without constantly polling for it.

In the sample application, an event notification class is implemented to monitor changes in the status of Connect and Disconnect events. That is, whenever at least one network connection becomes available or the last network connection becomes unavailable, these events will be fired notifying the application.  Classes do this by implementing the Observer design pattern.

The following class implements the notification method for these events:

internal class StatusObserver : Observer
{
public override void Notify(Event ev)
{
try
  {
//When 'Connect' event is received...
if (ev.GetType() == Event.EventType.eConnected
                              {
//Record ‘ConnectEvent received’
}

//When 'Disconnect' event is received...
else if (ev.GetType() == Event.EventType.eDisconnected)
{
//Record ‘DisconnectEvent received’
}
}

//Exception handling
catch (IntelMobileException ex)
{
//Handle exceptions
}
}
}

 

In the main class, event observers are added to event properties that need to be monitored (in this case ‘Connect’ and ‘Disconnect’).  Here’s how to do it:

//Add listener on 'Connect'/'Disconnect' events
StatusObserver observe = new StatusObserver();
myConnectivityInstance.Connected.AddObserver(observe);
myConnectivityInstance.Disconnected.AddObserver(observe);

 

By performing these steps, observers are added for the specified events. When these events occur, the ‘Notify()’ method implemented in the StatusObserver class is called and appropriate action can be taken based on the event type.

It is important to remove the observers before terminating the application. If this is not done, there may be a few hanging references even though the application has terminated:

//Remove listeners 
myConnectivityInstance.Connected.RemoveObserver(observe);
myConnectivityInstance.Disconnected.RemoveObserver(observe);

 


Summary

This tutorial shows how to use the Intel Mobile Platform SDK to retrieve connectivity information and events at runtime without continuously polling devices. The Intel Mobile Platform SDK provides a straightforward API that lets you mine other information about platform, devices/capabilities, such as battery, network, and power. 

 


About the Authors

Rajshree Chabukswar
Rajshree Chabukswar is a software engineer working on client enabling in the Software Solutions Group that enables client platforms through software optimizations. Prior to working at Intel, she obtained a Masters degree in Computer Engineering from Syracuse University, NY. Her e-mail is rajshree.a.chabukswar@intel.com.

Joe Zhao
Joe Zhao is a Software Technical Consulting Engineer working for Intel Corporation’s Software and Solutions Group in Shanghai, China. He worked for nearly 2 years in Intel. He was the engineer in supporting Mobile Platform SDK, and now he is in charge of the maintenance of MPSDK Open Source project website. He got Master degree from the University of East Anglia in England