2013-01-16 3 views
2

je le code ci-dessous pour écouter la connexion Wi-Fi se connecter/déconnecter des événements à l'aide du managedWifi api (http://managedwifi.codeplex.com/)Managedwifi: De temps en temps, WlanConnectionNotification ne soit pas tiré

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"); 
     } 
    } 

Au début, j'appelle registerWlanListener, et avant d'arrêter mon application, j'appelle unregisterWlanListener(). J'ai testé mon application de bureau sur win7 ainsi que la tablette win8 en connectant/déconnectant la connexion wifi plusieurs fois et essayer d'observer les notifications. Ce sont les questions sur les deux plates-formes:

  1. La plupart du temps, mon wlanConnectionChangeHandler est appelé sur le wifi de connexion/déconnexion et tout fonctionne très bien. Cependant, à certaines occasions, on ne l'appelle pas du tout. Qu'est-ce qui peut mener à cela? Je remarque qu'après la notification initiale manquée, je ne peux recevoir aucune autre notification même si je continue à connecter/déconnecter la connexion wifi.

  2. À différentes occasions, même si j'ai supprimé le gestionnaire d'événements, je reçois toujours des notifications. Ai-je manqué quelque chose en supprimant ces gestionnaires d'événements?

Merci.

Répondre

0

Évidemment, je ne pensais pas clairement quand je codais cela. Le problème concerne la portée locale de mon client WlanClient. Le corrige en en faisant une var globale et ne l'instancie qu'une seule fois./visagepalm

Questions connexes