Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 5.0 Lollipop and 4.4 KitKat ignores my WiFi network, enableNetwork() is useless

Tags:

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.

like image 596
Seraphim's Avatar asked Nov 17 '14 21:11

Seraphim's


1 Answers

You can try to use new Lollipop API ConnectivityManager.requestNetwork(), supposedly like this:

ConnectivityManager cm = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE); cm.requestNetwork(new NetworkRequest.Builder()                   .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)                   .setNetworkSpecifier("XX:XX:XX:XX:XX:XX")                   .build(),                   new ConnectivityManager.NetworkCallback() {                       void onAvailable(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.

like image 114
pelya Avatar answered Sep 28 '22 20:09

pelya