2

I'm using Managed Wifi to get the radio state of my Wifi adapter. How can I turn the radio ON in case it is actually off ?

Something like this :

WlanClient wlanClient = new WlanClient()
var targetInterface = wlanClient.Interfaces.FirstOrDefault()
if (targetInterface != null)
{
    bool radioIsOff = targetInterface .RadioState.PhyRadioState[0].dot11SoftwareRadioState == Wlan.Dot11RadioState.Off;
    if (radioIsOff)
    {
       // TODO
    }
}
JYL
  • 8,228
  • 5
  • 39
  • 63

2 Answers2

3

I just added this to the WlanInterface class of the Managed Wifi API :

        IntPtr radioStatePtr = new IntPtr(0L);
        try
        {
            Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
            radioState.dwPhyIndex = 0; // TODO : can change ???
            radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On; // ignored in fact, according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms706791(v=vs.85).aspx 
            radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.On;

            radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
            Marshal.StructureToPtr(radioState, radioStatePtr, false);

            Wlan.ThrowIfError(
                Wlan.WlanSetInterface(
                            client.clientHandle,
                            info.interfaceGuid,
                            Wlan.WlanIntfOpcode.RadioState,
                            (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)),
                            radioStatePtr,
                            IntPtr.Zero));
        }
        finally
        {
            if (radioStatePtr.ToInt64() != 0)
                Marshal.FreeHGlobal(radioStatePtr);
        }

Tested on Win 7.

JYL
  • 8,228
  • 5
  • 39
  • 63
0

I was struggling with this and I just want to share my solution

(download managed wifi recommended above) Add WlanApi.cs and Interop.cs to your project. Add using NativeWifi.

In WlanApi.cs change to: public IntPtr clientHandle; (You need the clientHandle. Not sure why it was set to private?)

Use this code:

string arg1 = "true"; //set to false if you want to turn it off.
      arg1 = arg1.ToLower();
            IntPtr radioStatePtr = new IntPtr(0L);
            try
            {
                WlanClient wc = new WlanClient();

                
                foreach (var iface in wc.Interfaces)
                {
                    //WlanInterface
                    if(iface.InterfaceName.ToLower()=="wifi")
                    { 
                    Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
                    radioState.dwPhyIndex = 0;
                    if(arg1=="true")
                    { 
                        radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On;
                        radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.On;
                    }
                    else
                    {
                        radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.Off;
                        radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.Off;
                    }
                    radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
                    Marshal.StructureToPtr(radioState, radioStatePtr, false);

                    Wlan.WlanSetInterface(wc.clientHandle, iface.InterfaceGuid, Wlan.WlanIntfOpcode.RadioState, (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)), radioStatePtr, IntPtr.Zero);
                    }

                }
               
            }
            finally
            {
                if (radioStatePtr.ToInt64() != 0)
                    Marshal.FreeHGlobal(radioStatePtr);
            }

Good luck :)

andla007
  • 21
  • 2