作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下代码使用 managedWifi api (http://managedwifi.codeplex.com/) 监听 wifi 连接/断开事件
public void wlanConnectionChangeHandler(Wlan.WlanNotificationData notifyData, Wlan.WlanConnectionNotificationData connNotifyData){
string msg = String.Empty;
switch (notifyData.notificationSource)
{
case Wlan.WlanNotificationSource.ACM:
switch ((Wlan.WlanNotificationCodeAcm)notifyData.notificationCode)
{
case Wlan.WlanNotificationCodeAcm.ConnectionStart:
msg = "ConnectionStart";
break;
case Wlan.WlanNotificationCodeAcm.ConnectionComplete:
msg = "ConnectionComplete";
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanAssociationAttributes conAttributes = wlanIface.CurrentConnection.wlanAssociationAttributes;
Wlan.Dot11Ssid ssid = conAttributes.dot11Ssid;
PhysicalAddress bssid = conAttributes.Dot11Bssid;
int rssi = wlanIface.RSSI;
msg += ". ssid: " + GetStringForSSID(ssid) + ". rssi: " + rssi.ToString() + ". MAC: " + bssid.ToString();
break;
}
break;
case Wlan.WlanNotificationCodeAcm.Disconnecting:
msg = "Disconnecting";
break;
case Wlan.WlanNotificationCodeAcm.Disconnected:
msg = "Disconnected";
break;
default:
msg = "unknown notificationCode =" + notifyData.notificationCode;
break;
}
MessageBox.Show(msg + " for profile:" + connNotifyData.profileName);
break;
default:
//MessageBox.Show("irrelevant notification. Ignore");
break;
}
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.ASCII.GetString( ssid.SSID, 0, (int) ssid.SSIDLength );
}
private void registerWlanListener()
{
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
string str = "Name=" + wlanIface.InterfaceName + ". State: ";
switch (wlanIface.InterfaceState)
{
case Wlan.WlanInterfaceState.NotReady:
str += "NotReady";
break;
case Wlan.WlanInterfaceState.Disconnected:
str += "Disconnected";
break;
case Wlan.WlanInterfaceState.Disconnecting:
str += "Disconnecting";
break;
case Wlan.WlanInterfaceState.Connected:
str += "Connected";
break;
}
wlanIface.WlanConnectionNotification += wlanConnectionChangeHandler;
MessageBox.Show(str + ". Listener registered");
}
}
private void unregisterWlanListener()
{
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
wlanIface.WlanConnectionNotification -= wlanConnectionChangeHandler;
MessageBox.Show(wlanIface.InterfaceName + ". Listener unregistered");
}
}
开始时,我调用了 registerWlanListener,在停止我的应用程序之前,我调用了 unregisterWlanListener()。我通过多次连接/断开 wifi 连接并尝试观察通知,在 win7 和 win8 平板电脑上测试了我的桌面应用程序。这些是两个平台上的问题:
大多数时候,我的 wlanConnectionChangeHandler 在 wifi 连接/断开时被调用,一切正常。但是,在某些情况下,它根本不会被调用。什么会导致这种情况?我注意到在最初错过通知后,即使我继续连接/断开 wifi 连接,我也根本无法收到任何进一步的通知。
在不同的情况下,即使我删除了事件处理程序,我仍然会收到通知。我在删除这些事件处理程序时遗漏了什么吗?
谢谢。
最佳答案
显然,我在编写代码时并没有想清楚。问题是我的 WlanClient 客户端的本地范围。通过将其设为全局变量并仅实例化一次来修复它。/捂脸
关于c# - Managedwifi : Occasionally, WlanConnectionNotification 没有被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14350121/
我有以下代码使用 managedWifi api (http://managedwifi.codeplex.com/) 监听 wifi 连接/断开事件 public void wlanConnecti
我是一名优秀的程序员,十分优秀!