天天看點

eap方法 華為手機怎麼連wifi_如何以程式設計方式在Android中建立和讀取WEP / EAP WiFi配置?...

啊,我沒有編輯空間,在這裡添加剩下的部分。

第4部分:以程式設計方式儲存EAP WiFi配置

如果您已經閱讀了第3部分,那麼您已經了解了在這裡工作的Reflection魔法,如果您直接跳到本節,請閱讀第3部分中的代碼片段之前的介紹,您将快速完成這裡的代碼。 !void saveEapConfig(String passString, String userName)

{

final String ENTERPRISE_EAP = "TLS";

final String ENTERPRISE_CLIENT_CERT = "keystore://USRCERT_CertificateName";

final String ENTERPRISE_PRIV_KEY = "USRPKEY_CertificateName";

//CertificateName = Name given to the certificate while installing it

final String ENTERPRISE_PHASE2 = "";

final String ENTERPRISE_ANON_IDENT = "ABC";

final String ENTERPRISE_CA_CERT = ""; // If required: "keystore://CACERT_CaCertificateName"

WifiConfiguration selectedConfig = new WifiConfiguration();

selectedConfig.SSID = "\"SSID_Name\"";

selectedConfig.priority = 40;

selectedConfig.hiddenSSID = true;

selectedConfig.allowedKeyManagement.clear();

selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);

selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

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);

selectedConfig.allowedPairwiseCiphers.clear();

selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

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, wcefEngine = null, wcefEngineId = 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;

else if (wcefField.getName().equals("engine"))

wcefEngine = wcefField;

else if (wcefField.getName().equals("engine_id"))

wcefEngineId = wcefField;

}

Method wcefSetValue = null;

if(!noEnterpriseFieldType){

for(Method m: wcEnterpriseField.getMethods())

//System.out.println(m.getName());

if(m.getName().trim().equals("setValue"))

wcefSetValue = m;

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);

}

else

{

wcefEap.set(selectedConfig, ENTERPRISE_EAP);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);

}

else

{

wcefPhase2.set(selectedConfig, ENTERPRISE_PHASE2);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);

}

else

{

wcefAnonymousId.set(selectedConfig, ENTERPRISE_ANON_IDENT);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);

}

else

{

wcefCaCert.set(selectedConfig, ENTERPRISE_CA_CERT);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);

}

else

{

wcefPrivateKey.set(selectedConfig, ENTERPRISE_PRIV_KEY);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);

}

else

{

wcefIdentity.set(selectedConfig, userName);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);

}

else

{

wcefPassword.set(selectedConfig, passString);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);

}

else

{

wcefClientCert.set(selectedConfig, ENTERPRISE_CLIENT_CERT);

}

if(!noEnterpriseFieldType)

{

wcefSetValue.invoke(wcefEngine.get(wifiConf), "1");

wcefSetValue.invoke(wcefEngineId.get(wifiConf), "keystore");

}

// 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 );  }

那就好了!我希望這有助于一些失去的開發人員,某個地方,某個時候:)