赞
踩
Android11
上WiFi开发前面文章已经做了介绍,本篇将记录以太网的开发过程,以太网的功能需求相对简单。获取网络信息、设置静态和动态IP
。
相关文章
1、【 Android11 WiFi开发 一 】WiFi列表获取与展示
2、【 Android11 WiFi开发 二 】WiFi连接、断开
3、【 Android11 WiFi开发 三 】WiFi信息获取与静态IP设置
和无线网络中有管理类WifiManager
一样,以太网也提供了管理类EthernetManager
,获取开关状态、获取以太网络信息、设置动态IP、静态IP
获取都通过它,在标准的Android
接口中,EthernetManager
是隐藏的,所以需要Framework
放开,下面就了解相关的API
和方法。
1、获取以太网管理类、连接管理类
通过系统服务来获取EthernetManager
,在WiFi开发的文章中提到过,连接管理类ConnectivityManager
用于获取WiFi的信息。IP
地址状态配置使用IpConfiguration
,同样适用于以太网。
val ethernetManager = App.context.getSystemService(EthernetManager::class.java) as EthernetManager
val connectivityManager = App.context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager
val ipConfiguration = ethernetManager.getConfiguration("eth0")
2、获取以太网是DHCP状态
/**
* 是否是DHCP
*/
fun isDHCP(): Boolean {
val assignment = ipConfiguration?.getIpAssignment()
return assignment == IpConfiguration.IpAssignment.DHCP || assignment == IpConfiguration.IpAssignment.UNASSIGNED
}
3、获取以太网IP地址
/**
* 获取以太网ip地址
*/
fun getIPAddress(): String? {
val linkProperties = connectivityManager!!.getLinkProperties(ConnectivityManager.TYPE_ETHERNET)
linkProperties?.linkAddresses?.forEach {
if (it.address is Inet4Address) {
return it.address.hostAddress
}
}
return "0.0.0.0"
}
3、获取以太网IP地址
/** * 获取以太网-子网掩码 */ fun getSubnetMask(): String { val networkInfo = connectivityManager?.activeNetworkInfo if (networkInfo != null && networkInfo.type == ConnectivityManager.TYPE_ETHERNET) { return intToIp(NetworkUtils.prefixLengthToNetmaskInt(getNetworkPrefixLength())) } return "0.0.0.0" } fun intToIp(int paramInt):String { return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "." + (0xFF & paramInt >> 24); } private fun getNetworkPrefixLength(): Int { var prefixLength = 0 val linkProperties = connectivityManager!!.getLinkProperties(ConnectivityManager.TYPE_ETHERNET) if(linkProperties != null){ for (linkAddress in linkProperties.linkAddresses) { val address: InetAddress = linkAddress.address if (address is Inet4Address) { prefixLength = linkAddress.prefixLength break } } } return prefixLength }
4、获取DNS地址
/** * 获取DNS Server */ fun getLocalDNS(): String? { val linkProperties = connectivityManager!!.getLinkProperties(ConnectivityManager.TYPE_ETHERNET) if( linkProperties != null){ for (dnsServer in linkProperties.dnsServers) { if (dnsServer is Inet4Address) { return dnsServer.hostAddress } } } return "0.0.0.0" }
5、获取默认网关
/** * 获取默认网关 */ fun getGateway(): String { val linkProperties = connectivityManager!!.getLinkProperties(ConnectivityManager.TYPE_ETHERNET) if(linkProperties != null){ var gatewayTemp = linkProperties.routes.toString() var gateway = gatewayTemp if (gatewayTemp.contains(">")) { gatewayTemp = gatewayTemp.substring(gatewayTemp.lastIndexOf('>') + 2, gatewayTemp.length - 1) gatewayTemp = gatewayTemp.substring(0, gatewayTemp.length - 5) } gatewayTemp = gatewayTemp.replace(" eth0", "").trim() if(gatewayTemp == "::"){ gateway = gateway.substring(0,gateway.lastIndexOf(">")) gateway = gateway.substring(gateway.lastIndexOf(">") + 2,gateway.length - 1) gatewayTemp = gateway.substring(0,gateway.lastIndexOf(" eth0")) } return if (gatewayTemp.contains("[]")) "" else gatewayTemp } return "0.0.0.0" }
上述方法通过ConnectivityManager
获取以太网信息,和无线网络类似,类型传ConnectivityManager.TYPE_ETHERNET
即可,下面看下设置有线网络DHCP
状态。
6、设置动态IP地址
/**
* 设置动态获取IP
*/
fun setWiredWithDhcp(){
ipConfiguration!!.setIpAssignment(IpConfiguration.IpAssignment.DHCP)
ethernetManager.setConfiguration("eth0",ipConfiguration)
}
7、设置静态IP地址
/** * 有线网络-静态ip设置 */ fun setWiredWithStaticIp(dhcp:Boolean,ip: String, mask: String, gateway: String, dns: String):Boolean{ val staticConfig = StaticIpConfiguration() val inetAddress: InetAddress? if(!TextUtils.isEmpty(ip) && ip != "0.0.0.0"){ try { inetAddress = NetworkUtils.numericToInetAddress(ip) } catch (e: Exception) { if(App.DEBUG) Log.i(TAG, "e = ${e.printStackTrace()}") return false } }else{ return false } if (!TextUtils.isEmpty(mask) && mask != "0.0.0.0") { val networkPrefixLength = parsePrefixLength(mask) try { staticConfig.ipAddress = LinkAddress(inetAddress, networkPrefixLength) } catch (e: Exception) { if(App.DEBUG) Log.i(TAG, "e = ${e.printStackTrace()}") return false } }else{ return false } if (!TextUtils.isEmpty(gateway) && gateway != "0.0.0.0") { try { staticConfig.gateway = NetworkUtils.numericToInetAddress(gateway) as Inet4Address } catch (e: Exception) { if(App.DEBUG) Log.i(TAG, "e = ${e.printStackTrace()}") return false } }else{ return false } if (!TextUtils.isEmpty(dns) && dns != "0.0.0.0") { try { staticConfig.dnsServers.add(NetworkUtils.numericToInetAddress(dns) as Inet4Address) } catch (e: Exception) { if(App.DEBUG) Log.i(TAG, "e = ${e.printStackTrace()}") return false } }else{ return false } if (!TextUtils.isEmpty(dns) && dns != "0.0.0.0") { try { staticConfig.dnsServers.add(NetworkUtils.numericToInetAddress(dns) as Inet4Address) } catch (e: Exception) { if(App.DEBUG) Log.i(TAG, "e = ${e.printStackTrace()}") return false } }else{ return false } ipConfiguration!!.setStaticIpConfiguration(staticConfig) ipConfiguration!!.setIpAssignment(if(dhcp) IpConfiguration.IpAssignment.DHCP else IpConfiguration.IpAssignment.STATIC) try { ethernetManager.setConfiguration("eth0", ipConfiguration) } catch (e: Exception) { if(App.DEBUG) Log.i(TAG, "e = ${e.printStackTrace()}") return false } return true } public static int parsePrefixLength(String netmask) { int prefixLength = 0; int maskNum = 0x00; String[] mask = parseDetailToArray(netmask); if (mask.length < 4) return prefixLength; for (int i = 0; i < 4; i++) { maskNum |= Integer.parseInt(mask[3 - i]) << (i * 8); } if (maskNum == 0xffffffff) return 32; if (maskNum == 0xffffff00) return 24; if (maskNum == 0xffff0000) return 16; if (maskNum == 0xff000000) return 8; for (int i = 0; i < 32; i++) { if (((maskNum << i) & 0x80000000) == 0x80000000) prefixLength++; else break; } return prefixLength; } private static String[] parseDetailToArray(String address) { String[] result = address.split("\\."); if (result.length < 4) { result = new String[]{"", "", "", ""}; } return result; }
设置静态IP
的操作和无线网络一样,只是这里通过EthernetManager
来设置。至此,以太网络的相关操作介绍完毕。
完结!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。