Release date: 2006-5-9 | Updated on: 2006-5-9
Applicable:
Microsoft. NET Compact framework1.0
Microsoft. NET Compact Framework 2.0
Windows Mobile-based devices
Abstract:Learn how to use. NET Compact Framework 2.0 and opennetcf to shareSource codeLibrary to create such an applicationProgramIt detects available wireless networks and retrieves configuration information from the smart device wireless network adapter.
Download build_wifi_discover_app_netcf2.msi from Microsoft download center.
Content on this page
Introduction
Opennetcf.org, smart device framework, and shared source license
Opennetcf. Net namespace
Background work
Summary
Introduction
In today's mobile devices and embedded devices, wireless Ethernet (Wi-Fi) network adapters are becoming more and more common, and wireless hotspots are almost everywhere in most technical fields.
Unfortunately, standard user interfaces (UI) and Wireless Zero Configuration (wzc) application programming interfaces (APIS) are found on Windows Mobile-based devices) set of available wireless networks is not easy.
Wzc is a set of standardized interfaces designed by Microsoft for wireless network interfaces. If the driver of a wireless card is designed to interact with wzc, you can use this standardized interface to control and query it, so that configuration and status queriesCodeStay consistent without considering the manufacturer of the adapter. Although not all wireless cards are compatible with wzc, most of them are compatible. This high adoption rate makes wzc a practical standard for Windows-based desktop computers and Windows Mobile operating systems. Unfortunately, there is no documentation on the wzc interface in Windows CE.
So what can embedded developers do? This article will show you how to use opennetcf.org's robust shared source code library instead of open source code to write a wide range of applications, so as to display network adapter properties and find nearby wireless access points. The sample application contains only about 200 lines of manually typed code, including comments, blank areas, and parentheses.
If you are interested in the operations behind the example application, this article providesDownload Sample Code(If you reference the opennetcf. net. dll assembly, the project runs normally ). This article also takes into account the complexity of the actual situation when developers perform four simple calls to the database.
Back to Top: opennetcf.org, smart device framework, and shared source license
Whenever third-party software is introduced into your solution, you must analyze the benefits and risks of using it. Therefore, before you learn more about the application in this example, you should understand the basic knowledge about opennetcf.org, smart device framework, and opennetcf shared source license.
First, opennetcf.org is a project operated by opennetcf consulting and LLP. It was initiated in the spirit of open source code. This project is developed for. NET Compact framework.CommunityProvide solutions to problems encountered starting from the first beta version.
The main product of the opennetcf.org project is the smart device framework, which is an extension class set that supplements. NET Compact Framework 1.0 and 2.0. The smart device framework provides many classes, attributes, and methods that can be used throughout the. NET Framework, as well as a major supplement to classes specific to the Windows CE environment.
The smart device framework is essentially a framework that enables. NET Compact framework developers can invest in the market for a short period of time (compared to using it separately. netcompact framework) provides more functional solutions. Best of all, you can get the complete source code-the source code is completely free; and the distribution license is very friendly. Opennetcf shared source license (for smart deviceframework versions 1.0 to 1.4) is very friendly for developers and businesses. This document does not provide a detailed description of it. However, the license is only one page; the language is concise; it has been proven to be available after review by lawyers from companies with a market value of billions of dollars. You can viewFull licenseIt basically states the following:
If you agree to provide an important value to the final product, you can use your application to use and distribute the smart device framework for free.
You can use the smart device framework as needed.
You can distribute opennetcf assembly as provided or merge it into your own product.
In your product documentation, you must declare the usage of the smart device framework.
You cannot use this framework as an independent product for simple compilation and sales.
You cannot blame opennetcf for problems caused by use.
The smartdevice framework has been downloaded nearly 200,000 times by users and has been used for nearly two years. Its size (in full format) is almost the same as that of. netcompact Framework 1.0. Although the smart device framework has been tested to a certain extent, as it is a community project, there may be some bugs, and some parts of the framework have received more development attention.
Now that you understand the risks, you can see the huge benefits of the smart device framework.
Back to Top: opennetcf. Net namespace
For the example application in this article, only one namespace in the smart device framework is used: "opennetcf. Net ". The sample source code is about 3,800 lines. If the code works normally (you will see it) and most of the code is used (you will do this ), you can immediately see that this can save weeks of development time.
This namespace contains a large number of classes for network use, including File Transfer Protocol (FTP), network statistics, and Bluetooth. However, this article mainly introduces the following four categories:AdapterAndAccesspoint, AndAdaptercollectionAndAccesspointcollection. As the name suggests, the last two classes are the collection classes of the first two classes.
Figure 1 shows a simple relationship between these classes.
Figure
1. Adapter
,
Accesspoint
And associated collection classes.
AdapterAndAdaptercollectionClass
AdapterClass indicates any Ethernet-compatible network adapter in the system. This adapter can be a wired or wireless network adapter, or other things that present the Ethernet layer to the Network Driver Interface Specification (NDIS) (for example, in a Universal Serial Bus (USB) microsoft ActiveSync connections that are active on the serial port or IRDA ).
As shown in figure 1 above,AdapterClass provides a large number of properties, most of which describe Internet Protocol (IP) configuration items (such as IP address, Gateway and subnet mask) or wireless connection information (such as signal strength ).
This example usesAdapterClass displays common adapter configurations for any adapter (wireless or wired.
The adaptercollection class is only a class derived from collectionbase. It provides a container for the adapter returned by the networking. getadapters call.
As shown in the code example above, you first need to obtain a list of all NDIS adapters that the device has.Networking. getadaptersReturnsAdaptercollectionClass generation. After the application is startedUpdateadaptersYou can obtain a list.UpdateadaptersObtainAdaptercollectionClass and fill the combo box with the adapter name.
Void updateadapters ()
{
// Get the available adapters
M_adapters = networking. getadapters ();
// Clear the combo
Cboadapters. Items. Clear ();
// Add the adapters
Foreach (adapter in m_adapters)
{
Cboadapters. Items. Add (adapter );
}
}
Each time the user changes the selected (or current) AdapterUpdateconfigUse the configuration information of the selected adapter to update the UI, as shown in the following code example.
Void updateconfig ()
{
Tabconfiguration. suspendlayout ();
// Update the adapter's configuration information
Lblmacaddress. Text =
Bitconverter. tostring (m_currentadapter.macaddress );
Lblipaddress. Text = m_currentadapter.currentipaddress;
Lblsubnet. Text = m_currentadapter.currentsubnetmask;
Lblgateway. Text = m_currentadapter.gateway;
// Wireless information
Bool wireless = m_currentadapter.iswireless;
If (Wireless)
{
Lbliswireless. Text = "true ";
Lblwzccompat. Text =
M_currentadapter.iswirelesszeroconfigcompatible.
Tostring ();
}
Else
{
Lbliswireless. Text = "false ";
Lblwzccompat. Text = "N/";
}
// Dynamic Host Configuration Protocol (DHCP) Information
Bool dhcpenabled = m_currentadapter.dhcpenabled;
If (dhcpenabled)
{
Lbldhcpenabled. Text = "true ";
Lblleaseobtained. Text =
M_currentadapter.leaseobtained.tow.datestring ();
Lblleaseexpires. Text =
M_currentadapter.leaseexpires.tow.datestring ();;
Mnurenew. Enabled = true;
}
Else
{
Lbldhcpenabled. Text = "false ";
Lblleaseobtained. Text = "N/";
Lblleaseexpires. Text = "N/";
Mnurenew. Enabled = false;
}
Tabconfiguration. resumelayout ();
}
Figure 2 shows the sample application running on the HP ipaq h5555 DeviceConfigurationTab. The selected adapter is a built-in 802.11b controller. When the device is connected through ActiveSync over the Transmission Control Protocol/Internet Protocol (TCP/IP), The ActiveSync connection is also enumerated as an adapter.
Figure
2. HP ipaq h5555
Built-in
802.11b
Adapter configuration information
The important attribute of this article isIswirelesszeroconfigcompatible. If a wireless adapter is incompatible with wzc, most of its attributes need to be accessed through a dedicated API call (usually not recorded or published), so that developers cannot create custom implementations or software. However, if an adapter is compatible with wzc (more and more adapters are used), you can use a set of standard Windows APIs to obtain a large amount of information about the adapter and the network access points it recognizes.
As mentioned above, no wzc API documentation has been officially written. However, Microsoft platform builder comes with the source code of the default network UI that is connected to the wzc-compatible adapter. This source code also provides methods to understand how APIs work (although valuable ).Opennetcf. net. accesspointClass in a user-friendly manner.
AccesspointAndAccesspointcollectionClass
Access points are the main nodes of interest in wireless networks. When you use an access point for a specific adapter, they are generally divided into two types: the access point that the adapter can recognize now, and the access point that the adapter previously identified and will be prioritized when it encounters again. These two lists are usually different, but they usually contain some identical access points. InAccesspointClass, these access points are defined as adjacent (usually called available) access points and preferred access points. The third important term of access point is associated access point, which is the access point currently used to run network traffic in this example. There may be no associated access points. For example, if you are not in the scope of any access point, but you are associated with the access point, the associated access point is always in the list of adjacent access points.
In this example, you obtain and display the following two lists: preferred access point and available access point. For the access points identified by the selected adapter, the signal strength is listed in decibels with a textual description (suchExcellent,Good,FairOrPoor).
As you can see in figure 3, the current example contains two available access points, both of which have good strength because they are less than 1 feet away from these access points.AccesspointClass also has several other attributes for providing information about its functions and privacy.
Figure
3.
Available (nearby) Wireless Networks Back to Top
Although the code used to create the wifidiscovery application is very simple, it masks the actual work on the. NET Compact framework layer in the opennetcf library. PairNetworking. getadaptersUse platform call to call the iphlpapi. dll fileGetadaptersinfoFunction, which returns an array of local structures-each local structure corresponds to a network adapter (wireless or wired) registered to the system ).
These structures are internally sent from the local APIIp_adapter_infoClass, which is the way the API returns data. EachIp_adapter_infoClass conversion to public hostingAdapterClass to provide general information (such as the adapter name, MAC address, and IP Address Configuration) exposed by all adapters ).
If the adapter is reported as an Ethernet Adapter, then query the adapter to see if it is wireless and call the wzcsapi. dll fileWzcqueryinterfaceAPI to check whether it supports the wzc interface.
If an adapter is found to be wireless, you can use the NDIS driver of the device (use the platform again) to query a large amount of information about it.
If you also find that an adapter is compatible with wzc, You can derive more information by calling Platform calls with different parametersWzcqueryinterfaceAPI. For example, you can query the available access point array or the associated Access Point array.
If you query neighboring access points, it is even more complex because the Platform calls the NDIS driver.
As a brief example of results generated using only a few simple calls, when you want to find the access point of the first adapter in the system, please look at the data flow in the system. In wifidiscovery.exe, the code is shown in the following four simple lines of code:
Adaptercollection adapters = networking. getadapters ();
Adapters [0]. iswireless;
Adapters [0]. iswirelesszeroconfigcompatible;
Accesspointcollection APS = adapters [0]. nearbyaccesspoints;
Figure 4 shows the actual operations in the system.
Figure
4.
This API is used to obtain a list of available (adjacent) wireless networks of an adapter. Back to Top Summary
. NET Compact framework and managed code are usually designed to improve the efficiency of developers. High efficiency means faster market input and richer feature sets. It means lower cost solutions, lower opportunity costs, and more resources available for other projects.
Although the sample application described in this article is not as robust as a complete wireless application (for example, it does not have an icon for the Access Point infrastructure and cannot change the Access Point Association ), but it can indeed test the production efficiency. By using a free shared source code library, you have created a basic Wi-Fi discovery application with less than 200 lines of code. The application displays all available adapters. For wireless adapters, this application displays all available and preferred access points and their signal strength. In addition, this article includes seven lines of code to update the DHCP address of any adapter (if the expected result is a more basic UI, only one line of code is required ).
Hosting code is not a solution to every business problem. C/C ++ certainly has a place, but in today's fast-paced business environment, developers must always serve customers with high efficiency. Hosted code (along with the rapid application development tool provided by microsoftvisual Studio 2005) provides a solid foundation for developers to compete successfully in the market.