Safecoder
Safecoder

Reputation: 1035

How to reconnect WiFi that is currently in sleep mode on Android

I have an Android application that wakes up each night and sync some data with our server. If the device put the WiFi in sleep mode after certain time of no use (or screen off), I need a way to wake up Wifi and do the sync and then allow it to return to sleep mode. So here is what I find out:

If the Wifi is in sleep mode, I can see that (by dumpsys) wifi is still enabled, but runState is stopped. So in my code, I would do something like

    WifiManager wManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
wManager.setWifiEnabled(true); 
    boolean connect = wManager.reconnect(); 
    Log.d(TAG, "connect " + connect);

I don't think setWifiEnabled(true) is necessary because it is still enabled while in sleep. But I did it anyways and there is no harm. But reconnect always return false, meaning that it cannot reconnect the wifi. And of course, dumpsys still shows that runState is Stopped.

Can someone please help me on this issue? What's the right procedure to wake up Wifi and get a working connection?

Thanks in advance!

Upvotes: 3

Views: 2726

Answers (1)

TacB0sS
TacB0sS

Reputation: 10266

Normally when a device is in sleep mode the only way to really wake it up is to acquire a wake lock... there are a few type of wake locks, you can read about it here...

Hope this helps...

Upvotes: 0

Related Questions