Reputation: 41
After spending lot of time on the web (nearly 2 days) I've discovered from Microsoft that the class systen.net.IpAddress does not works yet on UWP (awaiting from them an expected date for it).
What I'm looking for is to get the ipaddress of the connected wifi adapter. This looks like simple but I'm running in an infernal loop and wish you can help me.
1 Names space used :
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.WiFi;
using Windows.Networking.Connectivity;
using Windows.Security.Credentials;
using System.Linq;
using Windows.Devices.Enumeration;
2 - Variables at top (not all are used yet)
//For wifi adapter choosen by the user
private WiFiAdapter wifiSelectedAdapter;
//For wifi network to connect to choosen by the user
private WiFiAvailableNetwork wifiSelectedNetwork;
//For the connection profile
private ConnectionProfile connectionProfile;
private DeviceInformationCollection deviceInfoCollection;
private IPInformation ipInformation;
private string _string;
private string cnxProfile;
private object _object;
3 Finding available adapter
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
//Request wifi access from the device
var access = await WiFiAdapter.RequestAccessAsync();
if (access != WiFiAccessStatus.Allowed)
{
tblDebug.Text = "Access denied";
}
else
{
DataContext = this;
// Find all wifi adapters on the equipment
deviceInfoCollection = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
if (deviceInfoCollection.Count < 1)
{
tblDebug.Text = "No WiFi Adapters detected on you device.";
}
else
{
List<object> ls = new List<object>();
var test = 0;
foreach (var item in deviceInfoCollection)
{
ls.Add(item.Name);
test = test + 1;
}
ListViewResult.ItemsSource = ls;
tblDebug.Text = "Please select the wifi adapter to scan with and click scan button";
btScan.Visibility = Visibility.Visible;
}
}
}
4 - selecting adapter and start scan
private void ListViewResult_ItemClick(object sender, ItemClickEventArgs e)
{
_string = e.ClickedItem as string;
tblDebug.Text = "";
}
private async void btScan_Click(object sender, RoutedEventArgs e)
{
if (_string == null)
{
tblDebug.Text = "Please select an adapter before scanning wifi network";
}
else
{
foreach (var selecteditem in deviceInfoCollection)
{
if (selecteditem.Name == _string)
{
var _id = selecteditem.Id;
try
{
//var access = await WiFiAdapter.RequestAccessAsync();
wifiSelectedAdapter = await WiFiAdapter.FromIdAsync(_id);
}
catch (Exception ex)
{
throw;
}
}
}
//Clear ListView previous adapters list
ListViewResult.ItemsSource = null;
tblDebug.Text = "";
//Scan wifi available network
await wifiSelectedAdapter.ScanAsync();
//Populate ListView with available wifi network
List<string> ls = new List<string>();
var test = 0;
foreach (var network in wifiSelectedAdapter.NetworkReport.AvailableNetworks)
{
ls.Add(network.Ssid);
test = test + 1;
}
ListViewResult.ItemsSource = ls;
if (test <= 0)
{
tblDebug.Text = "No wifi network found.";
}
else
{
tblDebug.Text = "Select the network in the list, you want to connect to and click Connect button.";
btConnect.Visibility = Visibility.Visible;
tblWifiNetwork.Text = "We found the following network(s) :";
}
}
}
5 Selecting a network and connect
private async void btConnect_Click(object sender, RoutedEventArgs e)
{
foreach (var selecteditem in wifiSelectedAdapter.NetworkReport.AvailableNetworks)
{
if (selecteditem.Ssid == _string)
{
wifiSelectedNetwork = selecteditem;
}
}
//Empty the ListViewResult
ListViewResult.ItemsSource = null;
//Create the reconnection Kind to be passed for the connection
WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Automatic;
// Create the result to catch the connection result
WiFiConnectionResult result;
if (wifiSelectedNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211 &&
wifiSelectedNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None)
{
tblWifiNetwork.Text = "";
result = await wifiSelectedAdapter.ConnectAsync(wifiSelectedNetwork, reconnectionKind);
connectionProfile = await wifiSelectedAdapter.NetworkAdapter.GetConnectedProfileAsync();
cnxProfile = connectionProfile.ProfileName;
btDisconnect.Visibility = Visibility.Visible;
if (result.ConnectionStatus == WiFiConnectionStatus.Success)
{
tblDebug.Text = "You are connected to " + cnxProfile + " with ip ";// + ttt.ToString();
btDisconnect.Visibility = Visibility.Visible;
btIpaddress.Visibility = Visibility.Visible;
}
else
{
tblDebug.Text = "Sorry but we can't connect you to the selected network.";
wifiSelectedAdapter.Disconnect();
}
}
else
{
gridSecKey.Visibility = Visibility.Visible;
tblDebug.Text = "The network you are trying to connect to is secured, please enter the security key.";
// Only the password portion of the credential need to be supplied
var credential = new PasswordCredential();
if (!string.IsNullOrEmpty(pwNetworkKey.Password))
{
credential.Password = pwNetworkKey.Password;
result = await wifiSelectedAdapter.ConnectAsync(wifiSelectedNetwork, reconnectionKind, credential);
}
else
{
tblDebug.Text = "Please enter the security key before connecting.";
return;
}
}
}
At this stage I get connected to the desired wifinetwork.
6 Here is where I'm stuck
private async void btIpaddress_Click(object sender, RoutedEventArgs e)
{
var _networkAdapterId = wifiSelectedAdapter.NetworkAdapter.NetworkAdapterId;
var _connectedProfile = await wifiSelectedAdapter.NetworkAdapter.GetConnectedProfileAsync();
var _profileName = _connectedProfile.ProfileName;
var cnxProfile = NetworkInformation.GetConnectionProfiles().ToList();
List<object> _list = new List<object>();
foreach (var item in cnxProfile)
{
if (_profileName == item.ProfileName)
{
List<string> ipAddress = new List<string>();
var Hosts = NetworkInformation.GetHostNames();
foreach (var Host in Hosts)
{
var tt = Host.DisplayName;
{
string IP = Host.DisplayName;
ipAddress.Add(IP);
}
}
}
}
}
With the function NetworkInformation.GetHostNames();
I'm getting all the hostnames actually connected on my equipment.
The question : How to link my _networkAdapterId
with the relevant hostname from the function NetworkInformation.GetHostNames()
?
In the second picture, below IPInformation/NetworkAdapter we can find the NetworkAdapterID.
In other words, from this NetworkAdapterID, how can I retrieve it's IP address on UWP ?
Upvotes: 3
Views: 2550
Reputation: 41
Finally I came to as solution which looks like not so bad :)
'var _networkAdapterId = wifiSelectedAdapter.NetworkAdapter.NetworkAdapterId;
var connectedProfile = NetworkInformation.GetConnectionProfiles().ToList();
foreach (var item in connectedProfile)
{
if (_networkAdapterId == item.NetworkAdapter.NetworkAdapterId)
{
var hostname = NetworkInformation.GetHostNames()
.SingleOrDefault(
hn =>
hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null
&& hn.IPInformation.NetworkAdapter.NetworkAdapterId
== _networkAdapterId);
if (hostname != null)
{
// the ip address
_connectedIpAddress = hostname.CanonicalName;
}
}
}
Upvotes: 1