Share via


NetworkOperatorTetheringSessionAccessPointConfiguration 類別

定義

包含用來設定 Wi-Fi 連線熱點以及相關協助程式方法的所有個別會話設定字段。

NetworkOperatorTetheringSessionAccessPointConfiguration 包含與其持續對應 NetworkOperatorTetheringAccessPointConfiguration 相同的欄位和協助程式方法,並新增數個。 但 NetworkOperatorTetheringSessionAccessPointConfiguration 的所有可設定屬性都會視為每個會話,包括所有共享屬性。

NetworkOperatorTetheringSessionAccessPointConfiguration 主要與 StartTetheringAsync 搭配使用,以指定每個會話的聯機設定。 這麼做不會清除或改變現有的持續性設定。 呼叫不含參數的 StartTetheringAsync 一律會使用透過 ConfigureAccessPointAsync 預先設定的持續性設定。

public ref class NetworkOperatorTetheringSessionAccessPointConfiguration sealed
/// [Windows.Foundation.Metadata.Activatable(1114112, "Windows.Foundation.UniversalApiContract")]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 1114112)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class NetworkOperatorTetheringSessionAccessPointConfiguration final
[Windows.Foundation.Metadata.Activatable(1114112, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 1114112)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class NetworkOperatorTetheringSessionAccessPointConfiguration
function NetworkOperatorTetheringSessionAccessPointConfiguration()
Public NotInheritable Class NetworkOperatorTetheringSessionAccessPointConfiguration
繼承
Object Platform::Object IInspectable NetworkOperatorTetheringSessionAccessPointConfiguration
屬性

Windows 需求

裝置系列
Windows 11 Insider Preview (已於 10.0.26100.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v19.0 引進)
應用程式功能
wiFiControl

範例

Windows.Networking.NetworkOperators 命名空間中的連線 API 提供一組完整的功能,可讓您以程式設計方式設定和控制連線 (,也就是與其他裝置共用裝置的因特網連線) 。 下列程式代碼範例示範如何使用每個會話的共享組態。

透過每個會話的共享設定,您可以暫時覆寫永續性共用熱點設定,而不會永久改變它。 這在需要暫存設定時特別有用。 例如,使用行動熱點將 HMD 連線到計算機時,但當您想要透過暫時 Wi-Fi 網路連接裝置,而不需向使用者顯示設定詳細數據,以及變更使用者目前的行動熱點設定,盡可能順暢地進行動作時。

此外,若要達到與計算機的低延遲和低抖動連線,您可能想要使用 6 GHz 連線。 如果使用者的計算機透過 5 GHz Wi-Fi 連線到因特網,則行動熱點無法在 6 GHz 頻帶上啟動。 因此,每個會話參數 PerformancePriority 會告知驅動程式如何設定連線熱點與月台連線之間的效能優先順序。 Default 常數會告知驅動程式將月台聯機的優先順序設定為其他所有專案。 另一方面, TetheringOverStation 會告知驅動程式排定連線熱點的效能優先順序,因此,讓驅動程式視需要將月台聯機降級至 2.4 GHz。 這一切都要盡可能為使用者提供順暢的體驗,而不需要他們個人變更熱點設定和月台連線。

下列程式代碼範例會展示:

  • API 支持驗證。 您的應用程式必須測試它是否在 上執行的操作系統支援每個會話的聯機組態。 若要這樣做,請使用 ApiInformation.IsApiContractPresent 方法。
  • 取得共用管理員。 程式代碼範例的本機定義 GetTetheringManagerForCurrentConnection 方法會識別正在使用中的因特網連線配置檔,並擷取對應至該配置檔的連線管理員。 稍後會使用共用管理員來擷取主要存取點設定,並啟動實際的共享會話。
  • 設定會話組態。 您可以從現有的主要組態衍生聯機會話設定,或從頭開始設定一個。 然後,您可以設定或修改各種參數,例如 SSID、複雜密碼、訊號範圍、驗證種類和效能優先順序。
  • 啟動共享工作階段。 會話設定可以傳遞至 StartTetheringAsync 方法來起始共享工作階段。 此方法也會透過 TetheringOperationStatus 列舉提供廣泛的意見反應,以提供對作業結果的細微瞭解。
using System;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Windows.Networking.NetworkOperators;
using Windows.Networking.Connectivity;

namespace TetheringApiDemoApp
{
  static class TetheringApiDemoClass
  {
    // Sample desired per-session access point configuration values.
    private const string DesiredSsid = "DemoSsid";

    private const string DesiredPassphrase = "DemoPassphrase";

    private const TetheringWiFiBand DesiredBand =
      TetheringWiFiBand.SixGigahertz;

    private const TetheringWiFiAuthenticationKind DesiredAuthenticationKind =
      TetheringWiFiAuthenticationKind.Wpa3;

    private const TetheringWiFiPerformancePriority DesiredPerformancePriority =
      TetheringWiFiPerformancePriority.TetheringOverStation;

    public static void VerifyPerSessionTetheringApiSupport()
    {
      if (!ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 17))
      {
        throw new InvalidOperationException(
          "This OS doesn't support per-session tethering configurations.");
      }
    }

    public static NetworkOperatorTetheringManager GetTetheringManagerForCurrentConnection()
    {
      // Get the connection profile associated with the internet connection currently used by the local machine.
      ConnectionProfile currentConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

      if (currentConnectionProfile == null)
      {
        throw new InvalidOperationException("Machine isn't connected to the internet.");
      }

      TetheringCapability tetheringCapability =
        NetworkOperatorTetheringManager.GetTetheringCapabilityFromConnectionProfile(currentConnectionProfile);

      if (tetheringCapability != TetheringCapability.Enabled)
      {
        throw new InvalidOperationException(
          $"Tethering is disabled on this machine. Reason code: {tetheringCapability}.");
      }

      return NetworkOperatorTetheringManager.CreateFromConnectionProfile(currentConnectionProfile);
    }

    public static async Task<NetworkOperatorTetheringSessionAccessPointConfiguration>
      SetUpSessionConfigurationAsync(NetworkOperatorTetheringManager tetheringManager)
    {
      NetworkOperatorTetheringSessionAccessPointConfiguration sessionConfiguration =
         new NetworkOperatorTetheringSessionAccessPointConfiguration();

      sessionConfiguration.Ssid = DesiredSsid;
      sessionConfiguration.Passphrase = DesiredPassphrase;

      if (await sessionConfiguration.IsBandSupportedAsync(DesiredBand))
      {
        sessionConfiguration.Band = DesiredBand;
      }
      else
      {
        throw new InvalidOperationException("Desired band isn't supported.");
      }

      if (await sessionConfiguration.IsAuthenticationKindSupportedAsync(DesiredAuthenticationKind))
      {
        sessionConfiguration.AuthenticationKind = DesiredAuthenticationKind;
      }
      else
      {
        throw new InvalidOperationException("Desired authentication kind isn't supported.");
      }

      sessionConfiguration.PerformancePriority = DesiredPerformancePriority;

      return sessionConfiguration;
    }

    public static async Task StartTetheringSessionAsync(
      NetworkOperatorTetheringManager tetheringManager,
      NetworkOperatorTetheringSessionAccessPointConfiguration sessionConfiguration)
    {
      TetheringOperationStatus operationResult =
        await tetheringManager.StartTetheringAsync(sessionConfiguration);

      if (operationResult.Status == TetheringOperationStatus.Success)
      {
        Console.WriteLine("Tethering started successfully.");
      }
      else if (operationResult.Status == TetheringOperationStatus.AlreadyOn)
      {
        // Custom error message for AlreadyOn status.
        Console.WriteLine("Tethering is already on.");
      }
      else if (operationResult.Status == TetheringOperationStatus.RadioRestriction)
      {
        // Custom error message for RadioRestriction status.
        Console.WriteLine(
          "Can't start tethering at 6 GHz due to radio restrictions (2x2 + dual radio).");
      }
      else if (operationResult.Status == TetheringOperationStatus.BandInterference)
      {
        // Custom error message for BandInterference status.
        Console.WriteLine(
          "Can't start tethering at 6 GHz because a 5 GHz connection interferes.");
      }
      else
      {
        // Generic error message for all other statuses.
        Console.WriteLine(
          $"Failed to start tethering: {operationResult.AdditionalErrorMessage}.");
      }
    }

    public static async Task Main()
    {
      try
      {
        VerifyPerSessionTetheringApiSupport();

        NetworkOperatorTetheringManager tetheringManager = GetTetheringManagerForCurrentConnection();

        NetworkOperatorTetheringSessionAccessPointConfiguration sessionConfiguration =
          await SetUpSessionConfigurationAsync(tetheringManager);

        await StartTetheringSessionAsync(tetheringManager, sessionConfiguration);
      }
      catch (InvalidOperationException ex)
      {
        Console.WriteLine($"Failed to initialize tethering configuration: {ex.Message}.");
      }
      catch (Exception ex)
      {
        Console.WriteLine($"Unexpected error: {ex.Message}.");
      }
    }
  }
}

