亲宝软件园·资讯

展开

c# wifi信号查看 c#使用ManagedWifi查看当前Wifi信号并选择wifi的示例

人气:1
想了解c#使用ManagedWifi查看当前Wifi信号并选择wifi的示例的相关内容吗,在本文为您仔细讲解c# wifi信号查看的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:wifi信号查看,下面大家一起来学习吧。

使用ManagedWifi查看当前Wifi信号并选择wifi

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NativeWifi;

namespace WifiExample
{
    class WifiManager
    {
        //CMCC的WIFISSID
        public WIFISSID cmccWifiSSID;

        public WifiManager()
        {
            ScanSSID();
        }

        /// <summary>
        /// 将SSID转化成字符串
        /// </summary>
        static string GetStringForSSID(Wlan.Dot11Ssid ssid)
        {
            return Encoding.UTF8.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
        }

        /// <summary> 
        /// 枚举所有无线设备接收到的SSID 
        /// </summary> 
        public void ScanSSID()
        {
            WlanClient client = new WlanClient();
            foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
            {
                // Lists all networks with WEP security 
                Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                foreach (Wlan.WlanAvailableNetwork network in networks)
                {
                    WIFISSID targetSSID = new WIFISSID();
                    targetSSID.wlanInterface = wlanIface;
                    targetSSID.wlanSignalQuality = (int)network.wlanSignalQuality;
                    targetSSID.SSID = GetStringForSSID(network.dot11Ssid);
                    targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm.ToString();
                    targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString();
                    Console.WriteLine(targetSSID.SSID);
                    if (targetSSID.SSID.ToLower().Equals("cmcc"))
                    {
                        cmccWifiSSID = targetSSID;
                        return;
                    }
                }
            }
        } // EnumSSID 

        /// <summary> 
        /// 连接到CMCC
        /// </summary> 
        /// <param name="ssid"></param> 
        public void ConnectToCMCC()
        {
            // Connects to a known network with WEP security 
            string profileName = cmccWifiSSID.SSID; // this is also the SSID
            Console.WriteLine("profileName" + profileName);
            cmccWifiSSID.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
        }

        /// <summary> 
        /// 字符串转Hex 
        /// </summary> 
        /// <param name="str"></param> 
        /// <returns></returns> 
        public static string StringToHex(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.Default.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str) 
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(Convert.ToString(byStr[i], 16));
            }

            return (sb.ToString().ToUpper());
        }

    } // Class WifiManager


    class WIFISSID
    {
        public string SSID = "NONE";
        public string dot11DefaultAuthAlgorithm = "";
        public string dot11DefaultCipherAlgorithm = "";
        public bool networkConnectable = true;
        public string wlanNotConnectableReason = "";
        public int wlanSignalQuality = 0;
        public WlanClient.WlanInterface wlanInterface = null;
    }

}
```


调用链接CMCC
===
```
WifiManager wm = new WifiManager();
wm.ConnectToCMCC();

加载全部内容

相关教程
猜你喜欢
用户评论