My app connect directly to a hardware device that act as an access point (with no access to internet).
I can't connect because Android 5.0 automaticcaly switch to a valid internet connection, so if I have data connection (3G, 4G, ...) or a pre-configured network I can't connect to my device because it ignores the WiFi.
So how can I force Android to use the network I activated programatically?
I'm simply using:
wifiManager.enableNetwork(id, true))
where id
is the network of my device I want to connect to. The true
parameter is useless.
The suggested solution that use ConnectivityManager.requestNetwork()
has no effect.
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 thread
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
ConnectivityManager.setProcessDefaultNetwork(network);
//...
}
});
}