備註

共用是一項功能,可讓 Windows 裝置作為行動熱點運作:透過 Wi-Fi 或藍牙提供其他裝置的因特網連線。 您可以在 Wi-Fi 連線熱點上設定四個永續性欄位:網路 SSID、網路密碼、網路無線網路頻 (例如 2.4 GHz、5 GHz、6 GHz) ,以及網路驗證演算法 (例如 WPA2、WPA3) 。 設定之後,所有欄位都會在共用工作階段之間保存;這表示值會儲存在非揮發性記憶體中。

但您也可以使用完全個別會話設定來啟動共享會話。 每個會話的共享組態不會在共享會話之間保存,也不會改變目前的持續性設定。 每個工作階段欄位是 效能優先順序

因此 ,NetworkOperatorTetheringSessionAccessPointConfiguration 的目的是在透過 StartTetheringAsync 啟動共享會話時指定個別會話的共享設定。 這個僅限會話類別的其他成員通常是使用者透過 Windows 設定目前無法設定的值。

建構函式

NetworkOperatorTetheringSessionAccessPointConfiguration()

建立 NetworkOperatorTetheringSessionAccessPointConfiguration 的實例。

屬性

AuthenticationKind

取得或設定要用於 Wi-Fi 連線的驗證種類。 類似於 NetworkOperatorTetheringAccessPointConfiguration.AuthenticationKind

