public static WifiConfiguration parseWifiConfigJson(Context context, String jsonString) {
WifiConfiguration config = null
try {
JSONObject jsonObject = new JSONObject(jsonString)
config = new WifiConfiguration()
config.allowedAuthAlgorithms.clear()
config.allowedGroupCiphers.clear()
config.allowedKeyManagement.clear()
config.allowedPairwiseCiphers.clear()
config.allowedProtocols.clear()
config.SSID = "\"" + jsonObject.optString("SSID") + "\""
boolean autoConnect = jsonObject.optInt("autoConnect") == 1
config.status = autoConnect ? WifiConfiguration.Status.ENABLED
: WifiConfiguration.Status.DISABLED
if (autoConnect) {
config.priority = getMaxPriorityOfWifiConfig(context) + 1
}
String passWord = jsonObject.optString("passWord")
boolean hideNetwork = jsonObject.optInt("hideNetwork") == 1
config.hiddenSSID = hideNetwork
int authType = jsonObject.optInt("authenticationType")
Log.d("WifiConfig", config.SSID + "auth type is: " + authType + " , hideSSID is: "
+ hideNetwork)
switch (authType) {
case WIFICIPHER_NOPASS:
config.wepKeys[0] = "\"\""
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
config.wepTxKeyIndex = 0
break
case WIFICIPHER_WEP:
config.wepKeys[0] = formatWepPassword(passWord)
config.wepTxKeyIndex = 0
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED)
break
case WIFICIPHER_WPA:
if (!TextUtils.isEmpty(passWord)) {
config.preSharedKey = "\"" + passWord + "\""
}
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
break
case WIFICIPHER_EAP:
parseEapWifiConfig(context, config, jsonObject)
break
default:
config = null
}
return config
} catch (JSONException e) {
e.printStackTrace()
Log.e("WifiConfig", "parse json to wifi configuration failed")
}
return null
}
private static void parseEapWifiConfig(Context context, WifiConfiguration config,
JSONObject jsonObject) {
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X)
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP)
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40)
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104)
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP)
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP)
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN)
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA)
JSONObject eapJson
int eapType = -1
int phraseTypeId = -1
String anonymousName = ""
String username = ""
try {
eapJson = jsonObject.getJSONObject("detailInfo")
JSONObject eapTypealJson = eapJson.getJSONObject("protocal")
eapType = eapTypealJson.optInt("EAPType")
phraseTypeId = eapTypealJson.getInt("stage2Auth")
JSONObject identityJson = eapJson.getJSONObject("userIndentify")
username = identityJson.getString("currentUserName")
anonymousName = identityJson.optString("anonymousName")
} catch (JSONException e) {
e.printStackTrace()
Log.d("WifiConfig", "parse json error: " + e.getMessage())
config = null
return
}
String enterpriseEap = ""
int eapMethodId = WifiEnterpriseConfig.Eap.NONE
switch (eapType) {
case EAP_TYPE_TLS_JSON_KEY:
enterpriseEap = "TLS"
eapMethodId = WifiEnterpriseConfig.Eap.TLS
break
case EAP_TYPE_TTLS_JSON_KEY:
enterpriseEap = "TTLS"
eapMethodId = WifiEnterpriseConfig.Eap.TTLS
break
case EAP_TYPE_PEAP_JSON_KEY:
enterpriseEap = "PEAP"
eapMethodId = WifiEnterpriseConfig.Eap.PEAP
break
default:
enterpriseEap = ""
eapMethodId = WifiEnterpriseConfig.Eap.NONE
break
}
Log.d("WifiConfig", " enterpriseEap Type is: " + enterpriseEap)
String phase2 = ""
int phase2Id = WifiEnterpriseConfig.Phase2.NONE
switch (phraseTypeId) {
case PHASE_TYPE_NONE_JSON_KEY:
phase2 = ""
phase2Id = WifiEnterpriseConfig.Phase2.NONE
break
case PHASE_TYPE_PAP_JSON_KEY:
phase2 = "PAP"
phase2Id = WifiEnterpriseConfig.Phase2.PAP
break
case PHASE_TYPE_MSCHAP_JSON_KEY:
phase2 = "MSCHAP"
phase2Id = WifiEnterpriseConfig.Phase2.MSCHAP
break
case PHASE_TYPE_MSCHAPV2_JSON_KEY:
phase2 = "MSCHAPV2"
phase2Id = WifiEnterpriseConfig.Phase2.MSCHAPV2
break
case PHASE_TYPE_GTC_JSON_KEY:
phase2 = "GTC"
phase2Id = WifiEnterpriseConfig.Phase2.GTC
break
default:
phase2 = ""
phase2Id = WifiEnterpriseConfig.Phase2.NONE
}
//android4.3后自有api可是实现,不需要通过反射方式实现
if (android.os.Build.VERSION.SDK_INT >= 18) {
Log.d("WifiConfig", "set eap config >=18")
WifiEnterpriseConfig eapConfig = new WifiEnterpriseConfig()
eapConfig.setAnonymousIdentity(anonymousName)
eapConfig.setEapMethod(eapMethodId)
eapConfig.setIdentity(username)
eapConfig.setPhase2Method(phase2Id)
config.enterpriseConfig = eapConfig
return
}
//android4.3以下通过反射方式实现
// 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
// 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())
if (m.getName().trim().equals("setValue"))
wcefSetValue = m
}
if (!noEnterpriseFieldType) {
if (!TextUtils.isEmpty(enterpriseEap))
wcefSetValue.invoke(wcefEap.get(config), enterpriseEap)
} else {
if (!TextUtils.isEmpty(enterpriseEap))
wcefEap.set(config, enterpriseEap)
}
if (!noEnterpriseFieldType) {
if (!TextUtils.isEmpty("phase2"))
wcefSetValue.invoke(wcefPhase2.get(config), phase2)
}
else
{
if (!TextUtils.isEmpty("phase2"))
wcefPhase2.set(config, phase2)
}
if (!noEnterpriseFieldType)
{
if (!TextUtils.isEmpty("anonymousName"))
wcefSetValue.invoke(wcefAnonymousId.get(config), anonymousName)
}
else
{
if (!TextUtils.isEmpty("anonymousName"))
wcefAnonymousId.set(config, anonymousName)
}
if (!noEnterpriseFieldType)
{
wcefSetValue.invoke(wcefIdentity.get(config), username)
}
else
{
wcefIdentity.set(config, username)
}
if (!noEnterpriseFieldType)
{
// wcefSetValue.invoke(wcefPassword.get(config), passString)
}
else
{
// wcefPassword.set(config, passString)
}
} catch (Exception e) {
e.printStackTrace()
Log.e("WifiConfig", "parseEapWifiConfig catch error: " + e.getMessage())
}
}