Intel® Mobile Platform SDK 1.2: Network Detection

Submit New Article

Last Modified On :   October 10, 2008 5:19 PM PDT
Rate
 



Abstract

Learn how to use the open source Intel® Mobile Platform SDK 1.2 to extract network device information. Also included are code samples written in C# for the Microsoft Windows XP* platform.


Introduction

The ability for an application to determine the status of available network is an important factor in the resource constrained Mobile application arena, since wireless connections cannot be assumed to be always available (possibly a bad assumption for wired networks also). Applications need the capability to determine the current status of network devices and take the appropriate actions when connect or disconnect events occur. For example, when a mobile platform moves out of range from an access point (AP), or the signal is blocked.

The Intel® Mobile Platform Software Development Kit 1.2 Open Source (Intel® Mobile Platform SDK) provides a common framework for developers to retrieve properties of network devices and to monitor the appropriate current network status. It also features APIs to access power, disk and other system information important in mobile contexts, though we will focus exclusively on network connectivity here.

This article demonstrates how to use the Intel® Mobile Platform SDK to extract network device information. It also includes code samples written in C# for the Microsoft Windows XP* platform.


About the Intel® Mobile Platform SDK

Before we get started, here’s a brief run down of the architecture and design behind the Intel® Mobile Platform SDK:

  • Multi-architecture — Available 32-bit Intel® Architecture, Intel® Centrino® Mobile Technology (Intel CMT).etc.
  • Multi-operating system — Available on Microsoft Windows Vista Business/Ultimate Edition,Microsoft Windows * XP SP2,Microsoft Windows Tablet PC Edition 2005, Microsoft Windows Mobile* 2003 Pocket PC Edition, Microsoft Windows Mobile* 2003 Smartphone Edition, Microsoft Windows Mobile* 5.0 Pocket PC Edition, Microsoft Windows Mobile* 5.0 Smartphone Edition.
  • Multi-language — Accessible with languages, such as C/C++, Java*, .Net* (C#, Visual Basic*), and Common Language Runtime (CLR) languages.
  • Multi-runtime — Functions in C Runtime, .NET, CLR, and the following Java runtime environments: Java 2 Micro Edition* (J2ME), Java 2 Standard Edition* (J2SE), and Java 2 Enterprise Edition* (J2EE).
  • Multi-idiom — Support for both an object-oriented and procedural call-oriented invocation idiom.

 

Let’s now turn to the network tool which we are going to build in this article.


Network Status Tool description

This article shows how to use the Intel Mobile Platform SDK to retrieve current information on all the network devices present in the system and how to keep track of any network status changes at runtime.

The target platform is an Intel CMT laptop with the Windows XP Professional operating system running on it. The sample code was developed using C#.

Figure 1: UI with network disconnected

The user interface (UI) of the sample tool that we are going to build in this article is hown in Figure 1.The tabs display relevant Network Adapters and Link Protocols information. The Events Tab keeps track of the runtime network connect/disconnect status changes. Details within all the tabs are updated automatically when a network event occurs.

As shown in Figure 1, both network adapters (WiredAdapter and RadioAdapter) are in the MediaDisconected status. When an active network cable is connected to the system, the ‘mediaConnect’ event is fired and is logged in the ‘Events’ Tab’.

Figure 2: Logged event when a network cable is connected at runtime

The network ‘connect’status changes automatically, as shown in Figures 2 and 3. Figure 2 displays the events details added to the ‘Event’ tab.

Figure 3: The updated UI after the ‘mediaConnect’ event

Let’s now turn to the implementation of this application.


Implementing the Application

The Intel Mobile Platform SDK provides several properties and APIs to retrieve information from system devices. To access the connectivity information that we are after, instantiate instances of the NetworkAdapter and LinkProtocol classes as follows:

Click here to download source sample.

The Network Adapters and Link Protocols class as follows:

NetworkAdapterClass myClassNet = new NetworkAdapterClass();
LinkProtocolClass myClassLink = new LinkProtocolClass();
myCollectionNet = (NetworkAdapterCollection)myClassNet.GetInstances();
myCollectionLink = (LinkProtocolCollection)myClassLink.GetInstances();

 

Once the objects are initialized, the properties of each network adapter and link protocol can be retrieved.

Network Adapter

The NetworkAdapter class provides access to information concerning basic structure for organizing network adapter-related information in the system

while(myCollectionNet.HasNext())
{
myInstance = (NetworkAdapterInstance)myCollectionNet.Next();
if (!myInstance.Name.IsNull())
{

val = myInstance.Name.GetValue();
str = "Name: "+val;
listBox1.Items.Add(str);
}
if (!myInstance. ConnectionState.IsNull())
{
var = myInstance. ConnectionState.GetValue();
val = var.ToString();
str = "NetConnectionStatus: " + val;
listBox1.Items.Add(str);
}
…………………

}

 

Link Protocols

The LinkProtocol class can be used to access properties about protocols active on the network adapter devices.

while(myCollectionLink.HasNext())
{
myInstance = (LinkProtocolInstance)myCollectionLink.Next();

if (!myInstance.Id.IsNull())
{
val = myInstance.Id.GetValue();
str = "ID: " + val;
listBox3.Items.Add(str);
}
if (!myInstance.IpAddress.IsNull())
{
val = myInstance.IpAddress.GetValue();
str = "IP: " + val;
listBox3.Items.Add(str);
}
…………………
}

 

The property values that are retrieved are added to the UI list box with appropriate labels.

To know more about Network Adapters, Link Protocol properties, and APIs, refer to the "Network Device" section in the Programmers Guide*. This guide is distributed with the Intel® Mobile Platform SDK.

Event Notification

In this sample, the MyConnectionState class (derived from Observer) is implemented to listen to the Link Protocol mediaConnect and mediaDisconnect events as they occur at runtime. Here’s how we set up the observer.

internal class MyConnectionState : Observer
{
public override void Notify(Event evnt)
{
try
{
if (evnt.GetType() == Event.EventType.eMediaConnected)
{
//Record Connect event in the list box
}
if (evnt.GetType()==Event.EventType.eMediaDisconnected)
{
//Record Disconnect event in the list box.
}
}
catch (IntelMobileException ex)
{
//Handle Exception
}
}

 

In the main class constructor, event observers are added for each Link Protocol object event. Here’s how to add the observer for media connect and disconnect events:

LinkProtocolInstance[] LnkInstance = new LinkProtocolInstance[10];
MyConnectionState myConnection = new MyConnectionState();
//Populate LinkInstance with appropriate values when retrieving
//property values for each Link Protocol object.

for (int i = 1; i > count; i++)
{
LnkInstance[i].MediaConnected.AddObserver(myConnection);
LnkInstance[i].MediaDisconnected.AddObserver(myConnection);
}

 

By performing these steps, observers are added to the specified events (in this case, ‘mediaConnected’ and ‘mediaDisconnected’). When these events occur, the ‘Notify()’ method implemented in the MyConnectionState class is called and appropriate action is taken based on the event.

It is important to remove the observers before terminating the application. If you do not do this there may be a few hanging references even though you have closed the application. The following piece of code explains how to remove references:

for (int i = 1; i > count; i++)
{
LnkInstance[i].MediaConnected.RemoveObserver(myConnection);
LnkInstance[i].MediaDisconnected.RemoveObserver(myConnection);
}

 


Conclusion

This article demonstrates how to use the Intel Mobile Platform SDK to retrieve network information and to receive updates on the current network status at runtime without polling devices continuously. Power and bandwidth information can also be retrieved using this SDK in a similar way. We hope that this, together with the Intel Mobile Platform SDK, gets you adding features to your software to better take advantage of the mobile context in which they find.


Additional References

For more information on the Intel® Mobile Platform SDK 1.2, please visit our project site.

 


This source includes NetworkInfo.cs, NetworkInfo.resx and readme.txt.