前段時間作了個廠家測試程序,中間有涉及到網口的檢測。基於android4.0的以太網DHCP或者設置靜態Ip來實現網絡鏈接,主要實現過程以下: java
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信息爲: android
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] 網絡
所以須要對網關信息進行處理。 ide
4.Ping命令測試IP地址: 測試
使用Root權限,經過JNI封裝成一個系統庫,調用exe command進行ping -c 4 -w 100 192.168.1.1測試。代碼將不詳細貼出。 ui
執行結果: spa
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 .net
--還有一種方法,使用Runtime去ping,可是須要注意權限問題,以及輸入輸出流得本身處理。 exec執行時阻塞、非阻塞,返回結果問題 code
Process p= Runtime.getRuntime().exec(cmd);//String cmd orm
-------------------------------------------------------------------------------------------------------------------
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的獲取。