Skip to content

Commit 65f0665

Browse files
committed
Get wifi adapters
1 parent a9fb9a0 commit 65f0665

10 files changed

Lines changed: 354 additions & 59 deletions

File tree

‎Source/NETworkManager/MainWindow.xaml.cs‎

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -366,39 +366,8 @@ private void AfterContentRendered()
366366
// Search for updates...
367367
if (SettingsManager.Current.Update_CheckForUpdatesAtStartup)
368368
CheckForUpdates();
369-
370-
Test();
371369
}
372-
373-
private async void Test()
374-
{
375-
var access = await WiFiAdapter.RequestAccessAsync();
376-
377-
var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
378-
379-
if (result.Count >= 1)
380-
{
381-
// take first adapter
382-
WiFiAdapter adapter = await WiFiAdapter.FromIdAsync(result[0].Id);
383-
// scan for networks
384-
await adapter.ScanAsync();
385-
// find network with the correct SSID
386-
foreach (var x in adapter.NetworkReport.AvailableNetworks)
387-
{
388-
389-
Console.WriteLine("=== Adapter ===");
390-
Console.WriteLine(x.Ssid);
391-
Console.WriteLine(x.Bssid);
392-
Console.WriteLine(x.SecuritySettings);
393-
394-
}
395-
396-
// connect
397-
//await nwAdapter.ConnectAsync(nw, WiFiReconnectionKind.Automatic);
398-
}
399-
400-
}
401-
370+
402371
private void LoadApplicationList()
403372
{
404373
isApplicationListLoading = true;

‎Source/NETworkManager/Models/Network/NetworkInterface.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.ComponentModel;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Net;
89
using System.Net.NetworkInformation;
@@ -199,7 +200,7 @@ public static IPAddress DetectGatewayBasedOnLocalIPAddress(IPAddress localIPAddr
199200

200201
return null;
201202
}
202-
203+
203204
public Task ConfigureNetworkInterfaceAsync(NetworkInterfaceConfig config)
204205
{
205206
return Task.Run(() => ConfigureNetworkInterface(config));
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
using Windows.Devices.WiFi;
6+
7+
namespace NETworkManager.Models.Network
8+
{
9+
public class WiFi
10+
{
11+
public static async Task<List<WiFiAdapterInfo>> GetWiFiAdapterAsync()
12+
{
13+
List<WiFiAdapterInfo> wifiAdapters = new List<WiFiAdapterInfo>();
14+
List<NetworkInterfaceInfo> networkInterfaces = await NetworkInterface.GetNetworkInterfacesAsync();
15+
16+
foreach (WiFiAdapter wifiAdapter in await WiFiAdapter.FindAllAdaptersAsync())
17+
{
18+
foreach (NetworkInterfaceInfo networkInterface in networkInterfaces)
19+
{
20+
if (!wifiAdapter.NetworkAdapter.NetworkAdapterId.ToString().Equals(networkInterface.Id.TrimStart('{').TrimEnd('}'), StringComparison.OrdinalIgnoreCase))
21+
continue;
22+
23+
wifiAdapters.Add(new WiFiAdapterInfo
24+
{
25+
NetworkInterfaceInfo = networkInterface,
26+
Id = wifiAdapter.NetworkAdapter.NetworkAdapterId
27+
});
28+
}
29+
}
30+
31+
if (wifiAdapters.Count == 0)
32+
return null;
33+
34+
return wifiAdapters;
35+
}
36+
37+
public static async Task<IEnumerable<WiFiNetworkInfo>> GetWiFiNetworksAsync(Guid adapterId)
38+
{
39+
//https://docs.microsoft.com/en-us/uwp/api/windows.devices.wifi.wifiadapter.requestaccessasync
40+
//var access = await WiFiAdapter.RequestAccessAsync();
41+
42+
43+
44+
return null;
45+
}
46+
47+
private static async void Test()
48+
{
49+
50+
51+
var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
52+
53+
if (result.Count >= 1)
54+
{
55+
// take first adapter
56+
WiFiAdapter adapter = await WiFiAdapter.FromIdAsync(result[0].Id);
57+
// scan for networks
58+
await adapter.ScanAsync();
59+
// find network with the correct SSID
60+
foreach (var x in adapter.NetworkReport.AvailableNetworks)
61+
{
62+
63+
Console.WriteLine("=== Adapter ===");
64+
Console.WriteLine(x.Ssid);
65+
Console.WriteLine(x.Bssid);
66+
Console.WriteLine(x.SecuritySettings);
67+
68+
}
69+
70+
// connect
71+
//await nwAdapter.ConnectAsync(nw, WiFiReconnectionKind.Automatic);
72+
}
73+
}
74+
}
75+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Net.NetworkInformation;
3+
4+
namespace NETworkManager.Models.Network
5+
{
6+
public class WiFiAdapterInfo
7+
{
8+
public Guid Id { get; set; }
9+
public NetworkInterfaceInfo NetworkInterfaceInfo { get; set; }
10+
11+
public WiFiAdapterInfo()
12+
{
13+
14+
}
15+
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Net.NetworkInformation;
2+
3+
namespace NETworkManager.Models.Network
4+
{
5+
public class WiFiNetworkInfo
6+
{
7+
public WiFiNetworkInfo()
8+
{
9+
10+
}
11+
12+
}
13+
}

‎Source/NETworkManager/NETworkManager.csproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@
242242
<Compile Include="Models\Network\BandwidthMeterSpeedArgs.cs" />
243243
<Compile Include="Models\Network\DNSServer.cs" />
244244
<Compile Include="Models\Network\DNSServerInfo.cs" />
245+
<Compile Include="Models\Network\WiFiAdapterInfo.cs" />
246+
<Compile Include="Models\Network\WiFiNetworkInfo.cs" />
247+
<Compile Include="Models\Network\WiFi.cs" />
245248
<Compile Include="Models\Network\IPNetworkInfo.cs" />
246249
<Compile Include="Models\Network\Listener.cs" />
247250
<Compile Include="Models\Network\Connection.cs" />

‎Source/NETworkManager/Resources/Styles/RectangleStyles.xaml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343
<Setter Property="ToolTipService.ShowDuration" Value="600000" />
4444
</Style>
4545

46+
<Style x:Key="ReloadAnimation" TargetType="{x:Type Rectangle}">
47+
<Setter Property="Height" Value="24" />
48+
<Setter Property="RenderTransform">
49+
<Setter.Value>
50+
<RotateTransform CenterX="12" CenterY="12" />
51+
</Setter.Value>
52+
</Setter>
53+
<Style.Triggers>
54+
<Trigger Property="IsEnabled" Value="False">
55+
<Trigger.EnterActions>
56+
<BeginStoryboard>
57+
<Storyboard>
58+
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle" From="0" To="720" Duration="0:0:2" />
59+
</Storyboard>
60+
</BeginStoryboard>
61+
</Trigger.EnterActions>
62+
</Trigger>
63+
</Style.Triggers>
64+
</Style>
65+
4666
<!-- @FutureMe Don't touch this! Converter "RectangleConnectionStateStyleConverter" returns to the x:Key -->
4767
<Style x:Key="CheckRectangle" TargetType="{x:Type Rectangle}">
4868
<Style.Resources>

‎Source/NETworkManager/ViewModels/WiFiViewModel.cs‎

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,80 @@
1-
using System.ComponentModel;
1+
using NETworkManager.Models.Network;
2+
using NETworkManager.Models.Settings;
3+
using NETworkManager.Utilities;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.ComponentModel;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using System.Windows.Input;
210

311
namespace NETworkManager.ViewModels
412
{
513
public class WiFiViewModel : ViewModelBase
614
{
715
#region Variables
16+
private readonly bool _isLoading;
817

18+
private bool _isWiFiAdaptersLoading;
19+
public bool IsWiFiAdaptersLoading
20+
{
21+
get => _isWiFiAdaptersLoading;
22+
set
23+
{
24+
if (value == _isWiFiAdaptersLoading)
25+
return;
26+
27+
_isWiFiAdaptersLoading = value;
28+
OnPropertyChanged();
29+
}
30+
}
31+
32+
private List<WiFiAdapterInfo> _wiFiAdapters;
33+
public List<WiFiAdapterInfo> WiFiAdapters
34+
{
35+
get => _wiFiAdapters;
36+
set
37+
{
38+
if (value == _wiFiAdapters)
39+
return;
40+
41+
_wiFiAdapters = value;
42+
OnPropertyChanged();
43+
}
44+
}
45+
46+
private WiFiAdapterInfo _selectedWiFiAdapters;
47+
public WiFiAdapterInfo SelectedWiFiAdapter
48+
{
49+
get => _selectedWiFiAdapters;
50+
set
51+
{
52+
if (value == _selectedWiFiAdapters)
53+
return;
54+
55+
if (value != null)
56+
{
57+
//if (!_isLoading)
58+
// SettingsManager.Current.NetworkInterface_SelectedInterfaceId = value.Id;
59+
}
60+
61+
_selectedWiFiAdapters = value;
62+
OnPropertyChanged();
63+
}
64+
}
965
#endregion
1066

1167
#region Constructor, load settings
1268

1369
public WiFiViewModel()
1470
{
71+
_isLoading = true;
72+
1573
LoadSettings();
74+
75+
LoadAdapters();
76+
77+
_isLoading = false;
1678
}
1779

1880
private void LoadSettings()
@@ -22,11 +84,53 @@ private void LoadSettings()
2284
#endregion
2385

2486
#region ICommands & Actions
87+
public ICommand ReloadWiFiAdaptersCommand => new RelayCommand(p => ReloadAdapterAction(), ReloadWiFiAdapters_CanExecute);
2588

89+
private bool ReloadWiFiAdapters_CanExecute(object obj) => !IsWiFiAdaptersLoading;
90+
91+
private void ReloadAdapterAction()
92+
{
93+
ReloadAdapter();
94+
}
2695
#endregion
2796

2897
#region Methods
98+
private async void LoadAdapters()
99+
{
100+
IsWiFiAdaptersLoading = true;
101+
102+
WiFiAdapters = await WiFi.GetWiFiAdapterAsync();
103+
104+
// Get the last selected interface, if it is still available on this machine...
105+
if (WiFiAdapters.Count > 0)
106+
{
107+
var info = WiFiAdapters.FirstOrDefault(s => s.Id.ToString() == SettingsManager.Current.NetworkInterface_SelectedInterfaceId);
108+
109+
SelectedWiFiAdapter = info ?? WiFiAdapters[0];
110+
}
111+
112+
IsWiFiAdaptersLoading = false;
113+
}
114+
115+
private async void ReloadAdapter()
116+
{
117+
IsWiFiAdaptersLoading = true;
118+
119+
await Task.Delay(2000); // Make the user happy, let him see a reload animation (and he cannot spam the reload command)
120+
121+
Guid id = Guid.Empty;
122+
123+
if (SelectedWiFiAdapter != null)
124+
id = SelectedWiFiAdapter.Id;
29125

126+
WiFiAdapters = await WiFi.GetWiFiAdapterAsync();
127+
128+
// Change interface...
129+
SelectedWiFiAdapter = id == Guid.Empty ? WiFiAdapters.FirstOrDefault() : WiFiAdapters.FirstOrDefault(x => x.Id == id);
130+
131+
IsWiFiAdaptersLoading = false;
132+
}
133+
30134
public void OnViewVisible()
31135
{
32136

@@ -41,7 +145,7 @@ public void OnViewHide()
41145
#region Events
42146
private void SettingsManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
43147
{
44-
148+
45149
}
46150
#endregion
47151
}

‎Source/NETworkManager/Views/NetworkInterfaceView.xaml‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,7 @@
3030
<converters:NullOrEmptyToBoolConverter x:Key="NullOrEmptyToBoolConverter" />
3131
<converters:OperationalStatusToStringConverter x:Key="OperationalStatusToStringConverter" />
3232
<converters:PhysicalAddressToStringConverter x:Key="PhysicalAddressToStringConverter" />
33-
<converters:ValidateNetworkInterfaceConfigConverter x:Key="ValidateNetworkInterfaceConfigConverter" />
34-
<Style x:Key="ReloadAnimation" TargetType="Rectangle">
35-
<Setter Property="Height" Value="24" />
36-
<Setter Property="RenderTransform">
37-
<Setter.Value>
38-
<RotateTransform CenterX="12" CenterY="12" />
39-
</Setter.Value>
40-
</Setter>
41-
<Style.Triggers>
42-
<Trigger Property="IsEnabled" Value="False">
43-
<Trigger.EnterActions>
44-
<BeginStoryboard>
45-
<Storyboard>
46-
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle" From="0" To="720" Duration="0:0:2" />
47-
</Storyboard>
48-
</BeginStoryboard>
49-
</Trigger.EnterActions>
50-
</Trigger>
51-
</Style.Triggers>
52-
</Style>
33+
<converters:ValidateNetworkInterfaceConfigConverter x:Key="ValidateNetworkInterfaceConfigConverter" />
5334
</UserControl.Resources>
5435
<TabControl Style="{StaticResource DefaultTabControl}">
5536
<TabItem Header="{x:Static localization:Strings.Information}">

0 commit comments

Comments
 (0)