1

I'm looking for an API to programmatically toggle a wireless radio on and off in Windows, just like the following Windows 10 UI element does:

enter image description here

  • Need to toggle on/off Wi-Fi, Bluetooth, or mobile broadband
  • For Windows 7, 8 and 10
  • This is NOT a phone
  • C#/.NET4.5 would be ideal, but C++/Win32 would work too
William Leara
  • 10,595
  • 4
  • 36
  • 58

3 Answers3

6

For Windows 10, you can use the Radio Manager APIs to control different radio states. You can find the full sample apps here (both C# and C++).

First you need to get access to all of the system radios. This must be called in a UI thread:

var accessLevel = await Radio.RequestAccessAsync();

Then, you can find all radios on the system (the sample describes other ways to access the radios):

var radios = await Radio.GetRadiosAsync();

Given a radio object, you can then change state by the following:

Radio radio = SOME_RADIO;
radio.StateChanged = Radio_StateChangedCallback; // Called when the radio state completes the change
radio.SetStateAsync(RadioState.On); // Or RadioState.Off
Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68
Carter
  • 3,053
  • 1
  • 17
  • 22
  • thanks a lot! Unfortunately I have to support Win8 too, and your example is a Win10-only Universal app. (if I'm understanding correctly) :( – William Leara Jun 09 '16 at 23:26
  • Yes, I'm pretty sure this is only for Windows 10. This [post](http://stackoverflow.com/questions/24235524/are-there-apis-to-enable-disable-bluetooth-on-windows-8-1) might help for 8.1 though! I was hoping to at least get you part of the way :) – Carter Jun 09 '16 at 23:32
0

So in general what you need to do is to enumerate the network adapters (filter out Wireless, Bluetooth and Mobile Broadband) and toggle their state?

No wonders that there are 3 close votes since SO is full of related questions and answers:

  • This demonstrates C# + WMI
  • This demonstrates C++ + WINAPI Device Installation API

One of those should be a fair starting point to put together what you need.

Community
  • 1
  • 1
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • I didn't ask how to disable the the network **device**, I asked how to toggle the **radio**--big difference. When you toggle the switches in the Windows UI (see screenshot) the device is not removed from Windows (i.e. from the Device Manager), the switch simply toggles the radio on/off. All of the examples you mention are disabling the device. – William Leara Jun 09 '16 at 23:16
  • @WilliamLeara I may be wrong but since you mentioned Windows 7 - does it even distinguish enabling/disabling the device and turning the radio on and off? And disabling the device does not remove it, it does the same as you would disable it from the context menu in Control Panel. Windows 8 and up of course provide APIs to turn the radio on/off but if you need Windows 7 compatibility, then I'm not sure if there is anything better then disabling the device. – Rudolfs Bundulis Jun 10 '16 at 09:52
-1

Well I know you can manipulate the wireless connection with

netsh interface set interface name="Local Area Connection" via cmd line.

So maybe in c# you could do a Process.Start("netsh", "set name=\"Local Area Connection\""); to change the wifi settings. I'm sure you could download a bluetooth cmd line utility and do something similar with that.

I'll be able to test later when I get on my windows machine, but I can't right now, hopefully this helps.

Steezens
  • 66
  • 4