1

I'm working on an app that needs to change DNS servers on an active Wi-Fi connection.

I get WiFiConfiguration object of active WiFi connection and I know about linkProperties property of Java's WiFiConfiguration. But Mono's WifiConfiguration does not contain this feature; as it can get around this situation?

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
slapshin
  • 25
  • 6

1 Answers1

0

There is no API for that, but you can use reflection to do the hack. There is no guarantee that this will work

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); 
    }
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • The documentation (http://developer.android.com/reference/android/provider/Settings.System.html) contains property WIFI_STATIC_DNS1. In android 2 could use it to set the DNS server (http://stackoverflow.com/questions/4106502/set-android-ip-dns-gateway-setting-programatically). But this property is deprecated in api17. Google recommends using WiFiManager – slapshin Apr 20 '15 at 19:16
  • I tried it through reflection before - but Xamarin not allowed to do so (don't allow modify object's fields). Moreover, it is an unreliable method and for program for many people hardly acceptable.... – slapshin Apr 21 '15 at 19:07