Win10啓動wifi共享源碼(C#)

下面的代碼是從網上下載的,具體從哪裏下載的已經找不到鏈接了。

源文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.WiFi;
using Windows.Devices.WiFiDirect;
using Windows.Networking.Connectivity;
using Windows.Networking.NetworkOperators;
using Windows.Security.Credentials;

namespace Win10WiFi
{
    /// <summary>
    /// Windows10Wifi創建與連接
    /// </summary>
    public class WlanHostedNetworkHelper
    {
        private static string _ssid = "";
        public static string SSID
        { get { return _ssid; } }

        public static WiFiAdapter Adapter { get; set; }

        #region 創建WIFI
        static WiFiDirectAdvertisementPublisher _publisher;
        static WiFiDirectConnectionListener _listener;
        static NetworkOperatorTetheringManager _networkOperatorTetheringManager;
        /// <summary>
        /// 網絡共享管理對象
        /// </summary>
        public static NetworkOperatorTetheringManager NetworkOperatorTetheringManager
        {
            get
            {
                if (_networkOperatorTetheringManager == null)
                {
                    ConnectionProfile profile = NetworkInformation.GetConnectionProfiles().FirstOrDefault();
                    LogHelper.Default(new { Title = "NetworkOperatorTetheringManager", Data = profile });
                    if (profile != null)
                        _networkOperatorTetheringManager = NetworkOperatorTetheringManager.CreateFromConnectionProfile(profile);
                }
                return _networkOperatorTetheringManager;
            }
        }
        /// <summary>
        /// 監聽WiFi鏈接
        /// </summary>
        private static WiFiDirectAdvertisementPublisher WiFiDirectPublisher
        {
            get
            {
                if (_publisher == null)
                    _publisher = new WiFiDirectAdvertisementPublisher();
                return _publisher;
            }
        }

        private static WiFiDirectConnectionListener WiFiDirectListener
        {
            get
            {
                if (_listener == null)
                    _listener = new WiFiDirectConnectionListener();
                return _listener;
            }
        }

        public static uint GetConnectedClients()
        {
            uint uiRet = 0;

            if (NetworkOperatorTetheringManager != null) { 
                uiRet = NetworkOperatorTetheringManager.ClientCount;
            }
            return uiRet;
        }

        public static bool GetWifiState()
        {
            bool bRet = false;
            if (NetworkOperatorTetheringManager != null)
            {
                if (NetworkOperatorTetheringManager.TetheringOperationalState == TetheringOperationalState.On)
                {
                    bRet = true;
                }
            }

            return bRet;
        }

        /// <summary>
        /// 開起WiFi
        /// </summary>
        /// <param name="ssid"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public static bool StartWiFi(string ssid, string pass)
        {
            try
            {
                LogHelper.Default(new { Title = "StartWiFi", SSID = ssid, Pass = pass });
                try
                {
                    // This can raise an exception if the machine does not support WiFi. Sorry.
                    WiFiDirectListener.ConnectionRequested += OnConnectionRequested;
                }
                catch (Exception ex)
                {
                    LogHelper.Default("StartWiFi Listener", ex);
                    return false;
                }
                StopWiFi();

                WiFiDirectPublisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal;
                WiFiDirectPublisher.Advertisement.IsAutonomousGroupOwnerEnabled = true;
                WiFiDirectPublisher.Advertisement.LegacySettings.IsEnabled = true;

                WiFiDirectPublisher.Advertisement.LegacySettings.Ssid = ssid;
                var creds = new Windows.Security.Credentials.PasswordCredential();
                creds.Password = pass;
                WiFiDirectPublisher.Advertisement.LegacySettings.Passphrase = creds;
                WiFiDirectPublisher.Start();
                _ssid = ssid;
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Default("StartWiFi", ex);
                return false;
            }
        }
        /// <summary>
        /// 開啓共享網絡
        /// </summary>
        /// <param name="ssid"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public async static Task<NetworkOperatorTetheringOperationResult> StartShareWiFiAsync(string ssid, string pass)
        {
            NetworkOperatorTetheringOperationResult result = null;
            try
            {
                LogHelper.Default(new { Title = "StartWiFi", SSID = ssid, Pass = pass });

                if (NetworkOperatorTetheringManager != null)
                {
                    NetworkOperatorTetheringAccessPointConfiguration notapc = new NetworkOperatorTetheringAccessPointConfiguration();
                    notapc.Ssid = ssid;
                    notapc.Passphrase = pass;

                    await NetworkOperatorTetheringManager.ConfigureAccessPointAsync(notapc);
                    result = await NetworkOperatorTetheringManager.StartTetheringAsync();
                    _ssid = ssid;
                }
                else
                    LogHelper.Default(new { Title = "StartShareWiFi", Data = "未鏈接網絡" });
            }
            catch (Exception ex)
            {
                LogHelper.Default("StartShareWiFi", ex);
            }
            return result;
        }

        /// <summary>
        /// 關閉啓動的WiFi
        /// </summary>
        /// <returns></returns>
        public static bool StopWiFi()
        {
            try
            {
                _ssid = "";
                if (WiFiDirectPublisher.Status == WiFiDirectAdvertisementPublisherStatus.Started)
                    WiFiDirectPublisher.Stop();

                if (NetworkOperatorTetheringManager.TetheringOperationalState == TetheringOperationalState.On)
                {
                    NetworkOperatorTetheringManager.StopTetheringAsync();
                    Thread.Sleep(2000);
                }
                while (NetworkOperatorTetheringManager.TetheringOperationalState == TetheringOperationalState.InTransition || NetworkOperatorTetheringManager.TetheringOperationalState == TetheringOperationalState.On)
                {
                    if (NetworkOperatorTetheringManager.TetheringOperationalState == TetheringOperationalState.On)
                    {
                        NetworkOperatorTetheringManager.StopTetheringAsync();
                        Thread.Sleep(2000);
                    }
                    Thread.Sleep(500);
                }
                _networkOperatorTetheringManager = null;
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Default("StopWiFi", ex);
                return false;
            }
        }

        /// <summary>
        /// 外部設備鏈接WiFi監聽事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connectionEventArgs"></param>
        private static void OnConnectionRequested(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs connectionEventArgs)
        {
            try
            {
                WiFiDirectConnectionRequest request = connectionEventArgs.GetConnectionRequest();
                LogHelper.Default(new { Title = "OnConnectionRequested", Data = request.DeviceInformation.Name });
            }
            catch (Exception ex)
            {
                LogHelper.Default("OnConnectionRequested", ex);
            }
        }
        #endregion
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章