江貫榮
江貫榮

Reputation: 59

TetheringWiFiBand in Windows.Networking.NetworkOperators

I was trying to use Powershell script to set up windows 10 mobile hotspot (which is the function in "Settings" -> "Network & Internet" -> "Mobile hotspot"). I was able to turn it on through the following script:

[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] | Out-Null

# Define functions. Not important to this question
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
    $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
    $netTask = $asTask.Invoke($null, @($WinRtTask))
    $netTask.Wait(-1) | Out-Null
    $netTask.Result
}
Function AwaitAction($WinRtAction) {
    $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
    $netTask = $asTask.Invoke($null, @($WinRtAction))
    $netTask.Wait(-1) | Out-Null
}

# Create tethering manager
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)

# Create configuration
$configuration = new-object Windows.Networking.NetworkOperators.NetworkOperatorTetheringAccessPointConfiguration
$configuration.Ssid = "test"
$configuration.Passphrase = "12345678"

# ===================== My question is here =====================
[enum]::GetValues([Windows.Networking.NetworkOperators.TetheringWiFiBand])
$configuration | Get-Member 
# ===============================================================

# Check whether Mobile Hotspot is enabled
$tetheringManager.TetheringOperationalState

# Set Hotspot configuration
AwaitAction ($tetheringManager.ConfigureAccessPointAsync($configuration))


# Start Mobile Hotspot
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])

but I could only set the SSID and the Passphrase of the network but not the network band which we can choose between '5 GHz', '2.4 GHz' or 'Any available' in the setting gui.

I saw on this post (https://blogs.windows.com/windowsdeveloper/2019/09/10/windows-10-sdk-preview-build-18975-available-now/#DwOj8B0wPu5zd9hK.97) that it seems there is an enum 'TetheringWiFiBand' to set that starting from Windows 10 SDK, version 1903, build 18362 (it's exactly my windows version). However, as you can see, in the middle of my script, when I tried to access this enum, I got an error:

Unable to find type [Windows.Networking.NetworkOperators.TetheringWiFiBand].

Also, there is no such an enum when I print the member of Windows.Networking.NetworkOperators.NetworkOperatorTetheringAccessPointConfiguration

Does anyone have an idea of how to set the wifi band of the mobile hotspot based on this method? Thanks.

Upvotes: 4

Views: 2241

Answers (1)

江貫榮
江貫榮

Reputation: 59

For anyone who might be interested, the property of setting the tethering band has been added in WinRT Build 19041 (See: https://learn.microsoft.com/en-us/uwp/api/windows.networking.networkoperators.networkoperatortetheringaccesspointconfiguration?view=winrt-19041 and https://learn.microsoft.com/en-us/uwp/api/windows.networking.networkoperators.tetheringwifiband?view=winrt-19041)

So now you can add a line in the script posted in the question:

$configuration.Band = 1

to specify the tethering band to 2.4 GHz.

Upvotes: 2

Related Questions