Recover network gateway?

2

Does anyone know or know any way to find the ip address of gateway of the network? The local ip I know I can recover with class InetAddress : InetAddress.getLocalHost().getHostAddress();

    
asked by anonymous 23.04.2018 / 22:12

1 answer

1

It has a class called DhcpInfo inside the android.net package, it has some variables with network parameters. But the problem that these values should be converted.

Image to describe the scenario:

Javacode:

packagecom.schogini.dhcp;importandroid.app.Activity;importandroid.content.Context;importandroid.os.Bundle;importandroid.widget.TextView;importandroid.net.*;importandroid.net.wifi.WifiManager;publicclassdhcpInfoextendsActivity{publicStrings_dns1;publicStrings_dns2;publicStrings_gateway;publicStrings_ipAddress;publicStrings_leaseDuration;publicStrings_netmask;publicStrings_serverAddress;TextViewinfo;DhcpInfod;WifiManagerwifii;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){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 do 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

    
23.04.2018 / 22:22