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.
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.
Before we get started, here’s a brief run down of the architecture and design behind the Intel® Mobile Platform SDK:
Let’s now turn to the network tool which we are going to build in this article.
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.
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 AdapterThe 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 NotificationIn 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);
}
Click here to download source code. This source includes NetworkInfo.cs, NetworkInfo.resx and readme.txt.
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.