android Wifi 設置靜態ip地址的方法

調用setIpWithTfiStaticIp()即可爲連接好的wifi配置 靜態Ip。支持Android4.0以上及以下的版本。(PS:以下的函數使用條件是:wifi是連接好的)

測試成功的

  /**

     * 設置靜態ip地址的方法
     */
    private boolean setIpWithTfiStaticIp() {  
    WifiManager wifiManager = (WifiManager) FactoryTestApp.context.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration wifiConfig = null;
WifiInfo connectionInfo = wifiManager.getConnectionInfo();  //得到連接的wifi網絡

List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == connectionInfo.getNetworkId()) {
wifiConfig = conf;
break;
}
}


if (android.os.Build.VERSION.SDK_INT < 11) { // 如果是android2.x版本的話


ContentResolver ctRes = FactoryTestApp.context.getContentResolver();
android.provider.Settings.System.putInt(ctRes, 
android.provider.Settings.System.WIFI_USE_STATIC_IP, 1);
android.provider.Settings.System.putString(ctRes, 
android.provider.Settings.System.WIFI_STATIC_IP,"192.168.0.202");
// android.provider.Settings.System.putString(ctRes,
// android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
// android.provider.Settings.System.putString(ctRes,
// android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.1");
// android.provider.Settings.System.putString(ctRes, 
// android.provider.Settings.System.WIFI_STATIC_DNS1,"192.168.0.1");
// android.provider.Settings.System.putString(ctRes, 
// android.provider.Settings.System.WIFI_STATIC_DNS2,"61.134.1.9");


return true;
} else { // 如果是android3.x版本及以上的話
try {
setIpAssignment("STATIC", wifiConfig);
setIpAddress(InetAddress.getByName("192.168.3.102"), 24, wifiConfig);
// setGateway(InetAddress.getByName("192.168.3.1"), wifiConfig);
// setDNS(InetAddress.getByName("192.168.0.1"), wifiConfig);
wifiManager.updateNetwork(wifiConfig); // apply the setting
System.out.println("靜態ip設置成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("靜態ip設置失敗!");
return false;
}
}
    }
    
    private static void setIpAssignment(String assign, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
setEnumField(wifiConf, assign, "ipAssignment");
}


private static void setIpAddress(InetAddress addr, int prefixLength,
WifiConfiguration wifiConf) throws SecurityException,
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(new Class[] {
InetAddress.class, int.class });
Object linkAddress = laConstructor.newInstance(addr, prefixLength);


ArrayList<Object> mLinkAddresses = (ArrayList<Object>) getDeclaredField(
linkProperties, "mLinkAddresses");
mLinkAddresses.clear();
mLinkAddresses.add(linkAddress);
}

private static Object getField(Object obj, String name)
throws SecurityException, NoSuchFieldException,IllegalArgumentException, IllegalAccessException {
Field f = obj.getClass().getField(name);
Object out = f.get(obj);
return out;
}
private static Object getDeclaredField(Object obj, String name)
throws SecurityException, NoSuchFieldException,IllegalArgumentException, IllegalAccessException {
Field f = obj.getClass().getDeclaredField(name);
f.setAccessible(true);
Object out = f.get(obj);
return out;
}
@SuppressWarnings({"unchecked", "rawtypes" })
private static void setEnumField(Object obj, String value, String name)
throws SecurityException, NoSuchFieldException,IllegalArgumentException, IllegalAccessException {
Field f = obj.getClass().getField(name);
f.set(obj, Enum.valueOf((Class<Enum>)f.getType(), value));
}


// private static void setGateway(InetAddress gateway,WifiConfiguration wifiConf) 
// throws SecurityException,
// IllegalArgumentException, NoSuchFieldException,
// IllegalAccessException, ClassNotFoundException,
// NoSuchMethodException, InstantiationException,
// InvocationTargetException {
// Object linkProperties = getField(wifiConf, "linkProperties");
// if (linkProperties == null)
// return;
//
// if (android.os.Build.VERSION.SDK_INT >= 14) { // android4.x版本
// Class<?> routeInfoClass = Class.forName("android.net.RouteInfo");
// Constructor<?> routeInfoConstructor = routeInfoClass
// .getConstructor(new Class[] { InetAddress.class });
// Object routeInfo = routeInfoConstructor.newInstance(gateway);
//
// ArrayList<Object> mRoutes = (ArrayList<Object>)getDeclaredField(
// linkProperties, "mRoutes");
// mRoutes.clear();
// mRoutes.add(routeInfo);
// } else { // android3.x版本
// ArrayList<InetAddress> mGateways = (ArrayList<InetAddress>) getDeclaredField(
// linkProperties, "mGateways");
// mGateways.clear();
// mGateways.add(gateway);
// }
// }
//    
//    private static void setDNS(InetAddress dns, WifiConfiguration wifiConf)
//     throws SecurityException, IllegalArgumentException,
//     NoSuchFieldException, IllegalAccessException{
//     Object linkProperties = getField(wifiConf, "linkProperties");
//     if (linkProperties == null)
//     return;
//     ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>) 
//     getDeclaredField(linkProperties, "mDnses");
//     mDnses.clear(); // 清除原有DNS設置(如果只想增加,不想清除,詞句可省略)
//     mDnses.add(dns);
//     //增加新的DNS
//    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章