如何以编程方式创建和读取的WEP / EAP无线网络配置中的Android

 我来答
匿名用户

2014-12-21
展开全部
1. 第1部分:创建一个无线网络的WEP配置编程 。

第2部分:阅读一个WEP无线网络配置编程
再次Straighforward。

第3部分:读一个EAP的WiFi配置编程
现在,这是棘手的。你可以找到它通过香草的Android用户界面中WifiDialog.java节省了EAP的WiFi配置的代码。唔够方便我们在我们的应用程序的代码,那么不要!如果你碰巧去尝试这一点,你会得到错误说找不到符号eap,phase,client_cert等。有点详细的调查EnterpriseFieldis private内WiFiConfiguration类和所有的符号,我们无法找到是类型EnterpriseField。好了,我们已经打了一个路障,我们需要这些字段读取/保存一个EAP配置,但我们并没有以编程方式访问他们!Java Reflection API救援
好吧,我不是一个Java专家,所以我不会越来越到的反射API的细节,例如,你可以谷歌的教程或到达这里。
为了保持简短而亲切,反射API允许你检查类,接口,字段在不知道的类,方法等,还可以实例化新对象,并获取/设置现场reflection.And,重要的是能思考的帮助您访问私有的类里面嗯,这是我们需要做的不是吗? :)
让我们来检查代码示例现在它显示了如何读取一个EAP的WiFi反射API。作为一个片段将记录配置到一个文件,并将其保存在SD卡....非常漂亮..诶;)反射API概述一点点,我相信ING下面的代码是很容易。
private static final String INT_PRIVATE_KEY = "private_key";
private static final String INT_PHASE2 = "phase2";
private static final String INT_PASSWORD = "password";
private static final String INT_IDENTITY = "identity";
private static final String INT_EAP = "eap";
private static final String INT_CLIENT_CERT = "client_cert";
private static final String INT_CA_CERT = "ca_cert";
private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";

第4部分:储存的EAP无线网络配置编程
如果你已经读过的部分3,您已经了解了反射mojo,在这里工作,如果你是直接跳跃到本节,请在第3部分的代码段之前,看了介绍,你会加快速度通过代码来这里微风!
void saveEapConfig(String passString, String userName)
{
/********************************Configuration Strings****************************************************/
final String ENTERPRISE_EAP = "TLS";
final String ENTERPRISE_CLIENT_CERT = " CodeGo.net
final String ENTERPRISE_PRIV_KEY = " CodeGo.net
//CertificateName = Name given to the certificate while installing it

/*Optional Params- My wireless Doesn't use these*/
final String ENTERPRISE_PHASE2 = "";
final String ENTERPRISE_ANON_IDENT = "ABC";
final String ENTERPRISE_CA_CERT = "";
/********************************Configuration Strings****************************************************/

/*Create a WifiConfig*/
WifiConfiguration selectedConfig = new WifiConfiguration();

/*AP Name*/
selectedConfig.SSID = "\"SSID_Name\"";

/*Priority*/
selectedConfig.priority = 40;

/*Enable Hidden SSID*/
selectedConfig.hiddenSSID = true;

/*Key Mgmnt*/
selectedConfig.allowedKeyManagement.clear();
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

/*Group Ciphers*/
selectedConfig.allowedGroupCiphers.clear();
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

/*Pairwise ciphers*/
selectedConfig.allowedPairwiseCiphers.clear();
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

/*Protocols*/
selectedConfig.allowedProtocols.clear();
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

// Enterprise Settings
// Reflection magic here too, need access to non-public APIs
try {
// Let the magic start
Class[] wcClasses = WifiConfiguration.class.getClasses();
// null for overzealous java compiler
Class wcEnterpriseField = null;

for (Class wcClass : wcClasses)
if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME))
{
wcEnterpriseField = wcClass;
break;
}
boolean noEnterpriseFieldType = false;
if(wcEnterpriseField == null)
noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly

Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;
Field[] wcefFields = WifiConfiguration.class.getFields();
// Dispatching Field vars
for (Field wcefField : wcefFields)
{
if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
wcefAnonymousId = wcefField;
else if (wcefField.getName().equals(INT_CA_CERT))
wcefCaCert = wcefField;
else if (wcefField.getName().equals(INT_CLIENT_CERT))
wcefClientCert = wcefField;
else if (wcefField.getName().equals(INT_EAP))
wcefEap = wcefField;
else if (wcefField.getName().equals(INT_IDENTITY))
wcefIdentity = wcefField;
else if (wcefField.getName().equals(INT_PASSWORD))
wcefPassword = wcefField;
else if (wcefField.getName().equals(INT_PHASE2))
wcefPhase2 = wcefField;
else if (wcefField.getName().equals(INT_PRIVATE_KEY))
wcefPrivateKey = wcefField;
}

