get Local Ip address in Int value - Android java.net

Android examples for java.net:IP Address

Description

get Local Ip address in Int value

Demo Code

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class Main {

  public static byte[] getLocalIpInt(Context context) {
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (!wm.isWifiEnabled()) {
      return null;
    }//  w  w w .  j  a v  a  2  s. c  om

    WifiInfo wi = wm.getConnectionInfo();
    return intToBytes(wi.getIpAddress());
  }

  private static byte[] intToBytes(int i) {
    byte[] ip = new byte[4];
    ip[0] = (byte) (i & 0xFF);
    ip[1] = (byte) ((i >> 8) & 0xFF);
    ip[2] = (byte) ((i >> 16) & 0xFF);
    ip[3] = (byte) ((i >> 24) & 0xFF);
    return ip;
  }

}

Related Tutorials