java - getHostAddress() returns a reversed ip address -
i'm trying cell phone ip address using wifimanager , wifiinfo classes.
it returns correct ip address reversed.
public string getwifiipaddress() { wifimanager wm = (wifimanager) getsystemservice(wifi_service); wifiinfo wi = wm.getconnectioninfo(); byte[] ipaddress = biginteger.valueof(wi.getipaddress()).tobytearray(); try { inetaddress myaddr = inetaddress.getbyaddress(ipaddress); string hostaddr = myaddr.gethostaddress(); return hostaddr; } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } return ""; }
result : 73.0.168.192
ok, saw address reversed! :)
it referred big/little endian issue, read more endianness must-to-know programmers, specially when doing applications integrations , migrations on different operating systems.
add after gettting connection info wifi manager.
int ipaddress = wi.getipaddress(); ipaddress = (byteorder.nativeorder().equals(byteorder.little_endian)) ? integer.reversebytes(ipaddress) : ipaddress;
then continue code tobytearray , gethostaddress etc.
Comments
Post a Comment