Method wcefSetValue = null;
if(!noEnterpriseFieldType){
for(Method m: wcEnterpriseField.getMethods())
//System.out.println(m.getName());
if(m.getName().trim().equals("setValue"))
wcefSetValue = m;
}

/*EAP Method*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);
}
else
{
wcefEap.set(selectedConfig, ENTERPRISE_EAP);
}
/*EAP Phase 2 Authentication*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);
}
else
{
wcefPhase2.set(selectedConfig, ENTERPRISE_PHASE2);
}
/*EAP Anonymous Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);
}
else
{
wcefAnonymousId.set(selectedConfig, ENTERPRISE_ANON_IDENT);
}
/*EAP CA Certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);
}
else
{
wcefCaCert.set(selectedConfig, ENTERPRISE_CA_CERT);
}
/*EAP Private key*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);
}
else
{
wcefPrivateKey.set(selectedConfig, ENTERPRISE_PRIV_KEY);
}
/*EAP Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);
}
else
{
wcefIdentity.set(selectedConfig, userName);
}
/*EAP Password*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);
}
else
{
wcefPassword.set(selectedConfig, passString);
}
/*EAp Client certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);
}
else
{
wcefClientCert.set(selectedConfig, ENTERPRISE_CLIENT_CERT);
}
// Adhoc for CM6
// if non-CM6 fails gracefully thanks to nested try-catch

try{
Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID");
Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
//wcAdhoc.setBoolean(selectedConfig, prefs.getBoolean(PREF_ADHOC,
// false));
wcAdhoc.setBoolean(selectedConfig, false);
int freq = 2462; // default to channel 11
//int freq = Integer.parseInt(prefs.getString(PREF_ADHOC_FREQUENCY,
//"2462")); // default to channel 11
//System.err.println(freq);
wcAdhocFreq.setInt(selectedConfig, freq);
} catch (Exception e)
{
e.printStackTrace();
}

} catch (Exception e)
{
// TODO Auto-generated catch block
// FIXME As above, what should I do here?
e.printStackTrace();
}

WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean res1 = wifiManag.setWifiEnabled(true);
int res = wifiManag.addNetwork(selectedConfig);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false);
Log.d("WifiPreference", "enableNetwork returned " + b );
boolean c = wifiManag.saveConfiguration();
Log.d("WifiPreference", "Save configuration returned " + c );
boolean d = wifiManag.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + d );
}

以及多数民众赞成它!我希望这可以帮助开发者丢失,:)

希望这会有所帮助的
love贾贾王子
2015-08-24 · TA获得超过176个赞
知道小有建树答主
回答量:112
采纳率:0%
帮助的人:48.2万
展开全部
1. 第1部分:创建一个无线网络的WEP配置编程 。

第2部分:阅读一个WEP无线网络配置编程
再次Straighforward。

第3部分:读一个EAP的WiFi配置编程
现在,这是棘手的。你可以找到它通过香草的Android用户界面中WifiDialog.java节省了EAP的WiFi配置的代码。唔够方便我们在我们的应用程序的代码,那么不要!如果你碰巧去尝试这一点,你会得到错误说找不到符号eap,phase,client_cert等。有点详细的调查EnterpriseFieldis private内WiFiConfiguration类和所有的符号,我们无法找到是类型EnterpriseField。好了,我们已经打了一个路障,我们需要这些字段读取/保存一个EAP配置,但我们并没有以编程方式访问他们!Java Reflection API救援
好吧,我不是一个Java专家,所以我不会越来越到的反射API的细节,例如,你可以谷歌的教程或到达这里。
为了保持简短而亲切,反射API允许你检查类,接口,字段在不知道的类,方法等,还可以实例化新对象,并获取/设置现场reflection.And,重要的是能思考的帮助您访问私有的类里面嗯,这是我们需要做的不是吗? :)
让我们来检查代码示例现在它显示了如何读取一个EAP的WiFi反射API。作为一个片段将记录配置到一个文件,并将其保存在SD卡....非常漂亮..诶;)反射API概述一点点,我相信ING下面的代码是很容易。
private static final String INT_PRIVATE_KEY = "private_key";
private static final String INT_PHASE2 = "phase2";
private static final String INT_PASSWORD = "password";
private static final String INT_IDENTITY = "identity";
private static final String INT_EAP = "eap";
private static final String INT_CLIENT_CERT = "client_cert";
private static final String INT_CA_CERT = "ca_cert";
private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";

第4部分:储存的EAP无线网络配置编程
如果你已经读过的部分3,您已经了解了反射mojo,在这里工作,如果你是直接跳跃到本节,请在第3部分的代码段之前,看了介绍,你会加快速度通过代码来这里微风!
void saveEapConfig(String passString, String userName)
{
/********************************Configuration Strings****************************************************/
final String ENTERPRISE_EAP = "TLS";
final String ENTERPRISE_CLIENT_CERT = " CodeGo.net
final String ENTERPRISE_PRIV_KEY = " CodeGo.net
//CertificateName = Name given to the certificate while installing it

