Check for wifi networks signal in sleep mode

℡╲_俬逩灬. 提交于 2019-12-08 05:00:55

问题


My objective is to be able to scan changes in Wi-Fi networks (mainly to check what networks are available). Right know I'm doing it by registering a reciever:

registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));

where WifiReceiver is my BroadcastReceiver.

The problem is that it works only when the phone is on. As soon as it goes to sleep mode (either by pushing the power button or after some time), the receiver stops receiving. I already acquired a wake lock in onCreate of my main class and releasing it in onDestroy (it's a partial wake lock). Additionally I've tried this:

Settings.System.putInt(getContentResolver(),Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);

to keep wifi from sleeping. Unfortunately that didn't help.

Is there any possibility to scan for network changes, even when phone is asleep? I want to be able to check what networks are in range (by SSID). Maybe I should use another action?

Appreciate your help.


回答1:


Literally, no, the hardware isn't typically designed nor the low level drivers written to allow wifi to operate while the application CPU is in sleep mode - unlike the case with the mobile network interface which is intended to receive push events like phone calls, sms, and gmail "tickles".

So your choices are to either manage to successfully prevent the device from sleeping (good bye battery) or wake up periodically using an alarm, kick the wifi active, and poll the situation (still not good for battery life, but not as drastically so).




回答2:


I'm afraid this sleep behaviour is managed by the system under (on Android 2.3.4) Settings > Wireless & Networks > Wi-Fi Settings > Press Menu > Advanced > Wi-Fi Sleep policy.

This could actually go against the users will, careful with that.

However I think there has to be a way to modify this param. programmatically, using this settings : Settings.System, and of course the corresponding permission in your manifest.



来源:https://stackoverflow.com/questions/6778698/check-for-wifi-networks-signal-in-sleep-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!