前段时间做了个厂家测试程序,中间有涉及到网口的检测。基于android4.0的以太网DHCP或者设置静态Ip来实现网络连接,主要实现过程如下:
private void registerEthernet() { IntentFilter ethFilter = new IntentFilter(EthernetManager.ETHERNET_STATE_CHANGED_ACTION); ethFilter.addAction(EthernetManager.NETWORK_STATE_CHANGED_ACTION); registerReceiver(mEthConnectReceiver, ethFilter); } private BroadcastReceiver mEthConnectReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG, " Ethernet onRecevice action = " + action); if (action.equals(EthernetManager.ETHERNET_STATE_CHANGED_ACTION)) { int msg = intent.getIntExtra(EthernetManager.EXTRA_ETHERNET_STATE, -1); switch (msg) { case EthernetDataTracker.EVENT_DHCP_CONNECT_FAILED: break; case EthernetDataTracker.EVENT_DHCP_DISCONNECT_FAILED: break; case EthernetDataTracker.EVENT_STATIC_CONNECT_FAILED: break; case EthernetDataTracker.EVENT_STATIC_DISCONNECT_FAILED: break; case EthernetDataTracker.EVENT_DHCP_CONNECT_SUCCESSED: pingEthGateway(getEthGateWay());//ping命令DHCP连接网络是否成功 break; case EthernetDataTracker.EVENT_DHCP_DISCONNECT_SUCCESSED: break; case EthernetDataTracker.EVENT_STATIC_CONNECT_SUCCESSED: pingEthGateway(getEthGateWay());//ping命令测试静态IP连接网络是否成功 break; case EthernetDataTracker.EVENT_STATIC_DISCONNECT_SUCCESSED: break; case EthernetDataTracker.EVENT_PHY_LINK_UP: setDhcp();//网线插上后自己进行DHCP break; case EthernetDataTracker.EVENT_PHY_LINK_DOWN: Log.d(TAG, "EVENT_PHY_LINK_DOWN ");//网线拔出 break; default: break; } } } };
private String dhcp_gateway = null; private String wifi_gateway = null; private EthernetManager mEthManager = null; private WifiManager mWifiManager = null; private ConnectivityManager mConnectivityManager = null; public void getInstance(Context context) { mEthManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); } public void setDhcp() { mWifiManager.setWifiEnabled(false);//断开WIFI的连接 mEthManager.setEthernetEnabled(false); Log.i(TAG, "getEthernetState=" + mEthManager.getEthernetState()); mEthManager.setEthernetDefaultConf(); mEthManager.setInterfaceName("eth0"); mEthManager.setEthernetEnabled(true); Log.i(TAG, "getEthernetState=" + mEthManager.getEthernetState()); }
public String getEthGateWay() { String ip = mConnectivityManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET) .getAddresses().toString(); String mGW = mConnectivityManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET) .getRoutes().toString(); String mDns = mConnectivityManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET) .getDnses().toString(); Log.i(TAG, "getEthGateWay ip=" + ip); Log.i(TAG, "getEthGateWay mGW=" + mGW); Log.i(TAG, "getEthGateWay mDns=" + mDns); if (mGW.contains(">")) { mGW = mGW.substring(mGW.lastIndexOf('>') + 2, mGW.length() - 1); } dhcp_gateway = mGW; return mGW; }
其中获取的Ip信息为:
getEthGateWay ip=[/192.168.1.102]
getEthGateWay mGW=[0.0.0.0/0 -> 192.168.1.1]
getEthGateWay mDns=[/192.168.10.247, /192.168.1.1]
因此需要对网关信息进行处理。
4.Ping命令测试IP地址:
使用Root权限,通过JNI封装成一个系统库,调用exe command进行ping -c 4 -w 100 192.168.1.1测试。代码将不详细贴出。
执行结果:
I/SystemTestingJNI( 1836): executeCommand ret=0, result=PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
I/SystemTestingJNI( 1836): 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=6.54 ms
I/SystemTestingJNI( 1836): 64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.316 ms
I/SystemTestingJNI( 1836): 64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.194 ms
I/SystemTestingJNI( 1836): 64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.255 ms
I/SystemTestingJNI( 1836):
I/SystemTestingJNI( 1836): --- 192.168.1.1 ping statistics ---
I/SystemTestingJNI( 1836): 4 packets transmitted, 4 received, 0% packet loss, time 3001ms
I/SystemTestingJNI( 1836): rtt min/avg/max/mdev = 0.194/1.826/6.542/2.723 ms
--还有一种方法,使用Runtime去ping,但是需要注意权限问题,以及输入输出流得自己处理。 exec执行时阻塞、非阻塞,返回结果问题
Process p= Runtime.getRuntime().exec(cmd);//String cmd
-------------------------------------------------------------------------------------------------------------------
public static void setStaticIp() { mWifiManager.setWifiEnabled(false); mEthManager.setEthernetEnabled(false); DhcpInfoInternal mDhcpInfoInternal = new DhcpInfoInternal(); InetAddress iRoute = NetworkUtils.numericToInetAddress("192.168.1.103"); InetAddress iNetmask = NetworkUtils.numericToInetAddress("255.255.255.0"); try { int netmask = NetworkUtils.inetAddressToInt(iNetmask); int prefixLength = NetworkUtils.netmaskIntToPrefixLength(netmask); mDhcpInfoInternal.prefixLength = prefixLength; } catch (IllegalArgumentException e) { e.printStackTrace(); } mDhcpInfoInternal.ipAddress = "192.168.1.103"; mDhcpInfoInternal.addRoute(new RouteInfo(iRoute)); mDhcpInfoInternal.dns1 = "192.168.10.248"; mDhcpInfoInternal.serverAddress = "255.255.255.0"; mEthManager.saveEthernetIpInfo(mDhcpInfoInternal.makeDhcpInfo(), EthernetManager.ETHERNET_CONNECT_MODE_MANUAL); mEthManager.setEthernetEnabled(true); DhcpInfo dhcpInfo = mEthManager.getSavedEthernetIpInfo(); dhcp_gateway = Formatter.formatIpAddress(dhcpInfo.gateway); }
----通过网口实现网络连接实现,就此Ending。。------
DHCP详细代码:http://www.oschina.net/code/snippet_1051613_26563
配置工程中需要引用framework.jar,services.jar。
配置方法:
取到out\target\common\obj\JAVA_LIBRARIES\framework_intermediates\class.jar,然后放到本地,右击工程进入java build path ,通过add Library--user library ---添加刚取到的class.jar,取名为framewrok.最后调整Java build path 中的order and Export 将framework一直up到第一个。
取到out\target\common\obj\JAVA_LIBRARIES\services_intermediates\class.jar。方法同framework.
环境配置好后,重新Build。就不会有红叉叉了!!
WIFI文章:WIFI,热点扫描,配对,以及IP的获取。
http://my.oschina.net/hiliusl/blog/175605