android13 系统默认设置静态IP
android11系统的时候,默认静态IP设置很简单,修改frameworks\base\core\res\res\values\config.xml中的config_ethernet_interfaces字符数组,在里面添加静态IP的参数就可以了。
<string-array translatable="false" name="config_ethernet_interfaces">
<!--
<item>eth1;12,13,14,15;ip=192.168.0.10/24 gateway=192.168.0.1 dns=4.4.4.4,8.8.8.8</item>
<item>eth2;;ip=192.168.0.11/24</item>
<item>eth3;12,13,14,15;ip=192.168.0.12/24;1</item>
-->
</string-array>
android13以后,这个参数移动到了packages\modules\Connectivity\service\ServiceConnectivityResources\res\values\config.xml下面,修改以后以太网是能够默认静态IP,但是发现以太网上不了网了,设置动态IP也不行。很是奇怪。后面只能通过另外一种方式去设置,即开机启动的时候去设置一次静态IP。
具体使用如下几个函数:
public void initEthIp(Context context){
boolean issetIP = SystemProperties.getInt("persist.sys.eth_static_ip_flag", 0) == 1 ? true:false;
if(issetIP){
return;
}
if(setEthIPAddress(context, "192.168.1.2", "255.255.255.0", "192.168.1.200", "192.168.1.200","8.8.8.8")){
SystemProperties.set("persist.sys.eth_static_ip_flag", "1");
Log.d(TAG, "set static eth ip ok!");
}else{
Log.d(TAG, "set static eth ip failed!");
}
}
public boolean setEthIPAddress(Context context, String IP, String Mask, String Gateway, String DNS, String DNS2){
String Interface = "eth0";
EthernetManager mEthManager;
mEthManager = (EthernetManager) (EthernetManager)context.getSystemService(Context.ETHERNET_SERVICE);
IpConfiguration.IpAssignment mIpAssignment = IpConfiguration.IpAssignment.DHCP;
if (mEthManager != null) {
if (isIpAddress(IP) && isIpAddress(Mask) && isIpAddress(Gateway)
&& isIpAddress(DNS)) {
StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();
if (validateIpConfigFields(mStaticIpConfiguration, IP, Mask,
Gateway, DNS, DNS2) != 0) {
return false;
} else {
mEthManager.setEthernetEnabled(false);
Inet4Address inetAddr = getIPv4Address(IP);
int prefixLength = maskStr2InetMask(Mask);
InetAddress gatewayAddr = getIPv4Address(Gateway);
InetAddress dnsAddr = getIPv4Address(DNS);
if (inetAddr.getAddress().toString().isEmpty() || prefixLength == 0 || gatewayAddr.toString().isEmpty()
|| dnsAddr.toString().isEmpty()) {
return false;
}
ArrayList<InetAddress> dnsServers = new ArrayList<>();
dnsServers.add(dnsAddr);
if (!DNS2.isEmpty()) {
dnsServers.add(getIPv4Address(DNS2));
}
final StaticIpConfiguration.Builder staticIPBuilder = new StaticIpConfiguration.Builder()
.setDnsServers(dnsServers)
.setDomains(mStaticIpConfiguration.getDomains())
.setGateway(gatewayAddr)
.setIpAddress(new LinkAddress(inetAddr, prefixLength));
mStaticIpConfiguration = staticIPBuilder.build();
IpConfiguration ipConfiguration = new IpConfiguration();
ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.STATIC);
ipConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE);
ipConfiguration.setStaticIpConfiguration(mStaticIpConfiguration);
mEthManager.setConfiguration(Interface, ipConfiguration);
mEthManager.setEthernetEnabled(true);
return true;
}
} else {
return false;
}
}
return false;
}
private Inet4Address getIPv4Address(String text) {
try {
return (Inet4Address) InetAddresses.parseNumericAddress(text);
} catch (IllegalArgumentException | ClassCastException e) {
return null;
}
}
public int maskStr2InetMask(String maskStr) {
StringBuffer sb;
String str;
int inetmask = 0;
int count = 0;
/*
* check the subMask format
*/
Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$");
if (pattern.matcher(maskStr).matches() == false) {
Log.e(TAG, "subMask is error");
return 0;
}
String[] ipSegment = maskStr.split("\\.");
for (int n = 0; n < ipSegment.length; n++) {
sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));
str = sb.reverse().toString();
count = 0;
for (int i = 0; i < str.length(); i++) {
i = str.indexOf("1", i);
if (i == -1)
break;
count++;
}
inetmask += count;
}
return inetmask;
}
private boolean isIpAddress(String value) {
int start = 0;
int end = value.indexOf('.');
int numBlocks = 0;
while (start < value.length()) {
if (end == -1) {
end = value.length();
}
try {
int block = Integer.parseInt(value.substring(start, end));
if ((block > 255) || (block < 0)) {
return false;
}
} catch (NumberFormatException e) {
return false;
}
numBlocks++;
start = end + 1;
end = value.indexOf('.', start);
}
return numBlocks == 4;
}
private int validateIpConfigFields(
StaticIpConfiguration staticIpConfiguration, String ipAddr,
String netmask, String gateway, String dns, String dns2) {
final StaticIpConfiguration.Builder staticIPBuilder = new StaticIpConfiguration.Builder()
.setDnsServers(staticIpConfiguration.getDnsServers())
.setDomains(staticIpConfiguration.getDomains())
.setGateway(staticIpConfiguration.getGateway())
.setIpAddress(staticIpConfiguration.getIpAddress());
Inet4Address inetAddr = getIPv4Address(ipAddr);
if (inetAddr == null) {
return 2;
}
int networkPrefixLength = -1;
networkPrefixLength = maskStr2InetMask(netmask);
if(networkPrefixLength == 0){
return 3;
}
InetAddress gatewayAddr = getIPv4Address(gateway);
if (gatewayAddr == null) {
return 4;
}
staticIPBuilder.setGateway(gatewayAddr);
ArrayList<InetAddress> dnsServers = new ArrayList<>();
InetAddress dnsAddr = null;
dnsAddr = getIPv4Address(dns);
if (dnsAddr == null) {
return 5;
}
dnsServers.add(dnsAddr);
InetAddress dnsAddr2 = null;
dnsAddr2 = getIPv4Address(dns2);
if (dnsAddr2 == null) {
return 6;
}
dnsServers.add(dnsAddr2);
staticIpConfiguration = staticIPBuilder.build();
return 0;
}
每次开机的时候可以在系统应用中接收开机广播。然后调用initEthIp函数,去检测是否已经设置过静态IP,如果设置过了就用一个系统属性保存标志位,下次再开机的时候判断标志位是否再设置静态IP。