Beispiel #1
0
        private void ScanThreadProc()
        {
            lock (m_syncRoot)
            {
                Debug.WriteLine("+WiFiService.ScanThreadProc");
                Scanning = true;

                do
                {
                    if (m_wzc == null)
                    {
                        break;
                    }
                    if (!WiFiEnabled)
                    {
                        break;
                    }

                    try
                    {
                        var et = Environment.TickCount;

                        if (m_currentAPs == null)
                        {
                            m_currentAPs = m_wzc.NearbyAccessPoints;
                        }
                        else
                        {
                            m_currentAPs.Refresh();
                        }

                        if (!WiFiEnabled)
                        {
                            break;
                        }

                        var added   = m_currentAPs.Except(m_knownAPs);
                        var lost    = m_knownAPs.Except(m_currentAPs);
                        var updated = m_knownAPs.Intersect(m_currentAPs);

                        m_knownAPs = m_currentAPs.ToList();
                        RaisePropertyChanged("KnownAPs");

                        var handler = ScanComplete;

                        et = Environment.TickCount - et;
                        Debug.WriteLine(string.Format("Network scan took {0}ms", et));

                        if (!WiFiEnabled)
                        {
                            break;
                        }

                        if (handler != null)
                        {
                            ThreadPool.QueueUserWorkItem(delegate
                            {
                                handler(added.ToArray(), lost.ToArray(), updated.ToArray());
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(string.Format("Exception in WiFi network scan.  If you disabled the adapter, you can ignore this error: ", ex.Message));

                        // if we disable the WiFi adapter while this loop is executing, bad things can happen
                        // it's better to just catch around the whole thing and ignore
                        if (!WiFiEnabled)
                        {
                            break;
                        }
                    }
                } while (!m_stopScanEvent.WaitOne(ScanPeriod, false));

                Debug.WriteLine("-WiFiService.ScanThreadProc");
                Scanning     = false;
                m_currentAPs = null;
            }
        }
        internal unsafe void RefreshListPreferred(bool nearbyOnly)
        {
            // If the caller wants only the local preferred APs,
            // we check nearby list and, if the AP is not there,
            // we don't add it to our own preferred list.
            AccessPointCollection apc = null;

            if (nearbyOnly)
            {
                apc = adapter.NearbyAccessPoints;
            }

            // First step is to get the INTF_ENTRY for the adapter.
            // This includes the list of preferred SSID values.
            INTF_ENTRY ie = INTF_ENTRY.GetEntry(this.adapter.Name);

            // The field rdStSSIDList is the preferred list.  It comes
            // in the form of a WZC_802_11_CONFIG_LIST.
            RAW_DATA             rd = ie.rdStSSIDList;
            WZC_WLAN_CONFIG_LIST cl = new WZC_WLAN_CONFIG_LIST(rd);

            // Step through the list and add a new AP to the
            // collection for each entry.
            for (int i = 0; i < cl.NumberOfItems; i++)
            {
                WZC_WLAN_CONFIG c = cl.Item(0);

                // Get a NDIS_WLAN_BSSID corresponding to the
                // part of the WZC_WLAN_CONFIG entry and use that
                // to build an AccessPoint instance for this
                // entry.
                NDIS_WLAN_BSSID bssid = c.ToBssid();

                // If we're only showing those which we can hear,
                // see if the current SSID is in the nearby list.
                if (nearbyOnly)
                {
                    // Find the currently active AP with the SSID
                    // to match the one we're working on.
                    AccessPoint activeAP = apc.FindBySSID(bssid.SSID);
                    int         ss;

                    // If the given SSID is not in range, don't add
                    // an entry to the list.
                    if (activeAP != null)
                    {
                        // Update signal strength.
                        ss = activeAP.SignalStrengthInDecibels;

                        // Copy the signal strength value to the
                        // NDIS_WLAN_BSSID structure for the
                        // preferred list entry.
                        bssid.Rssi = ss;

                        // Create the AP instance and add it to the
                        // preferred list.
                        AccessPoint ap = new AccessPoint(bssid);
                        this.List.Add(ap);
                    }
                }
                else
                {
                    // Create the AP instance and add it to the
                    // preferred list.  The signal strength will
                    // not necessarily be valid.
                    AccessPoint ap = new AccessPoint(bssid);
                    this.List.Add(ap);
                }
            }
        }