I tried to get IP address of wifi connected devices in android. But I couldn't get those. If anyone has rich experience in this, can you help me? Hope to explain with results in details.
public void findAdressConnectedWifi() {
try {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
Toast.makeText(this, ipAddress, Toast.LENGTH_LONG).show();
String prefix = ipAddress.substring(0, ipAddress.lastIndexOf(".") + 1);
Log.d("wifi => ", "prefix: " + prefix);
for (int i = 0; i < 255; i++) {
String testIp = prefix + String.valueOf(i);
InetAddress address = InetAddress.getByName(testIp);
boolean reachable = address.isReachable(1000);
String hostName = address.getCanonicalHostName();
if (reachable) {
MainActivity.this.ipAddressArray.add(testIp);
Log.i("wifi => ", "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!");
Toast.makeText(this, "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}