2
Does anyone know or knows any way to find the IP address of gateway
network ? The local ip I know I can recover with the class InetAddress
: InetAddress.getLocalHost().getHostAddress();
2
Does anyone know or knows any way to find the IP address of gateway
network ? The local ip I know I can recover with the class InetAddress
: InetAddress.getLocalHost().getHostAddress();
1
Has a class called DhcpInfo
within the package android.net
, it has some variables with network parameters. But the problem that these values should be converted.
Image to describe the scenario:
Java code:
package com.schogini.dhcp;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.net.*;
import android.net.wifi.WifiManager;
public class dhcpInfo extends Activity {
public String s_dns1 ;
public String s_dns2;
public String s_gateway;
public String s_ipAddress;
public String s_leaseDuration;
public String s_netmask;
public String s_serverAddress;
TextView info;
DhcpInfo d;
WifiManager wifii;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
d=wifii.getDhcpInfo();
s_dns1="DNS 1: "+String.valueOf(d.dns1);
s_dns2="DNS 2: "+String.valueOf(d.dns2);
s_gateway="Default Gateway: "+String.valueOf(d.gateway);
s_ipAddress="IP Address: "+String.valueOf(d.ipAddress);
s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);
s_netmask="Subnet Mask: "+String.valueOf(d.netmask);
s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);
//dispaly them
info= (TextView) findViewById(R.id.infolbl);
info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.schogini.dhcp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".dhcpInfo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>
To make the conversion:
public String intToIp(int i) {
return ((i >> 24 ) & 0xFF ) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF) ;
}
Or
public String intToIp(int addr) {
return ((addr & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF));
}
Source: Programmatically Getting the gateway and subnet Mask Details
Browser other questions tagged java android
You are not signed in. Login or sign up in order to post.
Wictor thanks for the help, I have only one question, the line that creates the instance of the Wifimanager class, is accusing the error : Cannot resolve method getSystemService(java.lang.String)
– Yuri Ferreira
Try replacing wifii= (Wifimanager) getSystemService(Context.WIFI_SERVICE); by wifii = (Wifimanager) getActivity(). getSystemService(Context.WIFI_SERVICE);
– Wictor Chaves
thank you, I will try to make the modifications today still !
– Yuri Ferreira
Thank you Wictor, your code is working. Could you explain this method to me ? because I am able to get the ip of the gateway, but it is returning the ip backwards. example 1.200.168.192
– Yuri Ferreira
He takes that numbering and converts to ip format, that table demonstrates how it works, anyway I did another method(The third) reversing the numbering, since this with the output invested, and put a second method of the link I put as reference.
– Wictor Chaves