问题
I'm currently developing an application where I need to create a link (read: WiFi link) between a desktop app (on Windows 10) and a tablet (Android, but it is irrelevant). Workflow: Push button -> elevate privileges if required -> Create hosted network-like WiFi network -> Allow device to connect providing a SSID/password/dynamic IP address...
Previously, I used a system call to netsh (running the app with elevated privileges) to create a hosted network. Now it seems it is less and less possible to proceed this way (example: on the same computer, it worked on Win 7 but it no longer works on Win 10). It seems to be a driver problem because it still works with an external USB antenna but not with the internal antenna. Any way, I don't want to go further with this solution.
My goal: to be able to do this programmatically using an API. I saw a lot of discussions about WiFi Direct vs Hosted Network and it seems that Hosted Network is a vanishing technology while WiFi Direct has a brilliant future??? I don't know.
I found WiFi Direct API but they look Universal Windows (UWP) oriented while I want to be able to use them in a simple C# app. This post shows how to hack the system and use the API with a simple C# console app. So far, so good, it worked.
In order to have a simple use, I used the API in Legacy mode, giving a SSID and a password in such a way my tablet see a network. WiFiDirectAdvertisementPublisherAdvertisement.LegacySettings.IsEnabled = true;
The connection is very fast and everything is good.
Where it fails is that I use a streamer (something like splashtop) and it disconnects after 1-2 minutes (the streamer disconnects, not the WiFi). On the opposite, if I simply go to the "Wireless Access Point" form in Windows settings and activate it, the communication holds for more than 24 hours (and it's still running now without flaw). So, the problem is not with the streamer, neither the WiFi device hardware/driver. My first idea is that there is a WiFi Direct setting that is badly set to sustain a streaming data flow.
Going with that I'm unable to find doc on the web. The Microsoft Wlanapi.dll documentation is so-so... and I still don't know if I must focus to WiFi Direct (really?) or stick to Hosted Network because it proved it works fine?
Here is my code that works fine to maintain a WiFi link but that makes the streamer to disconnect shortly:
using System;
using Windows.Devices.WiFiDirect;
using Windows.Security.Credentials;
namespace WFDcs_1
{
class Program
{
private WiFiDirectAdvertisementPublisher mPublisher = null;
private bool mConnected = false;
static void Main(string[] args)
{
Program zeProgram = new Program(args);
}
Program(string[] args)
{
StartAdvertisement(WiFiDirectAdvertisementListenStateDiscoverability.Normal);
Console.WriteLine("Hit a key to quit...");
Console.ReadKey();
}
void StopAdvertisement()
{
if (mConnected)
{
mPublisher.Stop();
mPublisher.StatusChanged -= OnStatusChanged;
}
}
void StartAdvertisement(WiFiDirectAdvertisementListenStateDiscoverability discoverability)
{
if ( mPublisher == null )
{
mPublisher = new WiFiDirectAdvertisementPublisher();
}
mPublisher.StatusChanged += OnStatusChanged;
mPublisher.Advertisement.IsAutonomousGroupOwnerEnabled = true;
mPublisher.Advertisement.LegacySettings.IsEnabled = true;
mPublisher.Advertisement.LegacySettings.Ssid = "MyGloriousSSID";
PasswordCredential lCred = new PasswordCredential();
lCred.Password = "test1234";
mPublisher.Advertisement.LegacySettings.Passphrase = lCred;
mPublisher.Advertisement.ListenStateDiscoverability = discoverability;
mPublisher.Start();
}
void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs statusEventArgs)
{
// *** 1 ***
Console.WriteLine("OnStatusChanged(...): New connection status: {0}", statusEventArgs.Status.ToString());
}
}
}
I would appreciate some reference to tutorials, examples, hints, whatever that could help me. Thank you!
回答1:
After looking to example refered to by Mike Petrichenko, I finaly found this link: (native c++ example to download) in this page. It makes use of WiFiDirect in legacy mode to simulate a hosted network. It uses WRL. No *.vcxproj
file to hack, no "strange" library to link to. Also there are some more or less useful explanations here. Exactly what I wanted.
I must say that this information was not easy to find...
来源:https://stackoverflow.com/questions/55121033/on-using-the-wifi-direct-api-on-windows