Android获取WIFI网关地址与子网掩码地址

WifiManager wm = (WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo di = wm.getDhcpInfo();
long getewayIpL=di.gateway;
String getwayIpS=long2ip(getewayIpL);//网关地址
long netmaskIpL=di.netmask;
String netmaskIpS=long2ip(netmaskIpL);//子网掩码地址

String long2ip(long ip){
	StringBuffer sb=new StringBuffer();
	sb.append(String.valueOf((int)(ip&0xff)));
	sb.append('.');
	sb.append(String.valueOf((int)((ip>>8)&0xff)));
	sb.append('.');
	sb.append(String.valueOf((int)((ip>>16)&0xff)));
	sb.append('.');
	sb.append(String.valueOf((int)((ip>>24)&0xff)));
	return sb.toString();
}

//注意添加权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

你可能感兴趣的:(Android获取WIFI网关地址与子网掩码地址)