/*Optional Params- My wireless Doesn't use these*/
final String ENTERPRISE_PHASE2 = "";
final String ENTERPRISE_ANON_IDENT = "ABC";
final String ENTERPRISE_CA_CERT = "";
/********************************Configuration Strings****************************************************/

/*Create a WifiConfig*/
WifiConfiguration selectedConfig = new WifiConfiguration();

/*AP Name*/
selectedConfig.SSID = "\"SSID_Name\"";

/*Priority*/
selectedConfig.priority = 40;

/*Enable Hidden SSID*/
selectedConfig.hiddenSSID = true;

/*Key Mgmnt*/
selectedConfig.allowedKeyManagement.clear();
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

/*Group Ciphers*/
selectedConfig.allowedGroupCiphers.clear();
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

/*Pairwise ciphers*/
selectedConfig.allowedPairwiseCiphers.clear();
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

/*Protocols*/
selectedConfig.allowedProtocols.clear();
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

// Enterprise Settings
// Reflection magic here too, need access to non-public APIs
try {
// Let the magic start
Class[] wcClasses = WifiConfiguration.class.getClasses();
// null for overzealous java compiler
Class wcEnterpriseField = null;

for (Class wcClass : wcClasses)
if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME))
{
wcEnterpriseField = wcClass;
break;
}
boolean noEnterpriseFieldType = false;
if(wcEnterpriseField == null)
noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly

Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;
Field[] wcefFields = WifiConfiguration.class.getFields();
// Dispatching Field vars
for (Field wcefField : wcefFields)
{
if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
wcefAnonymousId = wcefField;
else if (wcefField.getName().equals(INT_CA_CERT))
wcefCaCert = wcefField;
else if (wcefField.getName().equals(INT_CLIENT_CERT))
wcefClientCert = wcefField;
else if (wcefField.getName().equals(INT_EAP))
wcefEap = wcefField;
else if (wcefField.getName().equals(INT_IDENTITY))
wcefIdentity = wcefField;
else if (wcefField.getName().equals(INT_PASSWORD))
wcefPassword = wcefField;
else if (wcefField.getName().equals(INT_PHASE2))
wcefPhase2 = wcefField;
else if (wcefField.getName().equals(INT_PRIVATE_KEY))
wcefPrivateKey = wcefField;
}

Method wcefSetValue = null;
if(!noEnterpriseFieldType){
for(Method m: wcEnterpriseField.getMethods())
//System.out.println(m.getName());
if(m.getName().trim().equals("setValue"))
wcefSetValue = m;
}

