tags:

views:

108

answers:

3

Hi all, My phone has an IP. I would like to know how could I retrieve it? After searching in the web, I found that i can get only the current outside ip.(I want the local-perm ip of my phone)

Thanks,

ray.

+1  A: 

your ip will change with every networkthat you connect to - your phone has a mac address - is that what you are looking to find?

PaulStack
My phone has internal ip that never changes, thats what I need.
rayman
+3  A: 

A quick search on google sent me here: http://www.droidnova.com/get-the-ip-address-of-your-device,304.html

Read the comments about how to use the first block of code to get the wifi ip address (on local network, not public ip)

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();

Edit: The emulator seems to return 0 on wifiInfo.getIpAddress(), but it works fine on a phone. The following code converts the integer to an ip address:

String ipBinary = Integer.toBinaryString(ipAddress);

//get the four different parts
String a=ipBinary.substring(0,8);
String b=ipBinary.substring(8,16);
String c=ipBinary.substring(16,24);
String d=ipBinary.substring(24,32);

//Convert to numbers
String actualIpAddress =Integer.parseInt(d,2)+"."+Integer.parseInt(c,2)+"."+Integer.parseInt(b,2)+"."+Integer.parseInt(a,2);
Kjetil Watnedal
Hi, thats not what I wanted. this is the external IP address.
rayman
It is my understanding that it will return the internal IP address. Read the comment "by Erren on September 8, 2009". Seems he get the internal ip address, but backwards or something. He writes "As my wlan IP address is 10.83.35.80" and the address he gets back is 80.35.83.10. I don't have a dev env up, so I cant check it myself.
Kjetil Watnedal
@rayman: Kjetil is right, you should simply follow the linked how to
WarrenFaith
I tried it, and the result i got was: 0
rayman
I have set up my dev env and actually tried it now. It does work, but not on the emulator. The emulator returns 0. It works when you run it on you phone.
Kjetil Watnedal
A: 

The IP which I may would call "local-perm" is the localhost IP which is always 127.0.0.1

The IP of any other network adapter (Wifi in this case) changes depending on the network

Martin
That cant be: I have this device: Opus One. and I see this ip: 10.55.170.* (ive hidden the last number), and it never changes. I need to retrive that IP
rayman
What kind of network does your device use? And how do you get this IP adress? All I know is that there is only the localhost ip and the internet ip.
WarrenFaith
I use network: iDEN. and my local ip is static, and I got it with the phone. is there a way to retrieve the local ip?
rayman