I am using the following code for creating the Wifi-hotspot configuration. I am able to create the Hotspot configuration and able to enable it. but i have give configuration for WPA-PSK, But it always taken as OPEN network.
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;
}
After run this app the Hotspot got enabled. Please check the image below.
How to set the WPA_PSK wifi configuration in android app?
As per below answer i have modified the code below.
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;
}
sollution
I have done this in my app. Please check my code below.
First add below permissions to your manifest.
Then create below MyHotspotManager .java class
After that you can call the setHotspot method anywhere in you activity like below. Here I had to check whether app has the ACTION_MANAGE_WRITE_SETTINGS permission.
But if you want you can omit permission request part by changing your targetSdkVersion to 21 in build.gradle. Now you can call it like below
This my HotspotDetails.java class
replace above OPEN to SHARED