So I followed this example: How to find a list of wireless networks (SSID's) C# and now have following code using Native Wifi (like the accepted answer of the link):
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
SomeListBox.Add(Encoding.ASCII.GetString(network.SSID, 0, (int)network.SSIDLength))
}
}
But this doesn't list all networks it only lists the one I'm currently connected to. Only if I press the Windows Wlan button in the taskbar, the code lists all networks. Did I do something wrong or is there a way to trigger this Windows Wlan scan thing?
Thanks in advance ;)
Ok so calling wlanIface.Scan(); before Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0); did the thing for me so that the full code now looks like this:
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
wlanIface.Scan();
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
SomeListBox.Add(Encoding.ASCII.GetString(network.SSID, 0, (int)network.SSIDLength))
}
}
Related
I am using xamarin forms and i have one problem,
i am trying to get the DNS server Adresse from WIFI, and a im using below code :
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
foreach (IPAddress dnsAdress in dnsAddresses)
{
Debug.WriteLine(dnsAdress);
}
}
}
The problem is when i use this code on Microsoft Emulator "UWP" everything work fine see picture bellow using UWP device
but nothing work when i use my android mobile see picture bellow
using android device
NB: all devices are connect on the same wifi
I had done a simple to test your code and got the same null. And then I read the IPInterfaceProperties.DnsAddresses Property document, it seems that it doesn't support the xamarin.android.
You can check the it:https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipinterfaceproperties.dnsaddresses?view=net-6.0
In addition, I also search this but can not find any solution. So I try to use the android native method to get the dns address with the following code. So you can try to use the dependency service to get the dns address for android.
ConnectivityManager cm = (ConnectivityManager)GetSystemService(ConnectivityService);
//NetworkInfo has been deprecated, but I can not find the new api to to this
NetworkInfo activeNetworkInfo = cm.ActiveNetworkInfo;
foreach (Network network in cm.GetAllNetworks())
{
NetworkInfo networkInfo = cm.GetNetworkInfo(network);
if (networkInfo.GetType() == activeNetworkInfo.GetType())
{
LinkProperties lp = cm.GetLinkProperties(network);
foreach (InetAddress addr in lp.DnsServers)
{
var a = addr.GetAddress();
}
}
}
I have used both Batch and shell scripts to accomplish this, however, the fact that I have to go one by one through the saved xml files is a bit tedious.
I am trying to find a way to accomplish this through the NativeWifi API (found here here) However all I get are the available wireless access points and their signal strength.
Current goal for this question: retrieve saved wireless profiles in a folder if a physical wireless adapter exists.
BATCH FILE:
set wlanfld="WirelessProfiles"
if not exist %wlanfld% md %wlanfld%
echo off
netsh wlan export profile folder=%wlanfld% key=clear
setlocal enabledelayedexpansion
for /r %wlanfld% %%a in (*.xml) do (
set oldName=%%~xna
set newName=!oldName:~28!
ren "%%a" "!newName!"
)
Then here is a snippet of the code.
private void wifiButton_Click(object sender, EventArgs e)
{
textBox2.Text = "";
System.Diagnostics.Process.Start(#"c:\\Users\jpearson\Documents\Visual Studio 2013\Projects\ComplianceGuide\ComplianceGuide\wifi.bat");
Process.Start(#"c:\\Users\jpearson\Documents\Visual Studio 2013\Projects\ComplianceGuide\ComplianceGuide\bin\Debug\WirelessProfiles");
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIFace in client.Interfaces)
{
Wlan.WlanAvailableNetwork[] networks = wlanIFace.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
wifiBox.Text += String.Format("{0} Signal Quality {1}" + Environment.NewLine, GetString(network.dot11Ssid), network.wlanSignalQuality);
}
}
}
I'm trying to build C# app (desktop,windows, it doesn't really matter for now), with which I would like to connect to my windows phone, using sockets, to transfer some data... I know how this can be achieved,.
When connecting through sockets I don't want to manually enter windows phone device's IP address. So I want to know if it is possible to send some HTTP request from Windows phone app with some message, and fetch that message on computer, to be sure which IP is windows phone's IP?
In other words how to know which IP address belongs to Windows phone's IP address from Bunch of Ip addresses of devices on network?
Fallow this Link from Here
Add NameSpace :
using Windows.Networking;
public static string FindIPAddress()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
//IanaInterfaceType == 71 => Wifi
//IanaInterfaceType == 6 => Ethernet (Emulator)
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
if (ipAddresses.Count < 1)
{
return null;
}
else if (ipAddresses.Count == 1)
{
return ipAddresses[0];
}
else
{
//if multiple suitable address were found use the last one
//(regularly the external interface of an emulated device)
return ipAddresses[ipAddresses.Count - 1];
}
}
Edit:
If I am wrong You want to get particular Windows Phone IP address from bunch of IP addresses. AFAIK you can't get like this. But I suggest you to Append some string when getting IP of Windows Phone like below which will vary from other Ip addresses by looking it self.
string IPadd = FindIPAddress();
string WPIPadd = IPadd + " - Windows phone "
MessageDialog.show(WPIPadd);
If you find any thing new apart from this Let us know. Thanks
Is there a way to get a list of all SSID's and their mac address of reachable signals in my area?
I tried the Nativ WlanApi in my c# code. What I get is the list of all ssid's, but for
getting their mac address, I don't have any idea.
This is the code I using for getting the list:
private void show_all_ssids_Click(object sender, EventArgs e)
{
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
// Lists all available networks
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
this.ssidList.Text = "";
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
//Trace.WriteLine( GetStringForSSID(network.dot11Ssid));
this.ssidList.Text += GetStringForSSID(network.dot11Ssid) + "\r\n";
}
}
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
}
I hope there is a way.
In order to get a MAC address you would need to connect to that wireless network. Once you are connected you should be able to get the MAC address of machines on the immediate network using the same methods that you might for traditional wired networks - I believe that the best way of doing that would be by parsing the output of the arp -a command.
this is the solution:
Dim networksBss As Wlan.WlanBssEntry() = SelectedWifiAdapter.GetNetworkBssList()
For car = 0 To networksBss(i).dot11Bssid.Length - 1
If Len(Hex(networksBss(i).dot11Bssid(car))) = 1 Then ThisScan(i).MAC = ThisScan(i).MAC & "0"
ThisScan(i).MAC = ThisScan(i).MAC & Hex(networksBss(i).dot11Bssid(car)) & ":"
Next
anyway i'm still looking for a way to find details (strenght) of networks with SSID="" associating it with the proper MAC.
Is there a toolkit/package that is available that I could use to find a list of wireless networks (SSID's) that are available in either Java, C#, or C for Windows XP+? Any sample code would be appreciated.
For C#, take a look at the Managed Wifi API, which is a wrapper for the Native Wifi API provided with Windows XP SP2 and later.
I have not tested this code, but looking at the Managed Wifi API sample code, this should list the available SSIDs.
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
// Lists all available networks
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
Console.WriteLine( "Found network with SSID {0}.", GetStringForSSID(network.dot11Ssid));
}
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.ASCII.GetString( ssid.SSID, 0, (int) ssid.SSIDLength );
}
ArrayList<String>ssids=new ArrayList<String>();
ArrayList<String>signals=new ArrayList<String>();
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh wlan show all");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line.contains("SSID")||line.contains("Signal")){
if(!line.contains("BSSID"))
if(line.contains("SSID")&&!line.contains("name")&&!line.contains("SSIDs"))
{
line=line.substring(8);
ssids.add(line);
}
if(line.contains("Signal"))
{
line=line.substring(30);
signals.add(line);
}
if(signals.size()==7)
{
break;
}
}
}
for (int i=1;i<ssids.size();i++)
{
System.out.println("SSID name == "+ssids.get(i)+" and its signal == "+signals.get(i) );
}
Well, you didn't specify the OS so, for Linux I will suggest Wireless Tools for Linux by Jean Tourrilhes (http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html). The iwlist() command displays a lot of information about the available networks. The source code is in C. Another way is to write your own code in C using libpcap for capturing the beacon frames and extracting SSID from them (in monitor mode only). I haven't tested my sniffing code yet so I won't paste it here but it is pretty simple job.