Java Code Examples for android.net.wifi.WifiInfo#getIpAddress()

The following examples show how to use android.net.wifi.WifiInfo#getIpAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WifiUtil.java    From Rumble with GNU General Public License v3.0 7 votes vote down vote up
public static String getIPAddress() {
    WifiInfo wifiInfo = getWifiManager().getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();

    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }

    byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
    
    try {
        return InetAddress.getByAddress(ipByteArray).getHostAddress();
    } catch (UnknownHostException ex) {
        return null;
    }
}
 
Example 2
Source File: Utils.java    From RPiCameraViewer with MIT License 6 votes vote down vote up
public static String getLocalIpAddress()
{
	String address = "";
	WifiManager manager = (WifiManager)App.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	if (manager.isWifiEnabled())
	{
		WifiInfo wifiInfo = manager.getConnectionInfo();
		if (wifiInfo != null)
		{
			NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
			if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
			{
				int ip = wifiInfo.getIpAddress();
				address = Formatter.formatIpAddress(ip);
			}
		}
	}
	return address;
}
 
Example 3
Source File: SignalInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
/**
 * wifi
 *
 * @param mContext
 * @return
 */
private static void getDetailsWifiInfo(Context mContext, SignalBean signalBean) {
    try {
        WifiInfo mWifiInfo = getWifiInfo(mContext);
        int ip = mWifiInfo.getIpAddress();
        String strIp = "" + (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + ((ip >> 24) & 0xFF);
        signalBean.setBssid(mWifiInfo.getBSSID());
        signalBean.setSsid(mWifiInfo.getSSID().replace("\"", ""));
        signalBean.setnIpAddress(strIp);
        signalBean.setMacAddress(getMac(mContext));
        signalBean.setNetworkId(mWifiInfo.getNetworkId());
        signalBean.setLinkSpeed(mWifiInfo.getLinkSpeed() + "Mbps");
        int rssi = mWifiInfo.getRssi();
        signalBean.setRssi(rssi);
        signalBean.setLevel(calculateSignalLevel(rssi));
        isWifiProxy(mContext, signalBean);
        signalBean.setSupplicantState(mWifiInfo.getSupplicantState().name());
        signalBean.setnIpAddressIpv6(getNetIP());
    } catch (Exception e) {
        Log.i(TAG, e.toString());
    }

}
 
Example 4
Source File: NetworkUtil.java    From DoingDaily with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前WIFI连接的ip地址
 *
 * @param context
 * @return
 */
public static String getLocalIpAddress(Context context) {
    try {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int i = wifiInfo.getIpAddress();
        return int2ip(i);
    } catch (Exception ex) {
        return " 获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n" + ex.getMessage();
    }
}
 
Example 5
Source File: DeviceInfo.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
/**
 * 获取IP地址
 *
 * @param context
 * @return
 */
public static int getIpAddress(Context context) {
    int ipAddress = 0;
    final WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null || wifiInfo.equals("")) {
        return ipAddress;
    } else {
        ipAddress = wifiInfo.getIpAddress();
    }
    return ipAddress;
}
 
Example 6
Source File: IpUtil.java    From BaseProject with MIT License 5 votes vote down vote up
/**
 * 获取IP地址(网络为Wifi)
 */
@RequiresPermission(allOf = {Manifest.permission.CHANGE_WIFI_STATE,
                             Manifest.permission.ACCESS_WIFI_STATE})
public static String getWifiIp(@NonNull Context context) {
    // 获取wifi服务
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    // 判断wifi是否开启
    if (null != wifiManager && !wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        return intToIp(ipAddress);
    }
    return null;
}
 
Example 7
Source File: EspNetUtil.java    From cordova-plugin-smartconfig with Apache License 2.0 5 votes vote down vote up
/**
 * get the local ip address by Android System
 * 
 * @param context
 *            the context
 * @return the local ip addr allocated by Ap
 */
