Introduction
This article explains how we can bypass the Connection Planner and establish a connection using the Connection Manager APIs on Windows Mobile devices. A new application block Connection Manager is created which contains a wrapper to the Connection Manager native APIs on Windows Mobile devices. Also, the ConnectionMonitor
application block, provided by the Microsoft Patterns & Practices group, is enhanced to retrieve all the connections available on the device.
What is the Connection Manager in Windows Mobile?
Connection Manager, which you can implement by using the Connection Manager API, centralizes and automates the establishment and management of various types of network connections for applications that connect to the Internet or to the corporate network from Windows Mobile devices. When one or more applications request a connection, the Connection Manager establishes a connection using an optimal connection type, and notifies all the applications that the connection is active. Applications are configured to specify a connection name and a network name.
The Connection Manager tracks which connections are in use or are requested by applications. It closes unused connections, automatically disconnects connections when they are idle for a specified period of time, and closes low-priority connections to open high-priority connections. For example, voice connections are typically given a higher priority than data connections.
The Connection Manager handles many different types of connections, including Point-to-Point Protocol (PPP), Remote Access Service (RAS), and Point-to-Point Tunneling Protocol (PPTP) connections. The Connection Manager can also configure proxy server settings to allow network resources through a firewall or a Wireless Application Protocol (WAP) gateway.
Fig. Connection Manager Architecture
Connection Monitor Application Block
This application block is provided by the Microsoft Patterns & Practices group. This application block allows your application to retrieve connectivity information from a mobile device and monitor connectivity status changes.
Connection Manager Functions
The Connection Manager API consists of about 11 functions, but you only need two of these functions to know the connection status.
Function |
Description |
ConnMgrEnumDestinations
|
This function enumerates the available networks.
|
ConnMgrQueryDetailedStatus
|
This function returns detailed information about the existing data connections on the Windows Mobile device.
|
Connection Manager Application Block
This application block provides Managed APIs to establish or release connection from a mobile device. This application block bridges the gap in the managed code which allows the user to manage his connections.
Connection Manager Functions
The Connection Manager API consists of about 11 functions, but you only need six of these functions for the tasks of establishing and releasing a network connection. In many cases, your application may require as few as two of these functions. The following table shows six functions:
Function |
Description |
ConnMgrMapURL
|
Retrieves the network identifier (Internet or Work) for the specified URL.
|
ConnMgrEstablishConnection
|
Selects and establishes the appropriate connection for the specified network identifier. This method returns without waiting for the connection attempt to complete; use ConnMgrConnectionStatus to determine the connection status.
|
ConnMgrEstablishConnectionSync
|
Selects and establishes the appropriate connection for the specified network identifier. This method does not return until the connection attempt completes.
|
ConnMgrReleaseConnection
|
Releases the specified connection, which may also close the connection.
|
ConnMgrConnectionStatus
|
Retrieves the status of the specified connection.
|
ConnMgrMapConRef
|
This function maps a connection reference to its corresponding GUID. This function enables you to bypass the Connection Planner by explicitly providing a GUID that will map to the connection.
|
Using the Code
Display Connection Status
CMConsole is the sample code which shows how a user can use the Connection Monitor and Connection Manager application blocks. This part of the Connection Monitor sample was provided by the Microsoft Patterns & Practices group, and has been enhanced to show how we can use the Connection Manager application block to establish a connection.
public ConsoleForm()
{
InitializeComponent();
monitor = ConnectionMonitorFactory.CreateFromConfiguration();
monitor.ActiveNetworkChanged += NetworkChangeHandler;
}
The console screen shows the different connections, Cell Connection, NIC Connection, and Desktop connection which are defined in the app.config. ConnectionMonitorFactory.CreateFromConfiguration()
returns a ConnectionMonitor
object which keeps track of the status of the various connections specified in the configuration file.
The ConnectionMonitorFactory
object (monitor) retrieves a list of connections to be monitored from the app.config file. Internally, this object registers for SystemProperty
for each of these connections. The ActiveNetworkChanged
event indicates any one connection status has been changed so that we can update the connection list.
Fig. Display Connection List
Bypass Connection Planner
In the ConnectionList screen, it is implemented how we can bypass the connection planner in this code sample. It also explains how we can use the Connection Manager application block. ConnectionMonitorNativeHelper.GetConnectionList()
retrieves all the available connections on the device. It is displayed in the screen below. We can select a particular connection, say, “MY GPRS Connection”, and select the “Connect” menu in this sample to connect to the selected connection.
Note:- You can create a new GPRS connection on your WM 6.0 device emulator and test it with a Cellular Emulator.
Fig. List of all the available connections on the mobile device
private void mnuConnect_Click(object sender, EventArgs e)
{
const int CONNMONITOR_STATUS_CONNECTED = 0x10;
ConnectionMonitorNativeHelper.CONNMONITOR_CONNECTION_DETAILED_STATUS cnStatus =
cnList[lstConnection.SelectedIndex];
if (_connectionHandle != IntPtr.Zero)
{
ConnMgrReleaseConnection(_connectionHandle, 0);
_connectionHandle = IntPtr.Zero;
}
if (cnStatus.dwConnectionStatus != CONNMONITOR_STATUS_CONNECTED)
{
ConnMgrStatus status = ConnMgrStatus.Unknown;
Guid forceConnGuid = new Guid();
if (ConnMgrMapConRef(ConnMgrConRefTypeEnum.ConRefType_NAP,
cnStatus.pszDescription, ref forceConnGuid) != 0)
{
ConnMgrMapConRef(ConnMgrConRefTypeEnum.ConRefType_PROXY,
cnStatus.pszDescription, ref forceConnGuid);
}
ConnMgrConnectionInfo info = new ConnMgrConnectionInfo(forceConnGuid,
ConnMgrPriority.HighPriorityBackground);
ConnMgrEstablishConnectionSync(info, ref _connectionHandle,
_syncConnectTimeout, ref status);
if (status == ConnMgrStatus.Connected)
MessageBox.Show("Connect Succeeded");
else
MessageBox.Show("Connect failed: " + status.ToString());
}
}
Fig. After a connection was successfully made
References
- Establishing Network Connectivity with the Windows Mobile Connection Manager
- Connection Manager Application Development for Windows Mobile-based Devices
- Smart Client Software Factory