Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the currently connected WiFi SSID and signal strength in dotnet core?

Tags:

c#

.net

.net-core

I'm aware of this post Get SSID of the wireless network I am connected to with C# .Net on Windows Vista It described how to get an ssid in C#. However, that library isn't supported in dotnet core. Is there a way to get the ssid and signal strength of the currently connected wifi adapter in dotnet core? I've tried playing with the NetworkInterface namespace (System.Net.NetworkInformation), and that seems to give me at least some diagnostic information about the network, but it definitely doesn't include the ssid. What do you guys think? Is this supported (yet) ?

The code I was playing with is below. It doesn't give me what I need though. I was mostly using it to explore with the debugger.

var interfaces = NetworkInterface.GetAllNetworkInterfaces();
var connected = interfaces
     .Where(i => i.OperationalStatus == OperationalStatus.Up)
     .Where(i => i.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
     .ToList();

var wifi = connected[0];
var props = wifi.GetIPProperties();
var addr = wifi.GetPhysicalAddress();
var stats = wifi.GetIPStatistics();

Thanks guys.

like image 992
cdhanna Avatar asked Nov 17 '16 01:11

cdhanna


1 Answers

I don't know what .NET Core version you are using, but the following NuGet package from MSFT integrates with my .NET Core 3.0 app:

Microsoft.WindowsAPICodePack-Core version 1.1.0.2

Although the library is rather old - it dates back to 2014 - and it is not conforming to .NET Standard. Obviously this is not cross-platform and works only on Windows. It does not provide the information you require, but maybe it is a useful hint to the real solution?

Example code:

var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);            
foreach (var network in networks) { 
    sConnected = ((network.IsConnected == true) ? " (connected)" : " (disconnected)");
    Console.WriteLine("Network : " + network.Name + " - Category : " + network.Category.ToString() + sConnected);
}

Signal strength

With the following PowerShell gist utilizing netsh you will get the signal strength:

(netsh wlan show interfaces) -Match '^\s+Signal' -Replace '^\s+Signal\s+:\s+',''

After further Googling I stubbled upon the Codeplex archive of the ManagedWlan project:

https://archive.codeplex.com/?p=managedwifi

It provides a broad range of interops with the Windows kernel APIs. I believe it also allows you to retrieve the signal strength. I have migrated the provided MIT licensed source code to a GitHub repository: https://github.com/cveld/ManagedWifi

It compiles towards .NET Core 3.0 but is obviously not cross-platform.

like image 60
Carl in 't Veld Avatar answered Oct 16 '22 11:10

Carl in 't Veld