1

I have a question I want get my mobile IP

whether is Network or Wi-Fi

And I try some way , but I don't know why I can't get it

 public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLocalIpAddress();
    }
    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.e("IP", inetAddress.getHostAddress().toString());
                                    return inetAddress.getHostAddress().toString();
                                }
                    }
            }
        } catch (SocketException ex) {
            Log.e( "Error:" , ex.toString());
          }
        return null ;
        }
}

And this is my Log

fe80::a3c8:4c03:154:8e1d%rmnet_usb0

And this is my AndroidManifest

 <uses-permission android:name= "android.permission.INTERNET" />  
    <uses-permission android:name= "android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" />
MingHong Zheng
  • 175
  • 4
  • 13

3 Answers3

1
Plz try this it may help you..?
 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()) {
                          return inetAddress.getHostAddress().toString();
                      }
                  }
              }
          } catch (Exception ex) {
              Log.e("IP Address", ex.toString());
          }
          return null;
      }

Add below permission in the manifest file.
   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Vishnu V S
  • 355
  • 1
  • 10
0

You can try this:

private String getIPAddress(Context context) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return Formatter.formatIpAddress(ip);
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
0
 Use this for getting device ip address:
 WifiManager wm = (WifiManager) this.getSystemService(this.WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Vishnu V S
  • 355
  • 1
  • 10