简体   繁体   English

如何使用C#中的NativeWifi API发现您的WLAN连接到的BSSID

[英]How to discover the BSSID that your WLAN is connected to using NativeWifi API in C#

A WLAN can have more than one BSSID. WLAN可以具有多个BSSID。 I need to discovery which BSSID I am connected with. 我需要发现我连接了哪个BSSID。

In the NativeWIFI API using the struct WlanAvailableNetwork i can discover the WLAN that i am connected, but it dnt tell me which is the connected BSSID. 在使用结构WlanAvailableNetwork的NativeWIFI API中,我可以发现我已连接的WLAN,但它不会告诉我哪个是已连接的BSSID。

In the Struct WlanBssEntry i can get all the BSSID of any WLAN, but i also can't discover which one i am connected. 在Struct WlanBssEntry中,我可以获取任何WLAN的所有BSSID,但是我也无法发现连接的是哪个。

All that i need is the MAC addrees of the WLAN i am connected with. 我需要的只是与之连接的WLAN的MAC地址。 Thats why i want to discover which BSSID i am connected with. 那就是为什么我想发现与我连接的BSSID。

Txs for the help :D Txs的帮助:D

I already discovered it... I will post here my solution in case anyone needs it in the future... 我已经发现了它...我将在这里发布我的解决方案,以防将来有人需要它...

using NativeWifi;
public  void GetLog(int Count)
        {
            string Conectividade = "Disc";//Initi variable Conectividade as Disconnected
            string RRate = "0";//Initi variable RRate as 0
            string TRate = "0";//Initi variable TRate as 0

            WlanClient client = new WlanClient();
            foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)// Get the WLANs available
            {
                Wlan.WlanAssociationAttributes conAttributes = wlanIface.CurrentConnection.wlanAssociationAttributes;//Get the Attributes of current connection 
                string ConnectedSSID = Encoding.ASCII.GetString(conAttributes.dot11Ssid.SSID).ToString();//Save the SSID of WLAN connected with
                string ReceivedRate = conAttributes.rxRate.ToString();//Save the receipted rate of the connected WLAN
                string TransmitededRate = conAttributes.txRate.ToString();//Save the transmitted rate of the connected WLAN    
                byte[] ConnectedMacAddr = conAttributes.dot11Bssid;//MAC of the BSSID in which the WLAN is connected with
                string ConMac = "";
                for (int i = 0; i < ConnectedMacAddr.Length;                    {
                    ConMac += ConnectedMacAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();//ConMac sera o MAC da BSSID conectada
                }

                Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();//Vector with the BSS available
                Wlan.WlanAvailableNetwork[] wlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0);//Vector with the WLANS available

                WriteLog("\"ID" + listSeparatorQuotes + "DateTime" + listSeparatorQuotes + "SSID" + listSeparatorQuotes + "MAC" + listSeparatorQuotes + "Type" + listSeparatorQuotes + "Auth" + listSeparatorQuotes + "Cipher" + listSeparatorQuotes + "Connection" + listSeparatorQuotes + "RecivRate" + listSeparatorQuotes + "TransmiRate" + listSeparatorQuotes + "SignalQuality" + listSeparatorQuotes + "NumberOfBSSIDS\"", path, "WLANs" + StartDay + StartHour + ".csv");

                foreach (Wlan.WlanAvailableNetwork AVnetwork in wlanAvailableNetwork)
                {
                    string SSIDatual = Encoding.ASCII.GetString(AVnetwork.dot11Ssid.SSID).ToString();//Actual SSID
                    if(SSIDatual.Equals(ConnectedSSID))
                    {
                        Conectividade = "Con";
                        RRate = ReceivedRate;
                        TRate = TransmitededRate;
                    }
                    //___________________________ wlanAvailableNetwork ___________________________
                    WriteLog(Count.ToString() + listSeparator + System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + listSeparator + SSIDatual + listSeparator + ConMac + listSeparator +  AVnetwork.dot11BssType + listSeparator + AVnetwork.dot11DefaultAuthAlgorithm + listSeparator + AVnetwork.dot11DefaultCipherAlgorithm + listSeparator + Conectividade + listSeparator + RRate + listSeparator + TRate + listSeparator + AVnetwork.wlanSignalQuality + listSeparator + AVnetwork.numberOfBssids + "\"", path, "WLANs" + StartDay + StartHour + ".csv");//Its a function that's writes a log in the selected path...case you need this function send me a message!
                    //_________________________ End wlanAvailableNetwork _________________________
                    Conectividade = "Disc";//Reinitialize the value of Conectividade                        RRate = "0";//Reinitialize the value of RRate
                    TRate = "0";//Reinitialize the value of TRate
                }
                foreach (Wlan.WlanBssEntry network in wlanBssEntries)// Get all existent BSSIDs
                {
                    int rss = network.rssi;
                    byte[] macAddr = network.dot11Bssid;
                    string tMac = "";

                    for (int i = 0; i < macAddr.Length; i++)
                    {
                        tMac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();
                    }

                    //___________________________ wlanBSSEntries __________________________
                    WriteLog(Count.ToString() + listSeparator + System.DateTime.Now.ToString("MM/dd/yyyy  HH:mm:ss") + listSeparator + System.Text.ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString() + listSeparator + network.dot11BssType + listSeparator + network.chCenterFrequency + listSeparator + network.linkQuality + listSeparator + rss.ToString() + listSeparator + tMac, path, "BSSIDs" + StartDay + StartHour + ".csv");
                    //_________________________ End wlanBSSEntries _________________________



                }
                Console.ReadLine();
            }
        }

OBS: I'm writing the LOG as a .csv file, and I'm using culture to get the items separator, this way it will work in any place and culture... for example in Brazil and German the item's separator is a ";", but in USA the separator is a ",". OBS:我正在将LOG作为.csv文件编写,并且我使用文化来获取项目分隔符,这样,它就可以在任何地方和任何文化中工作...例如,在巴西和德语中,项目的分隔符是“;”,但在美国,分隔符是“,”。 It will adapt for any situation... 它将适应任何情况...

Here is the code to get the culture and the separation... 这是获得文化和分离的代码...

using System.Globalization;
private static string listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator; 
private static string listSeparatorQuotes = "\"" + listSeparator + "\""; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM