Android 5.0 Lollipop And 4.4 Kitkat Ignores My Wifi Network, Enablenetwork() Is Useless
Solution 1:
You can try to use new Lollipop API ConnectivityManager.requestNetwork(), supposedly like this:
ConnectivityManagercm= (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.requestNetwork(newNetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier("XX:XX:XX:XX:XX:XX")
.build(),
newConnectivityManager.NetworkCallback() {
voidonAvailable(Network network) {
}
});
where XX:XX:XX:XX:XX:XX
is your WiFi SSID. I'm not sure about it's format, and if it's used at all, I did not find any references to setNetworkSpecifier
inside Android sources, except for the NetworkCapabilities
class.
Solution 2:
I found a workaround to enable the desidered network on Lollipop:
WifiConfiguration enable network in Lollipop
Now this is my code after calling wifiManager.enableNetwork(id, true)
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> networks = wm.getConfiguredNetworks();
Iterator<WifiConfiguration> iterator = networks.iterator();
while (iterator.hasNext()) {
WifiConfiguration wifiConfig = iterator.next();
if (wifiConfig.SSID.replace("\"", "").equals(wc.SSID.replace("\"", "")))
wm.enableNetwork(wifiConfig.networkId, true);
else
wm.disableNetwork(wifiConfig.networkId);
}
wm.reconnect();
}
maybe the secret is the call to reconnect()
, I don't know at this time.
UPDATE Unfortunately, this workaround only works if a valid WiFi connection is active before executing the code. Does not works if you're connect by 3G only.
UPDATE 19 Jan 2015
This code actually works for me on Android 5/6.0.x:
//bind to current threadif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManagerconnManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builderrequest=newNetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connManager.registerNetworkCallback(request.build(), newConnectivityManager.NetworkCallback() {
@OverridepublicvoidonAvailable(Network network) {
ConnectivityManager.setProcessDefaultNetwork(network);
//...
}
});
}
Solution 3:
Just need to correctly format the SSID. Here is a sample code:
WifiManager mWifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration tmpConfig = new WifiConfiguration();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
tmpConfig.SSID=String.format("\"%s\"", sSSID);
tmpConfig.BSSID= sBSSID;
} else {
tmpConfig.SSID="\""+ sSSID +"\"";
tmpConfig.BSSID="\""+ sBSSID +"\"";
}
tmpConfig.priority =1;
if (iSecurityType.equals("WEP")) {
tmpConfig.wepKeys[0] = sPassword;
tmpConfig.wepTxKeyIndex =0;
} elseif (iSecurityType.equals("WPA2") || iSecurityType.equals("WPA")) {
tmpConfig.preSharedKey ="\""+ sPassword+"\"";
} else {
tmpConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
tmpConfig.status =WifiConfiguration.Status.ENABLED;
int netId = mWifiManager.addNetwork(tmpConfig);
mWifiManager.updateNetwork(tmpConfig);
boolean result = mWifiManager.enableNetwork(netId, true);
mWifiManager.updateNetwork(tmpConfig);
mWifiManager.saveConfiguration();
Post a Comment for "Android 5.0 Lollipop And 4.4 Kitkat Ignores My Wifi Network, Enablenetwork() Is Useless"