2022年12月21日

Android IP套件

 



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

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPUtils {

public static String getIp(Context mContext) {
String ip = "";
try {
//获取wifi服务
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
String dhcpInfos = intToIp(dhcpInfo.netmask);
String[] split = intToIp(ipAddress).split("\\.");
ip = split[0] + "." + split[1] + "." + split[2] + "." + (255 - Integer.parseInt(dhcpInfos.split("\\.")[3]));//根据子网掩码获取广播的IP地址
} else {
String asd = getInfo();
String[] split = asd.split(",");
String ipStr = split[0];
String NetMask = split[1];
String[] split1 = ipStr.split("\\.");
ip = split1[0] + "." + split1[1] + "." + split1[2] + "." + (255 - Integer.parseInt(NetMask.split("\\.")[3]));//根据子网掩码获取广播的IP地址
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}


private static String intToIp(int paramInt) {
return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
+ (0xFF & paramInt >> 24);
}

public static String getHostIp() {
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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (Exception ex) {
}
return "0.0.0.0";

}

public static String getInfo() throws SocketException {
String ipAddress = "";
String maskAddress = "";

for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
List<InterfaceAddress> mList = intf.getInterfaceAddresses();
for (InterfaceAddress l : mList) {
InetAddress inetAddress = l.getAddress();
if (!inetAddress.isLoopbackAddress()) {
String hostAddress = inetAddress.getHostAddress();
if (hostAddress.indexOf(":") > 0) {
continue;
} else {
ipAddress = hostAddress;
maskAddress = calcMaskByPrefixLength(l.getNetworkPrefixLength());
}
}
}
}
return ipAddress + "," + maskAddress;
}


private static String calcMaskByPrefixLength(int length) {
int mask = -1 << (32 - length);
int partsNum = 4;
int bitsOfPart = 8;
int maskParts[] = new int[partsNum];
int selector = 0x000000ff;

for (int i = 0; i < maskParts.length; i++) {
int pos = maskParts.length - 1 - i;
maskParts[pos] = (mask >> (i * bitsOfPart)) & selector;
}

String result = "";
result = result + maskParts[0];
for (int i = 1; i < maskParts.length; i++) {
result = result + "." + maskParts[i];
}
return result;
}

/**
* 判断是否为合法IP
**/
public static boolean isCorrectIp(String ipAddress) {
String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}

public static List<String> getConnectedIP() {
List<String> connectedIP = new ArrayList();
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
if (isCorrectIp(ip)) {
connectedIP.add(ip);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return connectedIP;

}
}

沒有留言:

張貼留言