-
Notifications
You must be signed in to change notification settings - Fork 83
/
Wifi.cs
152 lines (127 loc) · 3.95 KB
/
Wifi.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
using SimpleWifi.Win32;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using SimpleWifi.Win32.Interop;
using NotifCodeACM = SimpleWifi.Win32.Interop.WlanNotificationCodeAcm;
using NotifCodeMSM = SimpleWifi.Win32.Interop.WlanNotificationCodeMsm;
namespace SimpleWifi
{
public class Wifi
{
public event EventHandler<WifiStatusEventArgs> ConnectionStatusChanged;
private WlanClient _client;
private WifiStatus _connectionStatus;
private bool _isConnectionStatusSet = false;
public bool NoWifiAvailable = false;
public Wifi()
{
_client = new WlanClient();
NoWifiAvailable = _client.NoWifiAvailable;
if (_client.NoWifiAvailable)
return;
foreach (var inte in _client.Interfaces)
inte.WlanNotification += inte_WlanNotification;
}
/// <summary>
/// Returns a list over all available access points
/// </summary>
public List<AccessPoint> GetAccessPoints()
{
List<AccessPoint> accessPoints = new List<AccessPoint>();
if (_client.NoWifiAvailable)
return accessPoints;
foreach (WlanInterface wlanIface in _client.Interfaces)
{
WlanAvailableNetwork[] rawNetworks = wlanIface.GetAvailableNetworkList(0);
List<WlanAvailableNetwork> networks = new List<WlanAvailableNetwork>();
// Remove network entries without profile name if one exist with a profile name.
foreach (WlanAvailableNetwork network in rawNetworks)
{
bool hasProfileName = !string.IsNullOrEmpty(network.profileName);
bool anotherInstanceWithProfileExists = rawNetworks.Where(n => n.Equals(network) && !string.IsNullOrEmpty(n.profileName)).Any();
if (!anotherInstanceWithProfileExists || hasProfileName)
networks.Add(network);
}
foreach (WlanAvailableNetwork network in networks)
{
accessPoints.Add(new AccessPoint(wlanIface, network));
}
}
return accessPoints;
}
/// <summary>
/// Disconnect all wifi interfaces
/// </summary>
public void Disconnect()
{
if (_client.NoWifiAvailable)
return;
foreach (WlanInterface wlanIface in _client.Interfaces)
{
wlanIface.Disconnect();
}
}
public WifiStatus ConnectionStatus
{
get
{
if (!_isConnectionStatusSet)
ConnectionStatus = GetForcedConnectionStatus();
return _connectionStatus;
}
private set
{
_isConnectionStatusSet = true;
_connectionStatus = value;
}
}
private void inte_WlanNotification(WlanNotificationData notifyData)
{
if (notifyData.notificationSource == WlanNotificationSource.ACM && (NotifCodeACM)notifyData.NotificationCode == NotifCodeACM.Disconnected)
OnConnectionStatusChanged(WifiStatus.Disconnected);
else if (notifyData.notificationSource == WlanNotificationSource.MSM && (NotifCodeMSM)notifyData.NotificationCode == NotifCodeMSM.Connected)
OnConnectionStatusChanged(WifiStatus.Connected);
}
private void OnConnectionStatusChanged(WifiStatus newStatus)
{
ConnectionStatus = newStatus;
if (ConnectionStatusChanged != null)
ConnectionStatusChanged(this, new WifiStatusEventArgs(newStatus));
}
// I don't like this method, it's slow, ugly and should be refactored ASAP.
private WifiStatus GetForcedConnectionStatus()
{
if (NoWifiAvailable)
return WifiStatus.Disconnected;
bool connected = false;
foreach (var i in _client.Interfaces)
{
try
{
var a = i.CurrentConnection; // Current connection throws an exception if disconnected.
connected = true;
}
catch { }
}
if (connected)
return WifiStatus.Connected;
else
return WifiStatus.Disconnected;
}
}
public class WifiStatusEventArgs : EventArgs
{
public WifiStatus NewStatus { get; private set; }
internal WifiStatusEventArgs(WifiStatus status) : base()
{
this.NewStatus = status;
}
}
public enum WifiStatus
{
Disconnected,
Connected
}
}