Search code examples
c#windows-mobileopennetcf

Why doesn't the wireless radio state refresh when I change it?


I am trying to make a simple application to toggle the WiFi radio on a windows mobile device and have found that the OpenNETCF.WIndowsMobile namespace provides some gems to make this task super easy:

    private void button2_Click(object sender, EventArgs e)
    {
         var wifiRadio = (from radio in Radios.GetRadios()
               where radio.RadioType == RadioType.WiFi
               select radio).FirstOrDefault();

        if (wifiRadio != null)
            switch (wifiRadio.RadioState)
            {
                case RadioState.Off:
                    wifiRadio.RadioState = RadioState.On;
                    button2.Text = "Is On";
                    break;
                case RadioState.On:
                    wifiRadio.RadioState = RadioState.Off;
                    button2.Text = "Is Off";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }                            
    }

That works perfectly on the first click. On all subsequent clicks, the RadioState still reports as the same (even though the radio has been toggled).

After restarting the application, the RadioState will again return the correct state.

Is there a way to refresh the state so it reports correctly?


Solution

  • I discovered that there is a refresh method. If anyone else needs to know here's how you can utilize it:

            private void button2_Click(object sender, EventArgs e)
        {
            var radios = Radios.GetRadios();
            radios.Refresh();
    
            var wifiRadio = (from radio in radios
                   where radio.RadioType == RadioType.WiFi
                   select radio).FirstOrDefault();
    
            if (wifiRadio != null)
                switch (wifiRadio.RadioState)
                {
                    case RadioState.Off:
                        wifiRadio.RadioState = RadioState.On;
                        button2.Text = "Is On";
                        break;
                    case RadioState.On:
                        wifiRadio.RadioState = RadioState.Off;
                        button2.Text = "Is Off";
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }                            
        }
    }