Use. NET Compact Framework 2.0 to generate a Wi-Fi discovery application

Source: Internet
Author: User
Tags compact hosting microsoft download center
Released on: 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 the source code library shared by. NET Compact Framework 2.0 and OpenNETCF to create an application that detects available wireless networks and retrieves configuration information from smart device wireless network adapters.

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 the configuration and status query code are consistent, you do not need to consider 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 provides a solution for problems that the. NET Compact Framework development community has encountered since 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 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/A";
}

// Dynamic Host Configuration Protocol (DHCP) information
bool dhcpEnabled = m_currentAdapter.DhcpEnabled;
if (dhcpEnabled)
{
lblDHCPEnabled.Text = "True";
lblLeaseObtained.Text =
m_currentAdapter.LeaseObtained.ToShortDateString();
lblLeaseExpires.Text =
m_currentAdapter.LeaseExpires.ToShortDateString(); ;
mnuRenew.Enabled = true;
}
else
{
lblDHCPEnabled.Text = "False";
lblLeaseObtained.Text = "N/A";
lblLeaseExpires.Text = "N/A";
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 NetworksBack 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.