Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址。
//查看Wifi地址publicString getWifiIpAddress() { //获取wifi服务WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); //判断wifi是否开启if(!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); intipAddress = wifiInfo.getIpAddress(); String ip = intToIp(ipAddress); Log.d(wuc, wifiIp =+ ip); returnip; } //获取GPRS本地ip地址publicString getLocalIpAddress() { try{ for(Enumerationen = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for(EnumerationenumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if(!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) { Log.d(wuc, gprs =+ inetAddress.getHostAddress().toString()); returninetAddress.getHostAddress().toString(); }
}
}
} catch(SocketException ex) {
Log.e(WifiPreference IpAddress, ex.toString());
}
return null;
}
privateString intToIp(inti) {
return(i & 0xFF ) + .+
((i >> 8 ) & 0xFF) + .+
((i >> 16 ) & 0xFF) + .+
( i >> 24 & 0xFF) ;
}
日志信息
09-10 14:58:06.288 12190-12190/com.example.administrator.newtext D/wuc: gprs = 10.96.76.60
09-10 14:58:15.488 12190-12190/com.example.administrator.newtext D/wuc: wifiIp = 192.168.1.103
修改wifi地址网上找的-------------------------
@Override public voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
wifiManager= (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo connectionInfo = wifiManager.getConnectionInfo(); ListconfiguredNetworks = wifiManager.getConfiguredNetworks(); for(WifiConfiguration conf : configuredNetworks) { if(conf.networkId== connectionInfo.getNetworkId()) { wifiConf= conf; Toast.makeText(this, 11111, Toast.LENGTH_SHORT).show(); break; } } Log.d(wuc, wifiConfout =+ wifiConf); try{ setIpAssignment(STATIC, wifiConf); setIpAddress(InetAddress.getByName(192.168.0.110), 24, wifiConf); setGateway(InetAddress.getByName(255.255.255.0), wifiConf); setDNS(InetAddress.getByName(255.255.255.0), wifiConf); } catch(Exception e) { //TODO Auto-generated catch blocke.printStackTrace(); } Toast.makeText(this, 1121, Toast.LENGTH_SHORT).show(); wifiManager.updateNetwork(wifiConf); // apply the setting
public static voidsetIpAssignment(String assign, WifiConfiguration wifiConf) throwsSecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { Log.d(wuc, wifiConfin =+ wifiConf); setEnumField(wifiConf, assign, ipAssignment); } public static voidsetIpAddress(InetAddress addr, intprefixLength, WifiConfiguration wifiConf) throwsSecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException { Object linkProperties = getField(wifiConf, linkProperties); if(linkProperties == null) return; Class laClass = Class.forName(android.net.LinkAddress); Constructor laConstructor = laClass.getConstructor(newClass[]{ InetAddress.class, int.class}); Object linkAddress = laConstructor.newInstance(addr, prefixLength); ArrayList mLinkAddresses = (ArrayList) getDeclaredField(linkProperties, mLinkAddresses); mLinkAddresses.clear(); mLinkAddresses.add(linkAddress); } public static voidsetGateway(InetAddress gateway, WifiConfiguration wifiConf) throwsSecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException { Object linkProperties = getField(wifiConf, linkProperties); if(linkProperties == null) return; Class routeInfoClass = Class.forName(android.net.RouteInfo); Constructor routeInfoConstructor = routeInfoClass .getConstructor(newClass[]{InetAddress.class}); Object routeInfo = routeInfoConstructor.newInstance(gateway); ArrayList mRoutes = (ArrayList) getDeclaredField(linkProperties, mRoutes); mRoutes.clear(); mRoutes.add(routeInfo); } public static voidsetDNS(InetAddress dns, WifiConfiguration wifiConf) throwsSecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { Object linkProperties = getField(wifiConf, linkProperties); if(linkProperties == null) return; ArrayListmDnses = (ArrayList) getDeclaredField( linkProperties, mDnses); mDnses.clear(); // or add a new dns address , here I just want to// replace DNS1mDnses.add(dns); } public staticObject getField(Object obj, String name) throwsSecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getField(name); Object out = f.get(obj); returnout; } public staticObject getDeclaredField(Object obj, String name) throwsSecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getDeclaredField(name); f.setAccessible(true); Object out = f.get(obj); returnout; } public static voidsetEnumField(Object obj, String value, String name) throwsSecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Log.d(wuc, wifiConfinin =+ obj); Field f = obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class) f.getType(), value)); }
}
但是这个wifi修改地址没成功。
提示:java.lang.NoSuchFieldException: ipAssignment
难道是ipAssignment名称不对?
看网上说getField换成getDeclaredField
提示:java.lang.NoSuchFieldException: No field ipAssignment in class Landroid/net/wifi/WifiConfiguration; (declaration of 'android.net.wifi.WifiConfiguration' appears in /system/framework/framework.jar)