1
- using System ;
1
+ using log4net ;
2
+ using System ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
4
5
using System . Threading . Tasks ;
5
6
using Windows . Devices . WiFi ;
6
7
using Windows . Networking . Connectivity ;
7
8
using Windows . Security . Credentials ;
8
- using log4net ;
9
-
10
- //https://docs.microsoft.com/en-us/uwp/api/windows.devices.wifi.wifiadapter.requestaccessasync
11
- //var access = await WiFiAdapter.RequestAccessAsync() == WiFiAccessStatus.Allowed;
12
9
13
10
namespace NETworkManager . Models . Network ;
14
11
15
12
/// <summary>
16
- /// Class with WiFi related methods.
13
+ /// Class with Wi-Fi related methods.
17
14
/// </summary>
18
15
public static class WiFi
19
16
{
20
17
private static readonly ILog Log = LogManager . GetLogger ( typeof ( WiFi ) ) ;
21
18
22
19
/// <summary>
23
- /// Get all WiFi adapters async with additional information from <see cref="NetworkInterface" />.
20
+ /// Get all Wi-Fi adapters async with additional information from <see cref="NetworkInterface" />.
24
21
/// </summary>
25
22
/// <returns>
26
23
/// <see cref="WiFiAdapterInfo" /> with <see cref="NetworkInterface" /> and <see cref="WiFiAdapter" /> as
@@ -58,7 +55,7 @@ public static async Task<List<WiFiAdapterInfo>> GetAdapterAsync()
58
55
}
59
56
60
57
/// <summary>
61
- /// Get all available WiFi networks for an adapter with additional information's.
58
+ /// Get all available Wi-Fi networks for an adapter with additional information's.
62
59
/// </summary>
63
60
/// <param name="adapter">WiFi adapter as <see cref="WiFiAdapter" />.</param>
64
61
/// <returns>A report as <see cref="WiFiNetworkScanInfo" /> including a list of <see cref="WiFiNetworkInfo" />.</returns>
@@ -67,7 +64,7 @@ public static async Task<WiFiNetworkScanInfo> GetNetworksAsync(WiFiAdapter adapt
67
64
// Scan network adapter async
68
65
await adapter . ScanAsync ( ) ;
69
66
70
- // Try to get the current connected wifi network of this network adapter
67
+ // Try to get the current connected Wi-Fi network of this network adapter
71
68
var ( _, bssid ) = TryGetConnectedNetworkFromWiFiAdapter ( adapter . NetworkAdapter . NetworkAdapterId . ToString ( ) ) ;
72
69
73
70
var wifiNetworkInfos = adapter . NetworkReport . AvailableNetworks . Select ( availableNetwork => new WiFiNetworkInfo
@@ -86,14 +83,14 @@ public static async Task<WiFiNetworkScanInfo> GetNetworksAsync(WiFiAdapter adapt
86
83
}
87
84
88
85
/// <summary>
89
- /// Try to get the current connected wifi network (SSID and BSSID) of a network adapter from
86
+ /// Try to get the current connected Wi-Fi network (SSID and BSSID) of a network adapter from
90
87
/// netsh.exe.
91
88
/// Calling netsh.exe and parsing the output feels so dirty, but Microsoft's API returns only
92
89
/// the WLAN profile and the SSID of the connected network. The BSSID is needed to find a
93
90
/// specific access point among several.
94
91
/// </summary>
95
- /// <param name="adapterId">GUID of the WiFi network adapter.</param>
96
- /// <returns>SSID and BSSID of the connected wifi network. Values are null if not detected.</returns>
92
+ /// <param name="adapterId">GUID of the Wi-Fi network adapter.</param>
93
+ /// <returns>SSID and BSSID of the connected Wi-Fi network. Values are null if not detected.</returns>
97
94
// ReSharper disable once UnusedTupleComponentInReturnValue
98
95
private static ( string SSID , string BSSID ) TryGetConnectedNetworkFromWiFiAdapter ( string adapterId )
99
96
{
@@ -151,10 +148,10 @@ private static (string SSID, string BSSID) TryGetConnectedNetworkFromWiFiAdapter
151
148
}
152
149
153
150
/// <summary>
154
- /// Connect to a WiFi network with Pre-shared key, EAP or no security.
151
+ /// Connect to a Wi-Fi network with Pre-shared key, EAP or no security.
155
152
/// </summary>
156
- /// <param name="adapter">WiFi adapter which should be used for the connection.</param>
157
- /// <param name="network">WiFi network to connect to.</param>
153
+ /// <param name="adapter">Wi-Fi adapter which should be used for the connection.</param>
154
+ /// <param name="network">Wi-Fi network to connect to.</param>
158
155
/// <param name="reconnectionKind">Reconnection type to automatically or manuel reconnect.</param>
159
156
/// <param name="credential">Credentials for EAP or PSK. Empty for open networks.</param>
160
157
/// <param name="ssid">SSID for hidden networks.</param>
@@ -178,10 +175,10 @@ public static async Task<WiFiConnectionStatus> ConnectAsync(WiFiAdapter adapter,
178
175
}
179
176
180
177
/// <summary>
181
- /// Connect to a WiFi network with WPS push button.
178
+ /// Connect to a Wi-Fi network with WPS push button.
182
179
/// </summary>
183
- /// <param name="adapter">WiFi adapter which should be used for the connection.</param>
184
- /// <param name="network">WiFi network to connect to.</param>
180
+ /// <param name="adapter">Wi-Fi adapter which should be used for the connection.</param>
181
+ /// <param name="network">Wi-Fi network to connect to.</param>
185
182
/// <param name="reconnectionKind">Reconnection type to automatically or manuel reconnect.</param>
186
183
/// <returns></returns>
187
184
public static async Task < WiFiConnectionStatus > ConnectWpsAsync ( WiFiAdapter adapter , WiFiAvailableNetwork network ,
@@ -199,25 +196,24 @@ public static async Task<WiFiConnectionStatus> ConnectWpsAsync(WiFiAdapter adapt
199
196
}
200
197
201
198
/// <summary>
202
- /// Disconnect the wifi adapter from the current wifi network.
199
+ /// Disconnect the Wi-Fi adapter from the current Wi-Fi network.
203
200
/// </summary>
204
- /// <param name="adapter">WiFi adapter from which the wifi network should be disconnected.</param>
201
+ /// <param name="adapter">Wi-Fi adapter from which the Wi-Fi network should be disconnected.</param>
205
202
public static void Disconnect ( WiFiAdapter adapter )
206
203
{
207
204
adapter . Disconnect ( ) ;
208
205
}
209
206
210
207
/// <summary>
211
- /// Get the connect mode of a wifi network like Open, Eap (WPA2-Enterprise)
208
+ /// Get the connect mode of a Wi-Fi network like Open, Eap (WPA2-Enterprise)
212
209
/// or Psk (WPA2-Personal).
213
210
/// </summary>
214
- /// <param name="network">WiFi network as <see cref="WiFiAvailableNetwork" />.</param>
211
+ /// <param name="network">Wi-Fi network as <see cref="WiFiAvailableNetwork" />.</param>
215
212
/// <returns>Connect mode as <see cref="WiFiConnectMode" />.</returns>
216
213
public static WiFiConnectMode GetConnectMode ( WiFiAvailableNetwork network )
217
214
{
218
215
// Enterprise
219
- if ( network . SecuritySettings . NetworkAuthenticationType == NetworkAuthenticationType . Rsna ||
220
- network . SecuritySettings . NetworkAuthenticationType == NetworkAuthenticationType . Wpa )
216
+ if ( network . SecuritySettings . NetworkAuthenticationType is NetworkAuthenticationType . Rsna or NetworkAuthenticationType . Wpa )
221
217
return WiFiConnectMode . Eap ;
222
218
223
219
// Open
@@ -230,11 +226,11 @@ public static WiFiConnectMode GetConnectMode(WiFiAvailableNetwork network)
230
226
}
231
227
232
228
/// <summary>
233
- /// Check if WPS is available for a wifi network.
229
+ /// Check if WPS is available for a Wi-Fi network.
234
230
/// </summary>
235
231
/// <param name="adapter">WiFi adapter as <see cref="WiFiAdapter" />.</param>
236
232
/// <param name="network">WiFi network as <see cref="WiFiAvailableNetwork" />.</param>
237
- /// <returns></returns>
233
+ /// <returns>Ture if WPS is available. </returns>
238
234
public static async Task < bool > IsWpsAvailable ( WiFiAdapter adapter , WiFiAvailableNetwork network )
239
235
{
240
236
var result = await adapter . GetWpsConfigurationAsync ( network ) ;
@@ -243,7 +239,7 @@ public static async Task<bool> IsWpsAvailable(WiFiAdapter adapter, WiFiAvailable
243
239
}
244
240
245
241
/// <summary>
246
- /// Get the WiFi channel from channel frequency.
242
+ /// Get the Wi-Fi channel from channel frequency.
247
243
/// </summary>
248
244
/// <param name="kilohertz">Input like 2422000 or 5240000.</param>
249
245
/// <returns>WiFi channel like 3 or 48.</returns>
@@ -252,7 +248,7 @@ public static int GetChannelFromChannelFrequency(int kilohertz)
252
248
return ConvertChannelFrequencyToGigahertz ( kilohertz ) switch
253
249
{
254
250
// 2.4 GHz
255
- 2.412 => 1 ,
251
+ 2.412 => 1 ,
256
252
2.417 => 2 ,
257
253
2.422 => 3 ,
258
254
2.427 => 4 ,
@@ -265,32 +261,33 @@ public static int GetChannelFromChannelFrequency(int kilohertz)
265
261
2.462 => 11 ,
266
262
2.467 => 12 ,
267
263
2.472 => 13 ,
264
+ 2.484 => 14 , // Most countries do not allow this channel
268
265
// 5 GHz
269
- 5.180 => 36 ,
270
- 5.200 => 40 ,
271
- 5.220 => 44 ,
272
- 5.240 => 48 ,
273
- 5.260 => 52 ,
274
- 5.280 => 56 ,
275
- 5.300 => 60 ,
276
- 5.320 => 64 ,
277
- 5.500 => 100 ,
278
- 5.520 => 104 ,
279
- 5.540 => 108 ,
280
- 5.560 => 112 ,
281
- 5.580 => 116 ,
282
- 5.600 => 120 ,
283
- 5.620 => 124 ,
284
- 5.640 => 128 ,
285
- 5.660 => 132 ,
286
- 5.680 => 136 ,
287
- 5.700 => 140 ,
288
- 5.720 => 144 ,
289
- 5.745 => 149 ,
290
- 5.765 => 153 ,
291
- 5.785 => 157 ,
292
- 5.805 => 161 ,
293
- 5.825 => 165 ,
266
+ 5.180 => 36 , // UNII-1
267
+ 5.200 => 40 , // UNII-1
268
+ 5.220 => 44 , // UNII-1
269
+ 5.240 => 48 , // UNII-1
270
+ 5.260 => 52 , // UNII-2, DFS
271
+ 5.280 => 56 , // UNII-2, DFS
272
+ 5.300 => 60 , // UNII-2, DFS
273
+ 5.320 => 64 , // UNII-2, DFS
274
+ 5.500 => 100 , // UNII-2 Extended, DFS
275
+ 5.520 => 104 , // UNII-2 Extended, DFS
276
+ 5.540 => 108 , // UNII-2 Extended, DFS
277
+ 5.560 => 112 , // UNII-2 Extended, DFS
278
+ 5.580 => 116 , // UNII-2 Extended, DFS
279
+ 5.600 => 120 , // UNII-2 Extended, DFS
280
+ 5.620 => 124 , // UNII-2 Extended, DFS
281
+ 5.640 => 128 , // UNII-2 Extended, DFS
282
+ 5.660 => 132 , // UNII-2 Extended, DFS
283
+ 5.680 => 136 , // UNII-2 Extended, DFS
284
+ 5.700 => 140 , // UNII-2 Extended, DFS
285
+ 5.720 => 144 , // UNII-2 Extended, DFS
286
+ 5.745 => 149 , // UNII-3
287
+ 5.765 => 153 , // UNII-3
288
+ 5.785 => 157 , // UNII-3
289
+ 5.805 => 161 , // UNII-3
290
+ 5.825 => 165 , // UNII-3
294
291
_ => - 1
295
292
} ;
296
293
}
@@ -306,69 +303,82 @@ public static double ConvertChannelFrequencyToGigahertz(int kilohertz)
306
303
}
307
304
308
305
/// <summary>
309
- /// Check if the WiFi network is a 2.4 GHz network.
306
+ /// Check if the Wi-Fi network is a 2.4 GHz network.
310
307
/// </summary>
311
308
/// <param name="kilohertz">Frequency in kilohertz like 2422000 or 5240000.</param>
312
- /// <returns>True if WiFi network is 2.4 GHz.</returns>
309
+ /// <returns>True if Wi-Fi network is 2.4 GHz.</returns>
313
310
public static bool Is2dot4GHzNetwork ( int kilohertz )
314
311
{
315
312
var x = ConvertChannelFrequencyToGigahertz ( kilohertz ) ;
316
313
317
- return x is >= 2.412 and <= 2.472 ;
314
+ return x is >= 2.412 and <= 2.484 ;
318
315
}
319
316
320
317
/// <summary>
321
- /// Check if the WiFi network is a 5 GHz network.
318
+ /// Check if the Wi-Fi network is a 5 GHz network.
322
319
/// </summary>
323
320
/// <param name="kilohertz">Frequency in kilohertz like 2422000 or 5240000.</param>
324
- /// <returns>True if WiFi network is 5 GHz.</returns>
321
+ /// <returns>True if Wi-Fi network is 5 GHz.</returns>
325
322
public static bool Is5GHzNetwork ( int kilohertz )
326
323
{
327
324
var x = ConvertChannelFrequencyToGigahertz ( kilohertz ) ;
328
325
329
326
return x is >= 5.180 and <= 5.825 ;
330
327
}
328
+
329
+ public static bool Is6GHzNetwork ( int kilohertz )
330
+ {
331
+ var x = ConvertChannelFrequencyToGigahertz ( kilohertz ) ;
332
+
333
+ return x is >= 5.925 and <= 7.125 ;
334
+ }
331
335
332
336
/// <summary>
333
- /// Get the human readable network authentication type.
337
+ /// Get the human- readable network authentication type.
334
338
/// </summary>
335
- /// <param name="networkAuthenticationType">WiFi network authentication type as <see cref="NetworkAuthenticationType" />.</param>
336
- /// <returns>Human readable authentication type as string like "Open" or "WPA2 Enterprise".</returns>
339
+ /// <param name="networkAuthenticationType">Wi-Fi network authentication type as <see cref="NetworkAuthenticationType" />.</param>
340
+ /// <returns>Human- readable authentication type as string like "Open" or "WPA2 Enterprise".</returns>
337
341
public static string GetHumanReadableNetworkAuthenticationType ( NetworkAuthenticationType networkAuthenticationType )
338
342
{
339
343
return networkAuthenticationType switch
340
344
{
345
+ NetworkAuthenticationType . None => "-/-" ,
346
+ NetworkAuthenticationType . Unknown => "Unknown" ,
341
347
NetworkAuthenticationType . Open80211 => "Open" ,
342
- NetworkAuthenticationType . Rsna => "WPA2 Enterprise" ,
343
- NetworkAuthenticationType . RsnaPsk => "WPA2 PSK" ,
348
+ NetworkAuthenticationType . SharedKey80211 => "WEP" ,
344
349
NetworkAuthenticationType . Wpa => "WPA Enterprise" ,
345
- NetworkAuthenticationType . WpaNone => "WPA None" ,
346
350
NetworkAuthenticationType . WpaPsk => "WPA PSK" ,
347
- NetworkAuthenticationType . SharedKey80211 => "WEP" ,
351
+ NetworkAuthenticationType . WpaNone => "WPA None" ,
352
+ NetworkAuthenticationType . Rsna => "WPA2 Enterprise" ,
353
+ NetworkAuthenticationType . RsnaPsk => "WPA2 Personal (PSK)" ,
348
354
NetworkAuthenticationType . Ihv => "IHV" ,
349
- NetworkAuthenticationType . Unknown => "Unknown" ,
350
- NetworkAuthenticationType . None => "-/-" ,
355
+ NetworkAuthenticationType . Wpa3Enterprise192Bits => "WPA3 Enterprise (192-bit)" , // Same as Wpa3
356
+ NetworkAuthenticationType . Wpa3Sae => "WPA3 Personal (SAE)" ,
357
+ NetworkAuthenticationType . Owe => "OWE" ,
358
+ NetworkAuthenticationType . Wpa3Enterprise => "WPA3 Enterprise" ,
351
359
_ => "-/-"
352
360
} ;
353
361
}
354
362
355
363
/// <summary>
356
- /// Get the human readable network phy kind.
364
+ /// Get the human- readable network phy kind.
357
365
/// </summary>
358
- /// <param name="phyKind">WiFi network phy kind as <see cref="WiFiPhyKind" />.</param>
359
- /// <returns>Human readable phy kind as string like "802.11g" or "802.11ax".</returns>
366
+ /// <param name="phyKind">Wi-Fi network phy kind as <see cref="WiFiPhyKind" />.</param>
367
+ /// <returns>Human- readable phy kind as string like "802.11g" or "802.11ax".</returns>
360
368
public static string GetHumanReadablePhyKind ( WiFiPhyKind phyKind )
361
369
{
362
370
return phyKind switch
363
371
{
372
+ WiFiPhyKind . Unknown => "Unknown" ,
364
373
WiFiPhyKind . Dsss or WiFiPhyKind . Fhss => "802.11" ,
365
374
WiFiPhyKind . Ofdm => "802.11a" ,
366
375
WiFiPhyKind . Hrdsss => "802.11b" ,
367
376
WiFiPhyKind . Erp => "802.11g" ,
368
377
WiFiPhyKind . HT => "802.11n" ,
369
- WiFiPhyKind . Dmg => "802.11ad" ,
370
378
WiFiPhyKind . Vht => "802.11ac" ,
379
+ WiFiPhyKind . Dmg => "802.11ad" ,
371
380
WiFiPhyKind . HE => "802.11ax" ,
381
+ WiFiPhyKind . Eht => "802.11be" ,
372
382
_ => "-/-"
373
383
} ;
374
384
}
0 commit comments