WlanConnectionNotification doesn't get fired occasionally

Jan 15, 2013 at 10:47 PM

I'm really new to C# and i have found this API to be really helpful. I have the following code to listen to the wifi connect/disconnect events.

 

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, and have the following issues on both:

1. Most of the time, my wlanConnectionChangeHandler gets called on the wifi connect/disconnect and everything works fine. However, on some occasion, it doesn't get called at all. What can lead to this?

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

Thank you.

Jan 15, 2013 at 10:58 PM

just a quick note that i tested this by connecting/disconnecting the wifi connection many times and try to observe the notification. 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. It's confusing because this code does work most of the time.

Jan 16, 2013 at 3:59 PM

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