I am trying to set my network configuration. The problem is that after I set the properties of configuration and call addNetwork()
method, the return value (configuration id) is -1
. I do not know what's wrong with my code. I have used the same code in another project and it works correctly.
These are the permissions I used:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
the full code
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val wifiManager = activity.getSystemService(Context.WIFI_SERVICE) as WifiManager
val networkAccessPoint = NetworkAccessPoint.getInstance(wifiManager)
networkAccessPoint.generateConfigurationAccessPoint()
return inflater!!.inflate(R.layout.fragment_main, container, false);
}
NetwockAccessPoint class
public void generateConfigurationAccessPoint(){
WifiConfiguration wifiConfiguration = getWifiConfiguration();
String SSID = getSSID();
wifiConfiguration.SSID = String.format("\"%s\"", SSID);
wifiConfiguration.preSharedKey = "\"password\"";
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int networkConfigurationId = mWifiManager.addNetwork(wifiConfiguration);
mWifiManager.enableNetwork(networkConfigurationId, true);
setWifiConfiguration(wifiConfiguration);
setWifiAPEnabled(wifiConfiguration, true);
}
The code doesn't throw any exceptions.
SOLVED
I got the error what i did ... i wanted to create access point with hotspot so that i do not need addNetwork() enableNetwork() setConfiguration()
.... and i had got value -1 wifi was off
(As i mentioned before i wanted AP for hotspot).