0

I want to control RadioState of Wifi and Mobile(cellular) connection in Windows 10 x64 using custom application written in VB. It works for Wifi radio but doesn't for MobileBroadband.

Actually code does the same job as click on ActionCenter in Windows 10 and press Wifi or Cellular button.

 Private Async Sub TurnMobileOnOff(arg As Integer)
    Dim access = Await Windows.Devices.WiFi.WiFiAdapter.RequestAccessAsync
    Dim radios = Await Windows.Devices.Radios.Radio.GetRadiosAsync
    If access = Windows.Devices.WiFi.WiFiAccessStatus.Allowed Then
        For Each radio In radios
            If radio.Kind = Windows.Devices.Radios.RadioKind.MobileBroadband Then
                If arg = 1 Then
                    Await radio.SetStateAsync(Windows.Devices.Radios.RadioState.On)
                    RichTextBox1.AppendText(vbCrLf & "Mobile connection is turninng on")
                ElseIf arg = 0 Then
                    Await radio.SetStateAsync(Windows.Devices.Radios.RadioState.Off)
                    RichTextBox1.AppendText(vbCrLf & "Mobile connection is turninng off")
                End If
            End If
        Next

    End If
End Sub

There's no error message, just radiostate is unchanged. The same code works for WiFi as expected.

Source:https://learn.microsoft.com/en-us/uwp/api/windows.devices.radios.radiostate

Thank for any help.

theduck
  • 2,589
  • 13
  • 17
  • 23
ThePegass
  • 11
  • 2
  • Does the RichTextBox get updated `Mobile connection is turninng on`, `Mobile connection is turninng off`? – djv Oct 29 '19 at 15:15
  • Yes, it goes through `RadioState.On/Off` line, but state is never changed. – ThePegass Oct 29 '19 at 16:56
  • The [remarks section of the documentation](https://learn.microsoft.com/en-us/uwp/api/windows.devices.radios.radio.getradiosasync#remarks) points out that it will work only on a x64 machine when the application architecture is also x64, or `an x86 application running on an x64 architecture computer won't see any radio instances`. Make sure both the app and computer architecture match. – djv Oct 29 '19 at 20:36
  • And by doing `Await radio.SetStateAsync( ...)` you throw away the result, and possibly some valuable information. You can instead do `Dim result = Await radio.SetStateAsync(Windows.Devices.Radios.RadioState.On)` `RichTextBox1.AppendText($"{vbCrLf}Mobile connection turning on, result: {result}")` to see what the result of the operation is. The return type is [RadioAccessStatus](https://learn.microsoft.com/en-us/uwp/api/windows.devices.radios.radioaccessstatus): Allowed, DeniedBySystem, DeniedByUser, Unspecified. – djv Oct 29 '19 at 20:45
  • Thanks for your hints. System is x64 based and application is build as x64 platform as well. I changed code to see the result and I got: `DeniedBySystem` means lack of permissions. I tried run it as administrator, but it has no affect. – ThePegass Oct 30 '19 at 07:15
  • I must admit I have never touched this code in the past. Never even done UWP. But I'm researching for you. So are you sure `access` coming from `Devices.WiFi` is appropriate for `MobileBroadband`? RadioKind has both WiFi and MobileBroadband so it seems like they aren't the same thing. There is also [this post](https://stackoverflow.com/a/34646096/832052) in which the OP found that they needed to set a property of the radio prior to requesting access in order for a popup to appear. The issue was DeniedByUser which is not the same as your case but it's worth a try. – djv Oct 30 '19 at 15:54
  • Also see [this question](https://stackoverflow.com/q/37734995/832052) related to UWP, and [another answer](https://stackoverflow.com/a/6170507/832052) using WMI which should work for other versions of Windows in addition to W10. – djv Oct 30 '19 at 15:57
  • Hi. I think you are right that `access` seems represent only access to control `Devices.WiFi`, not `MobileBroadband`. I found WMI topic, but in this case it would disable/enable device using administrator privileges. I want just disable/enable software radio. Thanks to your link I found following on MS site (related to `Windows.Devices.Radios` namespace): _To use this namespace, you must specify the Radio device capability in the app package manifest. To learn more about device capabilities, see App Capability Declarations._ – ThePegass Oct 31 '19 at 08:21
  • I need to remark, that my application is not UWP but WindowsForm. So I don't have `Package.appxmanifest` file in my project where app capabilities have to be specified. I tried to created it manually, but I think it doesn't work wit WindowsForm app. So...the question is, how to get access to control `MobileBroadband` radio in WindowsForm app? I will keep searching...any other idea? – ThePegass Oct 31 '19 at 08:30
  • I was trying to set up your environment but I found that I could only do it with UWP. Could you share the details on how to set it up in WinForms? Any NuGet packages, SDKs, references, etc.? – djv Oct 31 '19 at 14:33
  • Yes, SDK references: `C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.WinMD` , `C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd` , `C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd` , `C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Networking.Connectivity.WwanContract\2.0.0.0\Windows.Networking.Connectivity.WwanContract.winmd` – ThePegass Oct 31 '19 at 19:33
  • `C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll` – ThePegass Oct 31 '19 at 19:35
  • All references of my project: [link](https://i.postimg.cc/s2yLrmF7/image.png) – ThePegass Oct 31 '19 at 20:24

0 Answers0