The user’s machine may have a wired and a wireless network interface, and AIR 2.0 allows you to access the low-level Network information on the client’s machine through a class called NetworkInfo. The NetworkInfo class can be used to enumerate all the network interfaces on a machine. We would like to be able to access that low-level network interface and display the networks.
Create an example of displaying the user’s available networks
The user's machine may have a wired and a wireless network interface, and AIR 2.0 allows you to access the low-level Network information on the client's machine through a class called NetworkInfo. The NetworkInfo class can be used to enumerate all the network interfaces on a machine.
The NetworkInfo class finds information about the machine's network interfaces, such as the local IP address and wired or wireless networks. The application below finds all the interfaces on the machine and the IP addresses.
Create a new project File > New > Flex Project and name the project NetworkInformationExample (ensure you are using AIR 2.0 with Flex 4.0 SDK). We will create an example of displaying the user's available networks. See complete example below:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="700" height="300"
initialize="initializeHandler()">
<fx:Script>
<![CDATA[
import flash.net.InterfaceAddress;
import flash.net.NetworkInfo;
import flash.net.NetworkInterface;
import mx.collections.ArrayCollection;
private var arrayCollection:ArrayCollection = new
ArrayCollection();
protected function initializeHandler():void
{
var networkInfo:NetworkInfo =
NetworkInfo.networkInfo;
var
networkInterfaces:Vector.<NetworkInterface> =
networkInfo.findInterfaces();
networkInterfaces.forEach(
function(networkInterface:NetworkInterface, index:int,
vect:Vector.<NetworkInterface>):void
{
var item:Object = new Object();
item.active = networkInterface.active;
item.name = networkInterface.name;
if ( networkInterface.addresses.length > 0 )
{
networkInterface.addresses.forEach(
function( interfaceAddress:InterfaceAddress, i:int,
vect2:Vector.<InterfaceAddress>):void {
item.addresses +=
networkInterface.addresses[i].ipVersion + ": " +
networkInterface.addresses[i].address + ", ";
});
}
else
{
item.addresses = "";
}
arrayCollection.addItem( item );
});
dataGrid.dataProvider = new
ArrayCollection(arrayCollection.source);
}
]]>
</fx:Script>
<mx:DataGrid id="dataGrid" width="700" height="300">
<mx:columns>
<mx:DataGridColumn dataField="name" width="40" />
<mx:DataGridColumn dataField="active" width="80"
/>
<mx:DataGridColumn dataField="addresses" />
</mx:columns>
</mx:DataGrid>
</s:WindowedApplication>
Once the application gets initialized we call the
initializeHandler method and we iterate through the collection of
network available. Notice that NetworkInfo cannot be
initialized. We first loop to find all the
NetworkInterface and than iterate through the addresses
collection. We than add all that information to an array
collection that will be attached to a data grid to show the
results.
protected function initializeHandler():void
{
var networkInfo:NetworkInfo = NetworkInfo.networkInfo;
var networkInterfaces:Vector.<NetworkInterface> =
networkInfo.findInterfaces();
networkInterfaces.forEach(
function(networkInterface:NetworkInterface, index:int,
vect:Vector.<NetworkInterface>):void
{
var item:Object = new Object();
item.active = networkInterface.active;
item.name = networkInterface.name;
if ( networkInterface.addresses.length > 0 )
{
networkInterface.addresses.forEach( function(
interfaceAddress:InterfaceAddress, i:int,
vect2:Vector.<InterfaceAddress>):void {
item.addresses += networkInterface.addresses[i].ipVersion + ": " +
networkInterface.addresses[i].address + ", ";
});
}
else
{
item.addresses = "";
}
arrayCollection.addItem( item );
});
dataGrid.dataProvider = new
ArrayCollection(arrayCollection.source);
}
The data grid holds the names, status of the network and
address available.
<mx:DataGrid id="dataGrid"
width="700" height="300">
<mx:columns>
<mx:DataGridColumn dataField="name" width="40" />
<mx:DataGridColumn dataField="active" width="80" />
<mx:DataGridColumn dataField="addresses" />
</mx:columns>
</mx:DataGrid>
Where to go from here?
To learn more about this feature and more order AdvancED Flex 4
+