Band

取得或設定要用於 Wi-Fi 連線的頻率帶。 類似於 NetworkOperatorTetheringAccessPointConfiguration.Band

Passphrase

取得或設定要用於 Wi-Fi 連線的網路複雜密碼。 類似於 NetworkOperatorTetheringAccessPointConfiguration.Passphrase

PerformancePriority

取得或設定當主要因特網連線也透過Wi-Fi時,要用於 Wi-Fi 連線的效能優先順序值。 如果站台聯機位於干擾所要求頻率帶的頻帶上,則 Wi-Fi 晶元組會嘗試將連接的頻率頻帶變更為不會干擾的另一個頻率帶。

許多 Wi-Fi 晶元組都有一項限制,其中如果主要連線超過 5 GHz 波段,則無法將連接熱點設定為使用 6 GHz 帶。 將連線的優先順序設定為 (TetheringOverStation) 會告訴 Wi-Fi 晶片組嘗試將主要連線變更為 2.4 GHz 波段,如此一來,就不會再干擾所要求 6 GHz 的網狀區域。

如果無法或藉由指定不同的效能優先順序值來允許頻率帶移位,則呼叫 StartTetheringAsync 將會失敗,並出現適當的結果狀態, (BandInterference) 。

Ssid

取得或設定要用於 Wi-Fi 連線的網路 SSID。 類似於 NetworkOperatorTetheringAccessPointConfiguration.Ssid

方法

IsAuthenticationKindSupported(TetheringWiFiAuthenticationKind)

取得值,指出 Wi-Fi 配接器是否允許使用特定驗證種類來設定連線熱點。 類似於 NetworkOperatorTetheringAccessPointConfiguration.IsAuthenticationKindSupported

IsAuthenticationKindSupportedAsync(TetheringWiFiAuthenticationKind)

以異步方式取得值,指出 Wi-Fi 配接器是否允許使用特定驗證種類來設定連接熱點。 類似於 NetworkOperatorTetheringAccessPointConfiguration.IsAuthenticationKindSupportedAsync

IsBandSupported(TetheringWiFiBand)

取得值,指出 Wi-Fi 配接器是否允許使用特定頻率帶設定連接熱點。 類似於 NetworkOperatorTetheringAccessPointConfiguration.IsBandSupported

IsBandSupportedAsync(TetheringWiFiBand)

以異步方式取得值,指出 Wi-Fi 配接器是否允許使用特定頻率帶設定連接熱點。 類似於 NetworkOperatorTetheringAccessPointConfiguration.IsBandSupportedAsync

適用於