This repository has been archived by the owner on Jan 30, 2019. It is now read-only.
/
NetworkPresenter.cs
277 lines (231 loc) · 9.31 KB
/
NetworkPresenter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources;
using Windows.Devices.WiFi;
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.Security.Credentials;
namespace IoTCoreDefaultApp
{
public class NetworkPresenter
{
private readonly static uint EthernetIanaType = 6;
private readonly static uint WirelessInterfaceIanaType = 71;
public static string GetDirectConnectionName()
{
var icp = NetworkInformation.GetInternetConnectionProfile();
if (icp != null)
{
if(icp.NetworkAdapter.IanaInterfaceType == EthernetIanaType)
{
return icp.ProfileName;
}
}
return null;
}
public static string GetCurrentNetworkName()
{
var icp = NetworkInformation.GetInternetConnectionProfile();
if (icp != null)
{
return icp.ProfileName;
}
var resourceLoader = ResourceLoader.GetForCurrentView();
var msg = resourceLoader.GetString("NoInternetConnection");
return msg;
}
public static string GetCurrentIpv4Address()
{
var icp = NetworkInformation.GetInternetConnectionProfile();
if (icp != null && icp.NetworkAdapter != null && icp.NetworkAdapter.NetworkAdapterId != null)
{
var name = icp.ProfileName;
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
if (hn.IPInformation != null &&
hn.IPInformation.NetworkAdapter != null &&
hn.IPInformation.NetworkAdapter.NetworkAdapterId != null &&
hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId &&
hn.Type == HostNameType.Ipv4)
{
return hn.CanonicalName;
}
}
}
var resourceLoader = ResourceLoader.GetForCurrentView();
var msg = resourceLoader.GetString("NoInternetConnection");
return msg;
}
private Dictionary<WiFiAvailableNetwork, WiFiAdapter> networkNameToInfo;
private static WiFiAccessStatus? accessStatus;
public static async Task<bool> WifiIsAvailable()
{
if ((await TestAccess()) == false)
{
return false;
}
try
{
var adapters = await WiFiAdapter.FindAllAdaptersAsync();
return adapters.Count > 0;
}
catch (Exception)
{
return false;
}
}
private async Task<bool> UpdateInfo()
{
if ((await TestAccess()) == false)
{
return false;
}
networkNameToInfo = new Dictionary<WiFiAvailableNetwork, WiFiAdapter>();
var adapters = WiFiAdapter.FindAllAdaptersAsync();
foreach (var adapter in await adapters)
{
await adapter.ScanAsync();
if (adapter.NetworkReport == null)
{
continue;
}
foreach (var network in adapter.NetworkReport.AvailableNetworks)
{
if (!HasSsid(networkNameToInfo, network.Ssid))
{
networkNameToInfo[network] = adapter;
}
}
}
return true;
}
private bool HasSsid(Dictionary<WiFiAvailableNetwork, WiFiAdapter> resultCollection, string ssid)
{
foreach (var network in resultCollection)
{
if (!string.IsNullOrEmpty(network.Key.Ssid) && network.Key.Ssid == ssid)
{
return true;
}
}
return false;
}
public async Task<IList<WiFiAvailableNetwork>> GetAvailableNetworks()
{
await UpdateInfo();
return networkNameToInfo.Keys.ToList();
}
public WiFiAvailableNetwork GetCurrentWifiNetwork()
{
var connectionProfiles = NetworkInformation.GetConnectionProfiles();
if (connectionProfiles.Count < 1)
{
return null;
}
var validProfiles = connectionProfiles.Where(profile =>
{
return (profile.IsWlanConnectionProfile && profile.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.None);
});
if (validProfiles.Count() < 1)
{
return null;
}
var firstProfile = validProfiles.First() as ConnectionProfile;
return networkNameToInfo.Keys.FirstOrDefault(wifiNetwork => wifiNetwork.Ssid.Equals(firstProfile.ProfileName));
}
public async Task<bool> ConnectToNetwork(WiFiAvailableNetwork network, bool autoConnect)
{
if (network == null)
{
return false;
}
var result = await networkNameToInfo[network].ConnectAsync(network, autoConnect ? WiFiReconnectionKind.Automatic : WiFiReconnectionKind.Manual);
return (result.ConnectionStatus == WiFiConnectionStatus.Success);
}
public void DisconnectNetwork(WiFiAvailableNetwork network)
{
networkNameToInfo[network].Disconnect();
}
public static bool IsNetworkOpen(WiFiAvailableNetwork network)
{
return network.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None;
}
public async Task<bool> ConnectToNetworkWithPassword(WiFiAvailableNetwork network, bool autoConnect, PasswordCredential password)
{
if (network == null)
{
return false;
}
var result = await networkNameToInfo[network].ConnectAsync(
network,
autoConnect ? WiFiReconnectionKind.Automatic : WiFiReconnectionKind.Manual,
password);
return (result.ConnectionStatus == WiFiConnectionStatus.Success);
}
private static async Task<bool> TestAccess()
{
if (!accessStatus.HasValue)
{
accessStatus = await WiFiAdapter.RequestAccessAsync();
}
return (accessStatus == WiFiAccessStatus.Allowed);
}
public class NetworkInfo
{
public string NetworkName { get; set; }
public string NetworkIpv6 { get; set; }
public string NetworkIpv4 { get; set; }
public string NetworkStatus { get; set; }
}
public static async Task<IList<NetworkInfo>> GetNetworkInformation()
{
var networkList = new Dictionary<Guid, NetworkInfo>();
var hostNamesList = NetworkInformation.GetHostNames();
var resourceLoader = ResourceLoader.GetForCurrentView();
foreach (var hostName in hostNamesList)
{
if ((hostName.Type == HostNameType.Ipv4 || hostName.Type == HostNameType.Ipv6) &&
(hostName != null && hostName.IPInformation != null && hostName.IPInformation.NetworkAdapter != null))
{
var profile = await hostName.IPInformation.NetworkAdapter.GetConnectedProfileAsync();
if (profile != null)
{
NetworkInfo info;
var found = networkList.TryGetValue(hostName.IPInformation.NetworkAdapter.NetworkAdapterId, out info);
if (!found)
{
info = new NetworkInfo();
networkList[hostName.IPInformation.NetworkAdapter.NetworkAdapterId] = info;
if (hostName.IPInformation.NetworkAdapter.IanaInterfaceType == WirelessInterfaceIanaType &&
profile.ProfileName.Equals("Ethernet"))
{
info.NetworkName = "Wireless LAN Adapter";
}
else
{
info.NetworkName = profile.ProfileName;
}
var statusTag = profile.GetNetworkConnectivityLevel().ToString();
info.NetworkStatus = resourceLoader.GetString("NetworkConnectivityLevel_" + statusTag);
}
if (hostName.Type == HostNameType.Ipv4)
{
info.NetworkIpv4 = hostName.CanonicalName;
}
else
{
info.NetworkIpv6 = hostName.CanonicalName;
}
}
}
}
var res = new List<NetworkInfo>();
res.AddRange(networkList.Values);
return res;
}
}
}