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 phones.
Of course, the WifiManager is useless when it comes to get info from the AP.
Luckily, I've been able to get the IPs of all the interfaces by doing this:
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()) {
Log.d("IPs", inetAddress.getHostAddress() );
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
This chunk of code will print all the IP of all the interfaces (Wifi hotspot included). The main problem is that I don't find a way to identify the WiFi interface. This is an issue since some phones have multiple interfaces (WiMax, etc). This is what I've tried so far:
- Filtering by the wifi iface display name: it's not a good approach because the display name changes from one device to another (wlan0, eth0, wl0.1, etc).
- Filtering by its mac address: almost work, but on some devices the hotspot iface does not have a MAC address ( iface.getHardwareAddress() returns null)...so not a valid solution.
Any suggestions?
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.
Then you can use it like this from the activity where you use the WiFi service
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.
I use the solution of ajma, changing
intf.getName().contains("wlan")
tointf.getName().contains("wl") || intf.getName().contains("ap")
. And it works for many mobile phones.But it returns null when you just connected to a WiFi.
Here is a possible solution that utilizes
WiFiManager
ConnectionInfo
to find correspondingNetworkInterface
.If you just need the IP then you can use:
When the Wifi is not setup as a hotspot, it has a name
android-xx7632x324x32423
home when hotspot is turned on, that name is gone. Also the ip address changes.So if you are able to get the Wifi config before enabling the hotspot, first of all you can use
intf.getName()
to get a reference to it.Second, the ip changed, so if you know which interface the wifi is in
CONNECTED
mode, you can use that info to identify it later on after enabling the hotspot.Below is some code I used for debugging. I just spit out everything I can find, make a huge mess then clean it up when I figured my problem out. GL
You can use that. It's not tested but it should works.