我用这段代码来改变wifi Ip设置(IP地址和网关)
您可以在此处查看完整代码:full class
WifiConfiguration wifiConf = null;
WifiManager manager = (WifiManager) getSystemService(MainActivity.WIFI_SERVICE);
WifiInfo connectionInfo = manager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = manager
.getConfiguredNetworks();
wifiConf = GetCurrentWifiConfiguration(manager);
setIpAssignment("STATIC", wifiConf);
setIpAddress(InetAddress.getByName("192.2.2.2"), 24, wifiConf);
setGateway(InetAddress.getByName("192.2.2.22"), wifiConf);
manager.updateNetwork(wifiConf);
manager.saveConfiguration();
功能:
public static void setIpAssignment(String assign , WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
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);
}
问题在这里:
当您从Android设备中设置检查wifi连接高级选项时,这些代码成功更改了IP地址和网关,但实际上没有设置!!!
我如何理解这一点:
首先我手动设置假的IP地址和网关,修改网络 - &gt;高级选项。然后尝试打开一个镀铬的网站,没有互联网访问,它的工作。然后我从代码中为连接的ssid设置了相同的IP地址和网关,但它没有工作,仍然可以访问互联网。
我在代码和手动中使用了相同的设置但是它不能从代码中使用。我做错了什么?
答案 0 :(得分:0)
正如我所看到的,您可以更改设置并在设置视图中更改值,但设备上系统状态的实际更改未发生。
您最有可能没有权利使用您的程序更改wifi设置。
尝试添加以下内容:
ACCESS_WIFI_STATE
CHANGE_WIFI_STATE
ACCESS_NETWORK_STATE
有关详细信息,请参阅我对问题的评论。
答案 1 :(得分:0)
如果您拥有唯一的IP地址,则可以添加任意数量的重复SSID。
在非专业人士的术语中,SSID只不过是为了简单而将该网络与其他网络区分开来的特定网络的标识。是什么定义了网络的IP地址。如果IP地址之间存在冲突,那么这可能是一个真正的问题。
对于某些设备(如Samsung),您需要检查“显示开放式网络连接”(如果可用),以便查看打开的连接以及默认情况下显示的某些设备。
Ex: - 我们说你有两个SSID作为测试的网络。 Test1具有可供公众使用的开放连接,Test2仅供个人使用并加密。您的移动设备只需连接到信号强度良好的网络即可。由于IP地址不同,因此不存在任何冲突,您可以进行浏览。
如果出现任何冲突(信号强度相同),设备将要求用户选择所需的网络或搜索新网络。
警告: - 出于与财务相关的原因导致开放网络导致严重的安全威胁,您需要自担风险。
IP地址有两个主要功能:主机或网络接口标识和位置寻址。其作用的特征如下:&#34;名称表示我们寻求的东西。地址表示它的位置。路线表明如何到达那里。
我可以给出的最简单的IP地址定义是 -
IP地址作为您必须交付货物的地址。如果您有两个相同的地址,那么货件将交付给谁。有关IP读取的更多详细信息this
现在基本上我对你的问题的回答是:
您可以使用ssid添加多个设置但不同的安全级别
我准备的小型演示:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectToAP("12345", "12345");
WifiConfiguration wifiConf = null;
WifiManager wifiManager = (WifiManager) getSystemService(MainActivity.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == 9) {
wifiConf = conf;
setWifiProxySettings(wifiConf);
try {
setIpAssignment("STATIC", wifiConf); // or "DHCP" for
// dynamic setting
setIpAddress(InetAddress.getByName("192.168.0.100"), 24,
wifiConf);
setGateway(InetAddress.getByName("4.4.4.4"), wifiConf);
setDNS(InetAddress.getByName("4.4.4.4"), wifiConf);
wifiManager.updateNetwork(wifiConf); // apply the setting
wifiManager.saveConfiguration(); // Save it
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
public static void setIpAssignment(String assign, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
setEnumField(wifiConf, assign, "ipAssignment");
}
public 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));
}
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;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
String TAG = "wifi";
WifiManager wifiManager;
public void connectToAP(String ssid, String passkey) {
Log.i(TAG, "* connectToAP");
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
String networkSSID = ssid;
String networkPass = passkey;
Log.d(TAG, "# password " + networkPass);
// for (ScanResult result : scanResultList) {
// if (result.SSID.equals(networkSSID)) {
if (true) {
// String securityMode = getScanResultSecurity(result);
String securityMode = "OPEN";
if (securityMode.equalsIgnoreCase("OPEN")) {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.NONE);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "# add Network returned " + res);
boolean b = wifiManager.enableNetwork(res, true);
Log.d(TAG, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
} else if (securityMode.equalsIgnoreCase("WEP")) {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\"";
wifiConfiguration.wepTxKeyIndex = 0;
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP40);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "### 1 ### add Network returned " + res);
boolean b = wifiManager.enableNetwork(res, true);
Log.d(TAG, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
}
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "### 2 ### add Network returned " + res);
wifiManager.enableNetwork(res, true);
boolean changeHappen = wifiManager.saveConfiguration();
if (res != -1 && changeHappen) {
Log.d(TAG, "### Change happen");
// AppStaticVar.connectedSsidName = networkSSID;
} else {
Log.d(TAG, "*** Change NOT happen");
}
wifiManager.setWifiEnabled(true);
}
// }
}
public String getScanResultSecurity(ScanResult scanResult) {
Log.i(TAG, "* getScanResultSecurity");
final String cap = scanResult.capabilities;
final String[] securityModes = { "WEP", "PSK", "EAP" };
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
return securityModes[i];
}
}
return "OPEN";
}
/* PROXY SETTINGS */
/*********************************************************************/
// 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 {
// Field f = obj.getClass().getField(name);
// f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
// }
public static void setProxySettings(String assign,
WifiConfiguration wifiConf) throws SecurityException,
IllegalArgumentException, NoSuchFieldException,
IllegalAccessException {
setEnumField(wifiConf, assign, "proxySettings");
}
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;
}
void setWifiProxySettings(WifiConfiguration config) {
if (null == config)
return;
try {
Object linkProperties = getField(config, "linkProperties");
if (null == linkProperties)
return;
Class proxyPropertiesClass = Class
.forName("android.net.ProxyProperties");
Class[] setHttpProxyParams = new Class[1];
setHttpProxyParams[0] = proxyPropertiesClass;
Class lpClass = Class.forName("android.net.LinkProperties");
Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy",
setHttpProxyParams);
setHttpProxy.setAccessible(true);
// get ProxyProperties constructor
Class[] proxyPropertiesCtorParamTypes = new Class[3];
proxyPropertiesCtorParamTypes[0] = String.class;
proxyPropertiesCtorParamTypes[1] = int.class;
proxyPropertiesCtorParamTypes[2] = String.class;
Constructor proxyPropertiesCtor = proxyPropertiesClass
.getConstructor(proxyPropertiesCtorParamTypes);
// create the parameters for the constructor
Object[] proxyPropertiesCtorParams = new Object[3];
proxyPropertiesCtorParams[0] = "127.0.0.1";
proxyPropertiesCtorParams[1] = 8118;
proxyPropertiesCtorParams[2] = "example.com";
// create a new object using the params
Object proxySettings = proxyPropertiesCtor
.newInstance(proxyPropertiesCtorParams);
// pass the new object to setHttpProxy
Object[] params = new Object[1];
params[0] = proxySettings;
setHttpProxy.invoke(linkProperties, params);
setProxySettings("STATIC", config);
} catch (Exception e) {
}
}
void unsetWifiProxySettings() {
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = GetCurrentWifiConfiguration(manager);
if (null == config)
return;
try {
// get the link properties from the wifi configuration
Object linkProperties = getField(config, "linkProperties");
if (null == linkProperties)
return;
// get the setHttpProxy method for LinkProperties
Class proxyPropertiesClass = Class
.forName("android.net.ProxyProperties");
Class[] setHttpProxyParams = new Class[1];
setHttpProxyParams[0] = proxyPropertiesClass;
Class lpClass = Class.forName("android.net.LinkProperties");
Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy",
setHttpProxyParams);
setHttpProxy.setAccessible(true);
// pass null as the proxy
Object[] params = new Object[1];
params[0] = null;
setHttpProxy.invoke(linkProperties, params);
setProxySettings("NONE", config);
// save the config
manager.updateNetwork(config);
manager.disconnect();
manager.reconnect();
} catch (Exception e) {
}
}
}
在上面的代码中尝试运行相同的代码:
String securityMode = "OPEN";
String securityMode = "WEP";
一次一次:
观察 :两个设置都是程序性地增加但是只能看到它们之间的相互重叠......但两者都存在。 现在问题是我怎么知道两者都存在? 1.运行上面的代码。 2.一次使用OPEN作为安全级别,一次使用WEP 3.一旦添加了标签wifi和忘记网络的选项..点击它 你会观察到虽然你删除了它,它仍然在那里:) 因此,两个设置都存在,但是以编程方式重叠。
我使用HOTSPOT进行分析:
我将2台设备作为热点,具有相同的ssid但不同的安全级别,我可以看到2个独立的wifi,但事情是在以编程方式添加时,它会保留在那里但重叠。
注意:手动添加或编程相同的WIFI设置将覆盖现有的一个,但即使它设置了不同的工作。
答案 2 :(得分:0)
mWifiManager.updateNetwork(selectWIfiConfi);
mWifiManager.saveConfiguration();
mWifiManager.setWifiEnabled(false);
mWifiManager.setWifiEnabled(true);
我重新打开wifi,它的电话版本为4.2.2