public static InetAddress getLocalInetAddress(Context context) {
	WifiManager wm = (WifiManager) context
			.getSystemService(Context.WIFI_SERVICE);
	WifiInfo wifiInfo = wm.getConnectionInfo();
	int localAddrInt = wifiInfo.getIpAddress();
	String localAddrStr = __formatString(localAddrInt);
	InetAddress localInetAddr = null;
	try {
		localInetAddr = InetAddress.getByName(localAddrStr);
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
	return localInetAddr;
}
 
Example 8
Source File: IpUtil.java    From AndroidBasicProject with MIT License 5 votes vote down vote up
/**
 * 获取IP地址(网络为Wifi)
 */
@RequiresPermission(allOf = {
        Manifest.permission.CHANGE_WIFI_STATE,
        Manifest.permission.ACCESS_WIFI_STATE})
public static String getWifiIp(Context context) {
    // 获取wifi服务
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // 判断wifi是否开启
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    return intToIp(ipAddress);
}
 
Example 9
Source File: Misc.java    From KA27 with Apache License 2.0 5 votes vote down vote up
public static String getIpAddr(Context context) {
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();

    String ipString = String.format(Locale.US,
        "%d.%d.%d.%d",
        (ip & 0xff),
        (ip >> 8 & 0xff),
        (ip >> 16 & 0xff),
        (ip >> 24 & 0xff));

    return ipString;
}
 
Example 10
Source File: Tools.java    From letv with Apache License 2.0 5 votes vote down vote up
public static boolean isWiFiConnected(Context context) {
    WifiManager mWifiManager = (WifiManager) context.getSystemService("wifi");
    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
    if (!mWifiManager.isWifiEnabled() || ipAddress == 0) {
        return false;
    }
    return true;
}
 
Example 11
Source File: DataUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static String getWifiMacAddress(Context context) {
    WifiInfo info = ((WifiManager) context.getSystemService("wifi")).getConnectionInfo();
    String ipString = NetworkUtils.DELIMITER_LINE;
    if (info == null) {
        return ipString;
    }
    int ipAddress = info.getIpAddress();
    if (ipAddress > 0) {
        return (ipAddress & 255) + "." + ((ipAddress >> 8) & 255) + "." + ((ipAddress >> 16) & 255) + "." + ((ipAddress >> 24) & 255);
    }
    return ipString;
}
 
Example 12
Source File: MediaServer.java    From HPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 WIFI IP
 */
private InetAddress getWIFIIpAddress(Context context) throws UnknownHostException {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    return InetAddress.getByName(String.format("%d.%d.%d.%d",
            (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
            (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)));
}
 
Example 13
Source File: VMNetwork.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 获取本地 IP 地址
 *
 * @param context 上下文对象
 */
public static String getLocalIP(Context context) {
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    String localIP = String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
    return localIP;
}
 
Example 14
Source File: NetworkUtils.java    From fangzhuishushenqi with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前连接wifi的名称
 *
 * @return
 */
public static String getConnectWifiIp(Context context) {
    if (isWifiConnected(context)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        if (ipAddress == 0) {
            return null;
        }
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
                + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    }
    return null;
}
 
Example 15
Source File: SocketServerFragment.java    From ScreenCapture with MIT License 5 votes vote down vote up
private String getlocalip() {
    WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(
            Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    //  Log.d(Tag, "int ip "+ipAddress);
    if (ipAddress == 0) return null;

    Log.d("WOW", "Origin ip:" + ipAddress);
    return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
}
 
Example 16
Source File: EnvironmentUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 判断当前是否连接了WiFi网络
 * @return true-连接了WiFi网络
 */
public static boolean isWifiConnected(@NonNull Context context){
    try{
        WifiInfo wifiInfo=((WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE)).getConnectionInfo();
        return wifiInfo!=null&&wifiInfo.getIpAddress()!=0;
    }catch (Exception e){e.printStackTrace();}
    return false;
}
 
Example 17
Source File: RemoteControllerActivity.java    From screenstandby with GNU General Public License v2.0 5 votes vote down vote up
private void getAssignIPAddressToTextbox()
{
	if (wifi == null) wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
	WifiInfo info = wifi.getConnectionInfo();
	int ip = info.getIpAddress();
	if (ip > 0)
	{
    	if (ip1 != null) ip1.setText((ip & 0xFF) + "");
    	if (ip2 != null) ip2.setText(((ip >> 8) & 0xFF) + "");
    	if (ip3 != null) ip3.setText(((ip >> 16) & 0xFF) + "");
    	if (ip4 != null) ip4.setText("");	
	}
	else
	{
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                if (intf.getName().contains("wlan"))
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    	InetAddress addr = enumIpAddr.nextElement();
                    	if (Inet4Address.class.isInstance(addr))
                    	{
				byte[] b = addr.getAddress();
				if (ip1 != null) ip1.setText((b[0] & 0xFF) + "");
				if (ip2 != null) ip2.setText((b[1] & 0xFF) + "");
				if (ip3 != null) ip3.setText((b[2] & 0xFF) + "");
				break;
                    	}
                    }
            }
        } catch (Exception ex) {
            Logger.Log(getBaseContext(), ex);
        }
	}
	if (ipPort != null) ipPort.setText(Core.SERVICE_PORT+"");
}
 
Example 18
Source File: NetworkUtils.java    From android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get ip address of the Wifi service
 *
 * @return IP
 */
public static String getWifiIPAddress() {
    WifiManager wifiMgr = (WifiManager) VPNhtApplication.getAppContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
}
 
Example 19
Source File: Utils.java    From Fairy with Apache License 2.0 4 votes vote down vote up
public static String getIpAddress() {
    WifiManager wifiManager = (WifiManager) App.getInstance().getApplicationContext().getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    return (ipAddress & 0xff) + "." + (ipAddress>>8 & 0xff) + "." + (ipAddress>>16 & 0xff) + "." + (ipAddress >> 24 & 0xff);
}
 
Example 20
Source File: NetworkReceiver.java    From Virtual-Hosts with GNU General Public License v3.0 4 votes vote down vote up
private String getWifiIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int int_ip = wifiInfo.getIpAddress();
    return intToIp(int_ip);
}