Ich verwende den folgenden Code zum Erstellen der Wifi-Hotspot-Konfiguration. Ich kann die Hotspot-Konfiguration erstellen und aktivieren. aber ich habe Konfiguration für WPA-PSK gegeben, aber es hat immer als OPEN-Netzwerk genommen.Wie erstellt man benutzerdefinierte WPA Hotspot mit SSID und Passwort in Android?
public boolean setHotSpot(String SSID,String passWord){
Method[] mMethods = mWifiManager.getClass().getDeclaredMethods();
for(Method mMethod: mMethods){
if(mMethod.getName().equals("setWifiApEnabled")) {
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = SSID ;
netConfig.preSharedKey = passWord;
netConfig.hiddenSSID = true;
netConfig.status = WifiConfiguration.Status.ENABLED;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
netConfig.allowedKeyManagement.set(4);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
try {
mMethod.invoke(mWifiManager, netConfig,true);
mWifiManager.saveConfiguration();
return true;
} catch (Exception e) {
e.printStackTrace();
CommonUtil.log(TAG,"Exception : "+e.getLocalizedMessage());
}
}
}
return false;
}
Nach Durchlauf dieser App der Hotspot aktiviert wurde. Bitte überprüfen Sie das Bild unten.
Wie die WPA_PSK wifi Konfiguration in android app gesetzt?
Wie unten beantwortet ich habe den Code unten geändert.
public boolean setHotSpot(String SSID, String passWord) {
boolean apstatus;
WifiConfiguration netConfig = new WifiConfiguration();
if (passWord == "") {
netConfig.SSID = SSID;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else {
netConfig.SSID = SSID;
netConfig.preSharedKey = passWord;
/*netConfig.hiddenSSID = true;
netConfig.status = WifiConfiguration.Status.ENABLED;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
netConfig.allowedKeyManagement.set(4);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);*/
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
}
try {
Method setWifiApMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
//setWifiApMethod.invoke(mWifiManager, previousConfigurations, false);
//Thread.sleep(2000);
apstatus = (Boolean) setWifiApMethod.invoke(mWifiManager, netConfig, true);
} catch (Exception e) {
CommonUtil.log(TAG, e.getMessage());
return false;
}
return apstatus;
}
seine nicht funktioniert. es zeigt die gleiche Ausgabe wie oben erwähnt. Haben Sie es überprüft mit Android 6 – Yuvaraja