bnikhil
bnikhil

Reputation: 37

Windows phone 8 - IP address is of wifi or carrier

I am developing an application on windows phone 8, and would like to know - Is it possible to check whether retrieved device IP address is over Wifi or carrier?

        Code used to find device IP address is -

        public IPAddress IdentifyDeviceIp()
            {
                    List<string> DeviceIPAddresses = new List<string>();
                    var DeviceHostnames = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();
                    foreach (var DeviceHostName in DeviceHostnames)
                    {
                        if (DeviceHostName.IPInformation != null)
                        {
                            string DeviceIpAddress = DeviceHostName.DisplayName;
                            // Emulator: ignore IPV6 addresses
                            if (DeviceIpAddress.Contains(":"))
                                continue;
                            DeviceIPAddresses.Add(DeviceIpAddress);
                        }
                    }          
                if (DeviceIPAddresses.Count == 0)
                {
                    MessageBox.Show("No IP address found!!");
                    return new IPAddress(0);
                }
                return IPAddress.Parse(DeviceIPAddresses[0]);
            }

Upvotes: 0

Views: 1308

Answers (2)

Doc Roms
Doc Roms

Reputation: 3308

you can use this code for determinate where is the type of Network Interface :

NetworkInterfaceType MyNetworkInterfaceType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType; 

If you've on Wifi, this code return "Wireless80211", you can read all documentation here

Also, with Windows Phone, you can Set your prefer NetworkInterface ( If you hove connected on Wifi and, on 3G, you can create a request with the Cellular connection (2G/3G/4G) or with NonCellular connection ( Ethernet, Wifi...) you can read this for information

You can Set your prefer Network for SocketRequest and for WebRequest you can read documentation about that in the msdn :

Microsoft.Phone.Net.NetworkInformation.WebRequestExtensions

Microsoft.Phone.Net.NetworkInformation.SocketExtensions

Use just the function

SetNetworkPreference(Socket/WebRequest, NetworkSelectionCharacteristics)

for define a request with your prefer network.

For better Experience for your application user, prefer the NonCellular DataConnection, generaly, it's faster [except for 4G] and cheaper... :D

For Your Problem, If you Set your prefer Connection, and you send a request, the ip adress use for this request must match the network defined preference.

Upvotes: 1

Pavel Saniuk
Pavel Saniuk

Reputation: 788

To determine what a network is currently used by a phone you can check NetworkInterfaceType. Mode details here http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh487166(v=vs.105).aspx.

Upvotes: 1

Related Questions