Exemplo n.º 1
0
        /// <summary>
        /// Grabs the access points seen ONLY by this interface. This is essentially just <see cref="Wifi.Scan"/>
        /// but without the interface iteration.
        /// </summary>
        /// <param name="wlanIface"></param>
        /// <param name="wifiInstance"></param>
        /// <param name="bRescan"></param>
        /// <returns></returns>
        public static List <AccessPoint> GetAccessPoints(this WlanInterface wlanIface, Wifi wifiInstance,
                                                         bool bRescan = true)
        {
            List <AccessPoint> accessPoints = new List <AccessPoint>();

            if (wifiInstance.NoWifiAvailable)
            {
                return(accessPoints);
            }

            if (bRescan && (DateTime.Now - wifiInstance.GetFieldValue <DateTime>("_lastScanned") >
                            TimeSpan.FromSeconds(60)))
            {
                wifiInstance.Scan();
            }

            WlanAvailableNetwork[]      rawNetworks = wlanIface.GetAvailableNetworkList(0);
            List <WlanAvailableNetwork> networks    = new List <WlanAvailableNetwork>();

            // Remove network entries without profile name if one exist with a profile name.
            foreach (WlanAvailableNetwork network in rawNetworks)
            {
                bool hasProfileName = !string.IsNullOrEmpty(network.profileName);
                bool anotherInstanceWithProfileExists =
                    rawNetworks.Where(n => n.Equals(network) && !string.IsNullOrEmpty(n.profileName)).Any();

                if (!anotherInstanceWithProfileExists || hasProfileName)
                {
                    networks.Add(network);
                }
            }

            foreach (WlanAvailableNetwork network in networks)
            {
                //see https://stackoverflow.com/questions/708952/how-to-instantiate-an-object-with-a-private-constructor-in-c/39076814#comment65026579_708976
                // AccessPoint ap = Activator.CreateInstance(typeof(AccessPoint), BindingFlags.Instance | BindingFlags.NonPublic,null,new object[]{wlanIface,network},null) as AccessPoint;
                AccessPoint ap =
                    CreatePrivateClassInstance(typeof(AccessPoint), new object[] { wlanIface, network }) as AccessPoint;
                accessPoints.Add(ap);
            }

            return(accessPoints);
        }