2013-01-16 4 views
2

i를 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()를 호출합니다. 필자는 wifi 연결을 여러 번 연결/연결 해제하고 win8 태블릿뿐만 아니라 win7에서도 내 데스크톱 앱을 테스트하고 알림을 보려고합니다. 이 두 플랫폼 모두에 문제가 있습니다 : 대부분의 시간

  1. 내 wlanConnectionChangeHandler은/연결하거나 연결을 끊고 무선 랜에 호출되는 모든 것이 잘 작동합니다. 그러나 어떤 경우에는 전혀 호출되지 않습니다. 무엇이 이것을 이끌 수 있습니까? 초기 연결이 끊어진 후 Wi-Fi 연결을 계속 끊어도 계속 알림을받을 수 없습니다.

  2. 별도로 이벤트 처리기를 제거했지만 여전히 알림을받습니다. 이러한 이벤트 처리기를 제거 할 때 뭔가가 누락 되었습니까?

감사합니다.

답변

0

분명히 내가 이것을 코딩 할 때 명확하게 생각하지 못했습니다. 문제는 내 WlanClient 클라이언트의 로컬 범위입니다. 전역 var로 만들고 한 번만 인스턴스화하여 문제를 해결했습니다./facepalm