String ip = InetAddress.getLocalHost().getHostAddress();
일반 PC java에서는 192.168.10.3 식으로 IPv4 주소가 정상적으로 나옵니다.
그러나, 갤S 2.2 Android에서 실행해 보면 WiFi 모드 인데도 127.0.0.1 로만 나오네요.
WiFi 모드에서 휴대폰의 local ip 를 얻을 수 있는 방법은 무엇일까요?
참고로, NetworkInterfaces를 얻어오는 경우에 pc에서 해보니 IPv6 주소가 올라오네요.
종선애비님 댓글 감사합니다.
NetworkInterface로 PC에서 해보면 IPv6 주소가 나와서 글을 올렸습니다.
실제 갤S 2.2에서 해보니 IPv4 주소가 나오네요.
그러나, 3G 상태에서 HSDPA가 활성화되지 않은 순간에는 아무런 IP도 가져오지 않았습니다.
참고로, SKT의 3G IP는 10.으로 시작하는 private ip 입니다.
이후에 찾으시는 분이 도움을 얻기를 기대합니다. ^^
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}




이더넷(ethernet) 장치가 여러개로 분류되는데 127.0.0.1은 loop-back으로 lo 장치에 해당합니다.
아마도 이게 기본으로 되어있기때문에, lo 값이 출력되는것 같네요.
원하는 IP를 얻으려면 lo가 아닌 eth0 장치나 다른 장치들의 리스트를 뽑아서 거기서 추출해야 할 듯 싶군요.
대략 이런식으로 하면 나오지 않을까 싶군요.
for ( Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
iface = ifaces.nextElement();
InetAddress ia = null;
for ( Enumeration<InetAddress> ips = iface.getInetAddresses(); ips.hasMoreElements(); ) {
ia = (InetAddress) ips.nextElement();
String tmpAddr = ia.getHostAddress();
}
}