Search code examples
c#windowswificodeplex

Managedwifi: Occasionally, WlanConnectionNotification doesn't get fired


I have the following code to listen to the wifi connect/disconnect events using the managedWifi api (http://managedwifi.codeplex.com/)

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

At the start, i call registerWlanListener, and before stopping my app, i call unregisterWlanListener(). I've tested my desktop app on win7 as well as win8 tablet by connecting/disconnecting the wifi connection many times and try to observe the notifications. These are the issues on both platforms:

  1. Most of the time, my wlanConnectionChangeHandler gets called on the wifi connect/disconnect and everything works fine. However, on some occasions, it doesn't get called at all. What can lead to this? I notice that after the initial missed notification, i can't receive any further notification at all even if i continue to connect/disconnect the wifi connection.

  2. On separate occasions, even though I've removed the event handler, i still receive notifications. Am I missing something in removing these event handler?

Thank you.


Solution

  • Obviously i wasn't thinking clearly when i was coding this. The issue is the local scope of my WlanClient client. Fixed it by making it a global var and only instantiate it once. /facepalm