/*EAP Method*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);
}
else
{
wcefEap.set(selectedConfig, ENTERPRISE_EAP);
}
/*EAP Phase 2 Authentication*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);
}
else
{
wcefPhase2.set(selectedConfig, ENTERPRISE_PHASE2);
}
/*EAP Anonymous Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);
}
else
{
wcefAnonymousId.set(selectedConfig, ENTERPRISE_ANON_IDENT);
}
/*EAP CA Certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);
}
else
{
wcefCaCert.set(selectedConfig, ENTERPRISE_CA_CERT);
}
/*EAP Private key*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);
}
else
{
wcefPrivateKey.set(selectedConfig, ENTERPRISE_PRIV_KEY);
}
/*EAP Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);
}
else
{
wcefIdentity.set(selectedConfig, userName);
}
/*EAP Password*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);
}
else
{
wcefPassword.set(selectedConfig, passString);
}
/*EAp Client certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);
}
else
{
wcefClientCert.set(selectedConfig, ENTERPRISE_CLIENT_CERT);
}
// Adhoc for CM6
// if non-CM6 fails gracefully thanks to nested try-catch

try{
Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID");
Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
//wcAdhoc.setBoolean(selectedConfig, prefs.getBoolean(PREF_ADHOC,
// false));
wcAdhoc.setBoolean(selectedConfig, false);
int freq = 2462; // default to channel 11
//int freq = Integer.parseInt(prefs.getString(PREF_ADHOC_FREQUENCY,
//"2462")); // default to channel 11
//System.err.println(freq);
wcAdhocFreq.setInt(selectedConfig, freq);
} catch (Exception e)
{
e.printStackTrace();
}

} catch (Exception e)
{
// TODO Auto-generated catch block
// FIXME As above, what should I do here?
e.printStackTrace();
}

WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean res1 = wifiManag.setWifiEnabled(true);
int res = wifiManag.addNetwork(selectedConfig);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false);
Log.d("WifiPreference", "enableNetwork returned " + b );
boolean c = wifiManag.saveConfiguration();
Log.d("WifiPreference", "Save configuration returned " + c );
boolean d = wifiManag.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + d );
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
baikewsp
2015-08-15 · TA获得超过1万个赞
知道小有建树答主
回答量:4588
采纳率:42%
帮助的人:678万
展开全部
1. 第1部分:创建一个无线网络的WEP配置编程 。

第2部分:阅读一个WEP无线网络配置编程
再次Straighforward。

第3部分:读一个EAP的WiFi配置编程
现在,这是棘手的。你可以找到它通过香草的Android用户界面中WifiDialog.java节省了EAP的WiFi配置的代码。唔够方便我们在我们的应用程序的代码,那么不要!如果你碰巧去尝试这一点,你会得到错误说找不到符号eap,phase,client_cert等。有点详细的调查EnterpriseFieldis private内WiFiConfiguration类和所有的符号,我们无法找到是类型EnterpriseField。好了,我们已经打了一个路障,我们需要这些字段读取/保存一个EAP配置,但我们并没有以编程方式访问他们!Java Reflection API救援
好吧,我不是一个Java专家,所以我不会越来越到的反射API的细节,例如,你可以谷歌的教程或到达这里。
为了保持简短而亲切,反射API允许你检查类,接口,字段在不知道的类,方法等,还可以实例化新对象,并获取/设置现场reflection.And,重要的是能思考的帮助您访问私有的类里面嗯,这是我们需要做的不是吗? :)
让我们来检查代码示例现在它显示了如何读取一个EAP的WiFi反射API。作为一个片段将记录配置到一个文件,并将其保存在SD卡....非常漂亮..诶;)反射API概述一点点,我相信ING下面的代码是很容易。
private static final String INT_PRIVATE_KEY = "private_key";
private static final String INT_PHASE2 = "phase2";
private static final String INT_PASSWORD = "password";
private static final String INT_IDENTITY = "identity";
private static final String INT_EAP = "eap";
private static final String INT_CLIENT_CERT = "client_cert";
private static final String INT_CA_CERT = "ca_cert";
private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";

第4部分:储存的EAP无线网络配置编程
如果你已经读过的部分3,您已经了解了反射mojo,在这里工作,如果你是直接跳跃到本节,请在第3部分的代码段之前,看了介绍,你会加快速度通过代码来这里微风!
void saveEapConfig(String passString, String userName)
{
/********************************Configuration Strings****************************************************/
final String ENTERPRISE_EAP = "TLS";
final String ENTERPRISE_CLIENT_CERT = " CodeGo.net
final String ENTERPRISE_PRIV_KEY = " CodeGo.net
//CertificateName = Name given to the certificate while installing it

/*Optional Params- My wireless Doesn't use these*/
final String ENTERPRISE_PHASE2 = "";
final String ENTERPRISE_ANON_IDENT = "ABC";
final String ENTERPRISE_CA_CERT = "";
/********************************Configuration Strings****************************************************/

/*Create a WifiConfig*/
WifiConfiguration selectedConfig = new WifiConfiguration();

/*AP Name*/
selectedConfig.SSID = "\"SSID_Name\"";

