Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址

//查看Wifi地址
public String getWifiIpAddress() {
    //获取wifi服务
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    //判断wifi是否开启
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    String ip = intToIp(ipAddress);
    Log.d("wuc", "wifiIp = " + ip);
    return ip;

}

//获取GPRS本地ip地址
public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf
                    .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    Log.d("wuc", "gprs = " + inetAddress.getHostAddress().toString());
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return null;
}

private String intToIp(int i) {

    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 void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = 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 block
    e.printStackTrace();
}
Toast.makeText(this, "1121", Toast.LENGTH_SHORT).show();
wifiManager.updateNetwork(wifiConf); // apply the setting

public static void setIpAssignment(String assign, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException,
        NoSuchFieldException, IllegalAccessException {
    Log.d("wuc", "wifiConfin = " + wifiConf);
    setEnumField(wifiConf, assign, "ipAssignment");
}

public 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 mLinkAddresses = (ArrayList) getDeclaredField(linkProperties,
            "mLinkAddresses");
    mLinkAddresses.clear();
    mLinkAddresses.add(linkAddress);
}

public 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;
    Class routeInfoClass = Class.forName("android.net.RouteInfo");
    Constructor routeInfoConstructor = routeInfoClass
            .getConstructor(new Class[]{InetAddress.class});
    Object routeInfo = routeInfoConstructor.newInstance(gateway);

    ArrayList mRoutes = (ArrayList) getDeclaredField(linkProperties,
            "mRoutes");
    mRoutes.clear();
    mRoutes.add(routeInfo);
}

public 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(); // or add a new dns address , here I just want to
    // replace DNS1
    mDnses.add(dns);
}

public 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;
}

public 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;
}

public static void setEnumField(Object obj, String value, String name)
        throws SecurityException, NoSuchFieldException,
        IllegalArgumentException, IllegalAccessException {
    Log.d("wuc", "wifiConfinin = " + obj);
    Field f = obj.getClass().getField(name);
    f.set(obj, Enum.valueOf((Class<Enum>) 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)
不知道是哪错了求大神指导
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值