|
| 1 | +--- |
| 2 | +-api-id: T:Windows.Networking.NetworkOperators.NetworkOperatorTetheringSessionAccessPointConfiguration |
| 3 | +-api-type: winrt class |
| 4 | +--- |
| 5 | + |
| 6 | +# Windows.Networking.NetworkOperators.NetworkOperatorTetheringSessionAccessPointConfiguration |
| 7 | + |
| 8 | +<!-- |
| 9 | +public sealed class NetworkOperatorTetheringSessionAccessPointConfiguration |
| 10 | +--> |
| 11 | + |
| 12 | + |
| 13 | +## -description |
| 14 | + |
| 15 | +Represents a per-session tethering configuration. |
| 16 | + |
| 17 | +**NetworkOperatorTetheringSessionAccessPointConfiguration** contains the same fields and helper methods as [NetworkOperatorTetheringAccessPointConfiguration](./networkoperatortetheringaccesspointconfiguration.md) does, with several additions. |
| 18 | + |
| 19 | +## -remarks |
| 20 | + |
| 21 | +The purpose of this class is to specify the per-session tethering configuration when starting the tethering session via [StartTetheringAsync](./networkoperatortetheringmanager_starttetheringasync_1060696031.md). The additional members of this per-session-only class are typically values that aren't currently configurable by users via Windows **Settings**. |
| 22 | + |
| 23 | +## -see-also |
| 24 | + |
| 25 | +## -examples |
| 26 | + |
| 27 | +```csharp |
| 28 | +using System; |
| 29 | +using System.Threading.Tasks; |
| 30 | +using Windows.Foundation.Metadata; |
| 31 | +using Windows.Networking.NetworkOperators; |
| 32 | +using Windows.Networking.Connectivity; |
| 33 | + |
| 34 | +namespace TetheringApiDemoApp |
| 35 | +{ |
| 36 | + static class TetheringApiDemoClass |
| 37 | + { |
| 38 | + // Sample desired per-session access point configuration values. |
| 39 | + private const string DesiredSsid = "DemoSsid"; |
| 40 | + |
| 41 | + private const string DesiredPassphrase = "DemoPassphrase"; |
| 42 | + |
| 43 | + private const TetheringWiFiBand DesiredBand = |
| 44 | + TetheringWiFiBand.SixGigahertz; |
| 45 | + |
| 46 | + private const TetheringWiFiAuthenticationKind DesiredAuthenticationKind = |
| 47 | + TetheringWiFiAuthenticationKind.Wpa3; |
| 48 | + |
| 49 | + private const TetheringWiFiPerformancePriority DesiredPerformancePriority = |
| 50 | + TetheringWiFiPerformancePriority.TetheringOverStation; |
| 51 | + |
| 52 | + public static void VerifyPerSessionTetheringApiSupport() |
| 53 | + { |
| 54 | + if (!ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 17)) |
| 55 | + { |
| 56 | + throw new InvalidOperationException( |
| 57 | + "This OS doesn't support per-session tethering configurations."); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static NetworkOperatorTetheringManager GetTetheringManagerForCurrentConnection() |
| 62 | + { |
| 63 | + // Get the connection profile associated with the internet connection currently used by the local machine. |
| 64 | + ConnectionProfile currentConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); |
| 65 | + |
| 66 | + if (currentConnectionProfile == null) |
| 67 | + { |
| 68 | + throw new InvalidOperationException("Machine isn't connected to the internet."); |
| 69 | + } |
| 70 | + |
| 71 | + TetheringCapability tetheringCapability = |
| 72 | + NetworkOperatorTetheringManager.GetTetheringCapabilityFromConnectionProfile(currentConnectionProfile); |
| 73 | + |
| 74 | + if (tetheringCapability != TetheringCapability.Enabled) |
| 75 | + { |
| 76 | + throw new InvalidOperationException( |
| 77 | + $"Tethering is disabled on this machine. Reason code: {tetheringCapability}."); |
| 78 | + } |
| 79 | + |
| 80 | + return NetworkOperatorTetheringManager.CreateFromConnectionProfile(currentConnectionProfile); |
| 81 | + } |
| 82 | + |
| 83 | + public static async Task<NetworkOperatorTetheringSessionAccessPointConfiguration> |
| 84 | + SetUpSessionConfigurationAsync(NetworkOperatorTetheringManager tetheringManager) |
| 85 | + { |
| 86 | + NetworkOperatorTetheringSessionAccessPointConfiguration sessionConfiguration = |
| 87 | + new NetworkOperatorTetheringSessionAccessPointConfiguration(); |
| 88 | + |
| 89 | + sessionConfiguration.Ssid = DesiredSsid; |
| 90 | + sessionConfiguration.Passphrase = DesiredPassphrase; |
| 91 | + |
| 92 | + if (await sessionConfiguration.IsBandSupportedAsync(DesiredBand)) |
| 93 | + { |
| 94 | + sessionConfiguration.Band = DesiredBand; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + throw new InvalidOperationException("Desired band isn't supported."); |
| 99 | + } |
| 100 | + |
| 101 | + if (await sessionConfiguration.IsAuthenticationKindSupportedAsync(DesiredAuthenticationKind)) |
| 102 | + { |
| 103 | + sessionConfiguration.AuthenticationKind = DesiredAuthenticationKind; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + throw new InvalidOperationException("Desired authentication kind isn't supported."); |
| 108 | + } |
| 109 | + |
| 110 | + sessionConfiguration.PerformancePriority = DesiredPerformancePriority; |
| 111 | + |
| 112 | + return sessionConfiguration; |
| 113 | + } |
| 114 | + |
| 115 | + public static async Task StartTetheringSessionAsync( |
| 116 | + NetworkOperatorTetheringManager tetheringManager, |
| 117 | + NetworkOperatorTetheringSessionAccessPointConfiguration sessionConfiguration) |
| 118 | + { |
| 119 | + TetheringOperationStatus operationResult = |
| 120 | + await tetheringManager.StartTetheringAsync(sessionConfiguration); |
| 121 | + |
| 122 | + if (operationResult.Status == TetheringOperationStatus.Success) |
| 123 | + { |
| 124 | + Console.WriteLine("Tethering started successfully."); |
| 125 | + } |
| 126 | + else if (operationResult.Status == TetheringOperationStatus.AlreadyOn) |
| 127 | + { |
| 128 | + // Custom error message for AlreadyOn status. |
| 129 | + Console.WriteLine("Tethering is already on."); |
| 130 | + } |
| 131 | + else if (operationResult.Status == TetheringOperationStatus.RadioRestriction) |
| 132 | + { |
| 133 | + // Custom error message for RadioRestriction status. |
| 134 | + Console.WriteLine( |
| 135 | + "Can't start tethering at 6 GHz due to radio restrictions (2x2 + dual radio)."); |
| 136 | + } |
| 137 | + else if (operationResult.Status == TetheringOperationStatus.BandInterference) |
| 138 | + { |
| 139 | + // Custom error message for BandInterference status. |
| 140 | + Console.WriteLine( |
| 141 | + "Can't start tethering at 6 GHz because a 5 GHz connection interferes."); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + // Generic error message for all other statuses. |
| 146 | + Console.WriteLine( |
| 147 | + $"Failed to start tethering: {operationResult.AdditionalErrorMessage}."); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static async Task Main() |
| 152 | + { |
| 153 | + try |
| 154 | + { |
| 155 | + VerifyPerSessionTetheringApiSupport(); |
| 156 | + |
| 157 | + NetworkOperatorTetheringManager tetheringManager = GetTetheringManagerForCurrentConnection(); |
| 158 | + |
| 159 | + NetworkOperatorTetheringSessionAccessPointConfiguration sessionConfiguration = |
| 160 | + await SetUpSessionConfigurationAsync(tetheringManager); |
| 161 | + |
| 162 | + await StartTetheringSessionAsync(tetheringManager, sessionConfiguration); |
| 163 | + } |
| 164 | + catch (InvalidOperationException ex) |
| 165 | + { |
| 166 | + Console.WriteLine($"Failed to initialize tethering configuration: {ex.Message}."); |
| 167 | + } |
| 168 | + catch (Exception ex) |
| 169 | + { |
| 170 | + Console.WriteLine($"Unexpected error: {ex.Message}."); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
0 commit comments