- 1 year ago
C# Get name of Wifi Direct Device Connected and OS family
To get the name of the Wi-Fi Direct device connected and the operating system family using C#, you can utilize the Windows.Devices.WiFiDirect
namespace from the UWP (Universal Windows Platform) API. Here's an example:
csharpusing System;
using Windows.Devices.Enumeration;
using Windows.Devices.WiFiDirect;
namespace WiFiDirectDeviceInfo
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// Find all Wi-Fi Direct devices
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(WiFiDirectDevice.GetDeviceSelector());
foreach (DeviceInformation deviceInfo in devices)
{
// Get the Wi-Fi Direct device object
WiFiDirectDevice device = await WiFiDirectDevice.FromIdAsync(deviceInfo.Id);
// Get the device name
string deviceName = device.Information.DeviceName;
Console.WriteLine("Device Name: " + deviceName);
// Get the operating system family
string osFamily = device.Information.OperatingSystemDisplayName;
Console.WriteLine("OS Family: " + osFamily);
device.Dispose();
}
}
}
}
In this example, we use the DeviceInformation
class to find all available Wi-Fi Direct devices using DeviceInformation.FindAllAsync
and the WiFiDirectDevice.GetDeviceSelector
method. Then, we iterate over each device to get its details.
Using the WiFiDirectDevice.FromIdAsync
method, we obtain a WiFiDirectDevice
object that represents the Wi-Fi Direct device. From this object, we can retrieve the device name using the Information.DeviceName
property. Additionally, we can access the operating system family using the Information.OperatingSystemDisplayName
property.
The example above assumes you are developing a UWP app and that you have the necessary capabilities declared in your app manifest to access Wi-Fi Direct devices. Make sure to set the appropriate permissions in your UWP project.
When you run the code, it will list the name of each connected Wi-Fi Direct device and the operating system family it belongs to.