Search code examples
c#windowswindows-mobilewifiopennetcf

Re-connect to a specific AccessPoint using OpenNETCF


I am attempting to have an application for Windows Mobile CE re-connect to a specific WEP Enabled network if it becomes available. I believe that this code scans through the currently available access points to get a reference to the network I need:

                // we have communicated with the server, so save the access point for future reference
                INetworkInterface[] wniNet = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface wni in wniNet)
                {
                    if (wni is WirelessNetworkInterface)
                    {
                        WirelessNetworkInterface wWireless = wni as WirelessNetworkInterface;
                        //if (mwniServerConnection == null)
                        if (string.IsNullOrEmpty(msAccessPointName))
                        {
                            MessageBox.Show("assigning: " + wWireless.AssociatedAccessPoint.ToString() + " to instance var.", "testing");

                            // first attempt to save the WEP enabled access point to the preferred networks
                            //WirelessZeroConfigNetworkInterface wWifiConnection = new WirelessZeroConfigNetworkInterface();
                            //wWifiConnection.AddPreferredNetwork(wWireless.AssociatedAccessPoint.ToString(),
                            //    wWireless.InfrastructureMode,
                            //    "9876543210",
                            //    1,
                            //    wWireless.AuthenticationMode,
                            //    WEPStatus.WEPEnabled,
                            //    null);
                            mwniServerConnection = wWireless;
                            msAccessPointName = wWireless.AssociatedAccessPoint.ToString();
                        }
                    }
                }

I am a little out of my depth having found the OpenNETCF library yesterday, I'd like to ask if there are any examples of code that can test if the network ac is available, and to connect if it is.


Solution

  • I found that using adapter.SetWirelessSettings re-connects the hand held device to the specific access point found in order communicate on the specific network my server runs on:

    public static bool AttemptToReConnectAccessPoint()
    {
        bool wbRtn = false;
    
        // attempt to connect to the proper network
        if (U.mapAccessPoint != null)
        {
            // if we are currently on the right access point, we don't want to blow off the connection
            if (CheckIfAccessPointIsCurrent()) return true;
            foreach (OpenNETCF.Net.Adapter wAdapter in OpenNETCF.Net.Networking.GetAdapters())
            {
                bool wbFound = false;
                foreach (OpenNETCF.Net.AccessPoint wAccessPoint in wAdapter.NearbyAccessPoints)
                {
                    if (wAccessPoint.Name == U.mapAccessPoint.Name)
                    {
                        wbRtn = wAdapter.SetWirelessSettings(wAccessPoint.Name);
                        // give the re-connect script a second to finish
                        Thread.Sleep(1000);
                        Application.DoEvents();
                        break;
                    }
                }
                if (wbFound) break;
            }
        }
    
        return wbRtn;
    }