Android drops WiFi after connecting programmatically
Asked Answered
T

0

6

I am experiencing a strange behavior in my Android app: Every time I connect programmatically to a WiFi network (source see below), first it works as intended, but after a couple of seconds (between 5 and around a minute) the connection is dropped and the device tries to connect to another network.

If I, however, connect to the same network manually (i.e. through Android's Settings App), everything works as intended and the device stays connected to the AP (I have cleared the WifiConfiguration-list, so there is only occurrence of the desired SSID).

Here is the relevant part of the code I use to connect to the network:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
String ssid = "NameOfAP";

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
    if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
        wifiManager.disconnect();
        wifiManager.enableNetwork(i.networkId, true);
        wifiManager.reconnect();
        break;
    }
}

Some additional info:

  • Lenovo Yoga Tablet 2 with Android 5.0.1 (since it is an in-house app which only runs on this device)
  • The network is not connected to the internet (though the captive portal detection flag is set to disabled/0)
  • I have tried to look at Android's code in Lollipop, but they are using wifiManager.connect(), which I somehow cannot access.

Any help appreciated!

Technicality answered 5/1, 2017 at 15:19 Comment(1)
You have to use ConnectivityManager's RegisterNetworkCallback to bind the network since Lollipop. in OnAvailable do manager.BindProcessToNetwork(network); intentfilter.com/2016/08/…Boland

© 2022 - 2024 — McMap. All rights reserved.