How can I set ProxySettings and ProxyProperties on an Android Wi-Fi connection using Java (programatically)?
As ipAssignment, linkProperties, ProxySettings and ProxyProperties are hidden fields within WifiConfiguration on Android 3.1 and up, I need to be able to enum the class and use the fields.
Following the code sample using the link below, I can set a static IP address, gateway and DNS for a particular Wi-Fi connection, but I also need to set Wificonfiguration.ProxySettings.STATIC and ProxyProperties
See Stack Overflow question How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.x.
For example,
WifiConfiguration config = new WifiConfiguration(configuration);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, ""));
Looking for something like:
setProxySettings("STATIC", wifiConf);
setProxyProperties("proxyserver.mine.com.au", 8080, ""); // Set Proxy server and port.
wifiManager.updateNetwork(wifiConf); //apply the setting
Using the following code from coolypf .ipAssignment .ProxySettings and linkProperties are hidden...
WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
manager.asyncConnect(this, new Handler());
if (!manager.isWifiEnabled()) return;
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;
}
if (configuration == null) return;
WifiConfiguration config = new WifiConfiguration(configuration);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.clear();
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, ""));
manager.saveNetwork(config);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…