forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWireless80211.cs
73 lines (66 loc) · 2.5 KB
/
Wireless80211.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
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using nanoFramework.Networking;
namespace WiFiAP
{
class Wireless80211
{
public static bool IsEnabled()
{
Wireless80211Configuration wconf = GetConfiguration();
return !string.IsNullOrEmpty(wconf.Ssid);
}
/// <summary>
/// Disable the Wireless station interface.
/// </summary>
public static void Disable()
{
Wireless80211Configuration wconf = GetConfiguration();
wconf.Options = Wireless80211Configuration.ConfigurationOptions.None;
wconf.SaveConfiguration();
}
/// <summary>
/// Configure and enable the Wireless station interface
/// </summary>
/// <param name="ssid"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool Configure(string ssid, string password)
{
// And we have to force connect once here even for a short time
var success = WiFiNetworkHelper.ConnectDhcp(ssid, password, token: new CancellationTokenSource(10000).Token);
Debug.WriteLine($"Connection is {success}");
Wireless80211Configuration wconf = GetConfiguration();
wconf.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable;
wconf.SaveConfiguration();
return true;
}
/// <summary>
/// Get the Wireless station configuration.
/// </summary>
/// <returns>Wireless80211Configuration object</returns>
public static Wireless80211Configuration GetConfiguration()
{
NetworkInterface ni = GetInterface();
return Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
}
public static NetworkInterface GetInterface()
{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
// Find WirelessAP interface
foreach (NetworkInterface ni in Interfaces)
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
return ni;
}
}
return null;
}
}
}