SalutonMondo
SalutonMondo

Reputation: 709

Android connect to Wi-Fi AP when screen is off

I'm working on a project which need to connect to a Wi-Fi in a background service, the service running when the device is screen off. The connecting code is like below:

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

while after the connecting code is executed in the background running service, the device never connect to the Wi-Fi successfully until the screen is turned on. I logged the supplicant state, it is in complete state. As the google docs says:

This state indicates that the supplicant has completed its processing for the association phase and that data connection is fully configured. Note, however, that there may not be any IP address associated with the connection yet. Typically, a DHCP request needs to be sent at this point to obtain an address.

So can i come to the conclusion that, when the screen is turned on, a DHCP request is send the device receives an IP and the connection is complete successfully. But who is in charge of sending the DHCP, the wpa_supplicant or Android framework, is there any docs about this? How can i connect to a Wi-Fi AP without turn on the screen? Thanks in advance!

Upvotes: 0

Views: 887

Answers (1)

Nguyễn Anh Quế
Nguyễn Anh Quế

Reputation: 243

Maybe because your device use feature turn of wifi when screen of (Settings -> Wireless and network -> WiFi settings -> (menu button) Advanced -> Wifi sleep policy) Your app will cannot connect to network

Upvotes: 1

Related Questions