C#打开WiFi热点,WiFi共享的四种方式总结(三)
拚忘的博客园要开始写随笔了,第一期就先讲讲wifi热点吧.....
持续更新C#、wpf、.NetCore相关内容,欢迎关注!!
C#打开WiFi热点,WiFi共享的四种方式总结(一)
C#打开WiFi热点,WiFi共享的四种方式总结(二)
C#打开WiFi热点,WiFi共享的四种方式总结(三)
C#打开WiFi热点,WiFi共享的四种方式总结(四)
C# Windows系统 ,WiFi共享的方式,打开WiFi热点我最近研究总结了又四种方式开启。
1.通过netsh wlan命令行开启
2.通过wlanapi接口共享
3.通过Win10 Api开启,Windows.Devices.WiFiDirect
4.通过Win10 api,Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager 开启电脑自带的热点。
三、第三种方式是通过win10的APi开启,
官网种说明这些API用于Windows 10上的UWP应用程序开发。那么在WPF种是否可以使用呢,答案是肯定的。
如何在wpf或者是winform使用这些API呢?参考:https://www.cnblogs.com/panwangvie/p/14870183.html
微软官网中的API在Windows.Devices.WiFiDirect.dll类库中; 详细介绍https://docs.microsoft.com/en-us/uwp/api/windows.devices.wifidirect?view=winrt-20348
详细代码如下:
private WiFiDirectAdvertisementPublisher publisher_; private WiFiDirectAdvertisement advertisement_; private WiFiDirectLegacySettings legacySettings_; private WiFiDirectConnectionListener connectionListener_; private readonly List<WiFiDirectDevice> connectedDevices_; private string ssid_; private string passphrase_; private void StartConnectionListener() { connectionListener_ = new WiFiDirectConnectionListener(); connectionListener_.ConnectionRequested += ConnectionListenerOnConnectionRequested; } private async void ConnectionListenerOnConnectionRequested(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs args) { try { var connectionRequest = args.GetConnectionRequest(); if (connectionRequest == null) { throw new WlanHostedNetworkException( "Call to ConnectionRequestedEventArgs.GetConnectionRequest() return a null result."); } var deviceInfo = connectionRequest.DeviceInformation; var wiFiDirectDevice = await WiFiDirectDevice.FromIdAsync(deviceInfo.Id); if (wiFiDirectDevice == null) { throw new WlanHostedNetworkException($"Connection to {deviceInfo.Id} failed;"); } if (wiFiDirectDevice.ConnectionStatus == WiFiDirectConnectionStatus.Connected) { var endpointPairs = wiFiDirectDevice.GetConnectionEndpointPairs(); var connection = endpointPairs.First(); var remoteHostName = connection.RemoteHostName; var remoteHostNameDisplay = remoteHostName.DisplayName; RaiseDeviceConnected(wiFiDirectDevice, $"{remoteHostNameDisplay} connected."); connectedDevices_.Add(wiFiDirectDevice); } wiFiDirectDevice.ConnectionStatusChanged += WfdDeviceOnConnectionStatusChanged; }catch(Exception ex) { throw new WlanHostedNetworkException(ex.Message, ex); } } private void WfdDeviceOnConnectionStatusChanged(WiFiDirectDevice wiFiDirectDevice, object args) { var endpointPairs = wiFiDirectDevice.GetConnectionEndpointPairs(); var connection = endpointPairs.First(); var remoteHostName = connection.RemoteHostName; var remoteHostNameDisplay = remoteHostName.DisplayName; var status = wiFiDirectDevice.ConnectionStatus; if (status == WiFiDirectConnectionStatus.Connected) { RaiseDeviceConnected(wiFiDirectDevice, $"{remoteHostNameDisplay} connected"); } else { if (connectedDevices_.Contains(wiFiDirectDevice)) { connectedDevices_.Remove(wiFiDirectDevice); } RaiseDeviceDisconnected(wiFiDirectDevice, $"{remoteHostNameDisplay} disconnected."); } } public void Start() { Reset(); publisher_ = new WiFiDirectAdvertisementPublisher(); publisher_.StatusChanged += PublisherOnStatusChanged; advertisement_ = publisher_.Advertisement; advertisement_.IsAutonomousGroupOwnerEnabled = true; legacySettings_ = advertisement_.LegacySettings; legacySettings_.IsEnabled = true; if (SsidProvided) { legacySettings_.Ssid = Ssid; } else { ssid_ = legacySettings_.Ssid; } var passwordCredentials = legacySettings_.Passphrase; if (PassphraseProvided) { passwordCredentials.Password = Passphrase; } else { passphrase_ = passwordCredentials.Password; } publisher_.Start(); } public void Stop() { publisher_?.Stop(); } private void PublisherOnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args) { try { var status = args.Status; switch (status) { case WiFiDirectAdvertisementPublisherStatus.Started: { StartConnectionListener(); RaiseAdvertisementStarted("Advertisement started."); break; } case WiFiDirectAdvertisementPublisherStatus.Aborted: { var error = args.Error; string message; switch (error) { case WiFiDirectError.RadioNotAvailable: message = "Advertisement aborted, Wi-Fi radio is turned off"; break; case WiFiDirectError.ResourceInUse: message = "Advertisement aborted, Resource In Use"; break; default: message = "Advertisement aborted, unknown reason"; break; } RaiseAdvertisementAborted(message); break; } case WiFiDirectAdvertisementPublisherStatus.Stopped: { RaiseAdvertisementStopped("Advertisement stopped"); break; } } } catch (WlanHostedNetworkException ex) { RaiseAsyncException(ex.Message); } }
这种方法的好处:
是直接调用封装好的dll方便,且能够监听到设备连接情况。
缺点:
但是这个方法并非是直接打开win电脑中自带的那个移动热点。当我把电脑中自带的移动热点打开时,此方法开启的热点会断开。
如何开启电脑自带的移动热点呢,看下一篇。C#打开WiFi热点,WiFi共享的四种方式总结(四)