My application stopped working once I upgraded to Marshmallow, it was supposed to be able to change the WiFi connection, but now it doesn't do anything at all.
I've spent some time reading about the new permission model of the Android 6.0. Well awesome, but old apps should continue working... Anyway, I started trying to implement the granting of permission, but realized that this is a normal permission and there should be done no permission request for it if it's defined in android manifest:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
The permission has always been in Android Manifest, and if I understand correctly it is supposed to work because it's a "Normal permission". So why doesn't it work, does anybody have a solution?
Adding the code fragment related to my case:
protected void connectWifi() {
if ((!connectedToAccessPoint(settings.getMainConnectionName()))
&& (accessPointIsAvailable(settings.getMainConnectionName()))) {
ConnectionUtils.connectToWifi(this,
settings.getMainConnectionName(),
settings.getMainConnectionPassword());
Toast.makeText(this,
"Connecting to " + settings.getMainConnectionName(),
Toast.LENGTH_LONG).show();
handler.postDelayed(sendUpdatesToUI,
DelayConstants.BASIC_REQUEST_SENT);
handler.postDelayed(sendUpdatesToUI,
DelayConstants.CHANGE_CONNECTION);
}
}
And here the technical part for the connection:
public static void connectToWifi(Context context, String ssid, String password) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
// setup a wifi configuration
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + ssid + "\"";
wc.preSharedKey = "\""+ password + "\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
int netId = wifiManager.addNetwork(wc);
wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
}
In AndroidManifest as mentioned before there's that CHANGE_WIFI_STATE permission, which was there since the app was running on devices not having Android 6.0