As the title says... I\'m trying to be able to get the IP of the wifi iface when it is configured as hotspot. Ideally, I would like to find something that works for all the phon
With the number of new phones coming out every year from new manufacturers simply identifying the name of the wireless interface is bound to fail in the nearest future. The following method can get the remote server ip from the integer ip returned by getDhcpInfo().serverAddress.
public String getIPv4Address(int ipAddress) {
// convert integer ip to a byte array
byte[] tempAddress = BigInteger.valueOf(ipAddress).toByteArray();
int size = tempAddress.length;
// reverse the content of the byte array
for(int i = 0; i < size/2; i++) {
byte temp = tempAddress[size-1-i];
tempAddress[size-1-i] = tempAddress[i];
tempAddress[i] = temp;
}
try {
// get the IPv4 formatted ip from the reversed byte array
InetAddress inetIP = InetAddress.getByAddress(tempAddress);
return inetIP.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "";
}
Then you can use it like this from the activity where you use the WiFi service
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String serverIp = getIPv4Address(wifi.getDhcpInfo().serverAddress);
Log.v("Server Ip", serverIp);
This should show you the IP of the connected server.
NOTE: ensure you have already created a successful connection to an access point (e.g. hotspot) via WiFi before querying for the server ip. You only need the SSID and preSharedkey (if it's secure) to create a successful connection and not the server ip.