2013-01-16 47 views
2

我有以下代碼聆聽到WiFi使用managedWifi API(http://managedwifi.codeplex.com/)Managedwifi:有時,WlanConnectionNotification不被解僱

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()。我已經在win7以及win8平板電腦上測試了我的桌面應用程序,通過多次連接/斷開wifi連接並嘗試觀察通知。這是兩個平臺上的問題:

  1. 大部分的時間,我的wlanConnectionChangeHandler被調用的WiFi連接/斷開,一切工作正常。但是,在某些情況下,它根本不會被調用。什麼會導致這一點?我注意到,在最初錯過通知後,即使我繼續連接/斷開無線連接,我也無法收到任何進一步的通知。

  2. 在不同的場合,即使我刪除了事件處理程序,仍然收到通知。我在刪除這些事件處理程序時丟失了些什麼?

謝謝。

回答

0

很明顯,當我編寫代碼時,我並沒有清楚地思考。問題是我的WlanClient客戶端的本地範圍。通過使它成爲一個全局變量來修復它,並只實例化一次。/facepalm