Search code examples
c#.netinteropwifi

How can I turn ON radio of a Wifi adapter that is actually OFF?


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
    }
}

Solution

  • 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.