/*Priority*/
selectedConfig.priority = 40;

/*Enable Hidden SSID*/
selectedConfig.hiddenSSID = true;

/*Key Mgmnt*/
selectedConfig.allowedKeyManagement.clear();
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

/*Group Ciphers*/
selectedConfig.allowedGroupCiphers.clear();
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

/*Pairwise ciphers*/
selectedConfig.allowedPairwiseCiphers.clear();
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

/*Protocols*/
selectedConfig.allowedProtocols.clear();
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

// Enterprise Settings
// Reflection magic here too, need access to non-public APIs
try {
// Let the magic start
Class[] wcClasses = WifiConfiguration.class.getClasses();
// null for overzealous java compiler
Class wcEnterpriseField = null;

for (Class wcClass : wcClasses)
if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME))
{
wcEnterpriseField = wcClass;
break;
}
boolean noEnterpriseFieldType = false;
if(wcEnterpriseField == null)
noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly

Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;
Field[] wcefFields = WifiConfiguration.class.getFields();
// Dispatching Field vars
for (Field wcefField : wcefFields)
{
if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
wcefAnonymousId = wcefField;
else if (wcefField.getName().equals(INT_CA_CERT))
wcefCaCert = wcefField;
else if (wcefField.getName().equals(INT_CLIENT_CERT))
wcefClientCert = wcefField;
else if (wcefField.getName().equals(INT_EAP))
wcefEap = wcefField;
else if (wcefField.getName().equals(INT_IDENTITY))
wcefIdentity = wcefField;
else if (wcefField.getName().equals(INT_PASSWORD))
wcefPassword = wcefField;
else if (wcefField.getName().equals(INT_PHASE2))
wcefPhase2 = wcefField;
else if (wcefField.getName().equals(INT_PRIVATE_KEY))
wcefPrivateKey = wcefField;
}

Method wcefSetValue = null;
if(!noEnterpriseFieldType){
for(Method m: wcEnterpriseField.getMethods())
//System.out.println(m.getName());
if(m.getName().trim().equals("setValue"))
wcefSetValue = m;
}

/*EAP Method*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);
}
else
{
wcefEap.set(selectedConfig, ENTERPRISE_EAP);
}
/*EAP Phase 2 Authentication*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);
}
else
{
wcefPhase2.set(selectedConfig, ENTERPRISE_PHASE2);
}
/*EAP Anonymous Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);
}
else
{
wcefAnonymousId.set(selectedConfig, ENTERPRISE_ANON_IDENT);
}
/*EAP CA Certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);
}
else
{
wcefCaCert.set(selectedConfig, ENTERPRISE_CA_CERT);
}
/*EAP Private key*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);
}
else
{
wcefPrivateKey.set(selectedConfig, ENTERPRISE_PRIV_KEY);
}
/*EAP Identity*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);
}
else
{
wcefIdentity.set(selectedConfig, userName);
}
/*EAP Password*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);
}
else
{
wcefPassword.set(selectedConfig, passString);
}
/*EAp Client certificate*/
if(!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);
}
else
{
wcefClientCert.set(selectedConfig, ENTERPRISE_CLIENT_CERT);
}
// Adhoc for CM6
// if non-CM6 fails gracefully thanks to nested try-catch

try{
Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID");
Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
//wcAdhoc.setBoolean(selectedConfig, prefs.getBoolean(PREF_ADHOC,
// false));
wcAdhoc.setBoolean(selectedConfig, false);
int freq = 2462; // default to channel 11
//int freq = Integer.parseInt(prefs.getString(PREF_ADHOC_FREQUENCY,
//"2462")); // default to channel 11
//System.err.println(freq);
wcAdhocFreq.setInt(selectedConfig, freq);
} catch (Exception e)
{
e.printStackTrace();
}

} catch (Exception e)
{
// TODO Auto-generated catch block
// FIXME As above, what should I do here?
e.printStackTrace();
}

WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean res1 = wifiManag.setWifiEnabled(true);
int res = wifiManag.addNetwork(selectedConfig);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false);
Log.d("WifiPreference", "enableNetwork returned " + b );
boolean c = wifiManag.saveConfiguration();
Log.d("WifiPreference", "Save configuration returned " + c );
boolean d = wifiManag.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + d );
}

以及多数民众赞成它!我希望这可以帮助开发者丢失,:)

希望这会有所帮助的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式