Ashok Malhotra February 2016

How to Set wifi proxy Hostname, port and bypass programetically in android 5.0 and above

I want to set wifi proxy hostname, port and bypass programmatically. i follow this link ->How can I set ProxySettings and ProxyProperties on Android Wi-Fi connection using Java?. but that code is not working on Android 5.0.2. Is there any other way to set those settings? Thanks in advance.

Answers


Jackson Chengalai February 2016

Tested.. Its working in Android 5.1.0, Dont forget to add permissions

      WifiConfiguration GetCurrentWifiConfiguration(WifiManager manager)
      {
        if (!manager.isWifiEnabled())
            return null;

        List<WifiConfiguration> configurationList = manager.getConfiguredNetworks();
        WifiConfiguration configuration = null;
        int cur = manager.getConnectionInfo().getNetworkId();
        for (int i = 0; i < configurationList.size(); ++i)
        {
            WifiConfiguration wifiConfiguration = configurationList.get(i);
            if (wifiConfiguration.networkId == cur)
                configuration = wifiConfiguration;
        }

        return configuration;
    }



    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void setWifiProxySettings5()
    {
        //get the current wifi configuration
        WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration config = GetCurrentWifiConfiguration(manager);
        if(null == config)
            return;

        try
        {
            //linkProperties is no longer in WifiConfiguration
            Class proxyInfoClass = Class.forName("android.net.ProxyInfo");
            Class[] setHttpProxyParams = new Class[1];
            setHttpProxyParams[0] = proxyInfoClass;
            Class wifiConfigClass = Class.forName("android.net.wifi.WifiConfiguration");
            Method setHttpProxy = wifiConfigClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
            setHttpProxy.setAccessible(true);

            //Method 1 to get the ENUM ProxySettings in IpConfiguration
            Class ipConfigClass = Class.forName("android.net.IpConfiguration");
            Field f = ipConfigClass.getField("proxySettings");
            Class proxySettingsClass = f.getType();

            //Method 2 to get the ENUM ProxySettings in IpConfiguration
            //Note the $ between the class and ENUM
            //Class pr 

Post Status

Asked in February 2016
Viewed 1,862 times
Voted 14
Answered 1 times

Search




Leave an answer