Android Question [RESOLVED] Android 12 - WIFI_SLEEP_POLICY_NEVER

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi All

Has anyone come up with a clever way to overcome WIFI_SLEEP_POLICY_NEVER (this constant was deprecated in API level 30. This is no longer used by the platform.
https://developer.android.com/reference/android/provider/Settings.Global#WIFI_SLEEP_POLICY_NEVER). We have a requirement to have an always on connection to a server app, it's used a server socket, RAW TCP. Under Android 12+ the wifi connection seems to be put to sleep. The app will re-establish the connection, but the frequency is random when the phone sleeps the WIFI.

We have also noticed that other apps (not ours) are show delays receiving notifications, one be a Door Bell app in our office.

Views and opinion's appreciated.

Regards

John.
 
Solution
I resolved this issue:
1. The server ignores the fact that it did not receive the ping when expected, and keep the connection open.
2. As part of the client connection class on the server there is a health timer and ticks every 10 seconds an sends a single byte to the connection
3. If point 2 fails then I know the connection is dead, and I close it.

This was implemented this morning and seems to be working quite well.

Computersmith64

Well-Known Member
Licensed User
Longtime User
The issue isn't related to your Wifi connection, it's related to the battery usage setting for the particular app. If you go to the device settings App Info for the particular app you'll see the "Battery usage" item. Tap on that & you can set either Unrestricted, Optimized or Restricted. The problem is, you can't change this setting in code. The only thing you can do is check the current setting using ActivityManager.isBackgroundRestricted() & then pop up a dialog asking the user to change it if it's not already set.

It's all part of Android's attempts to prolong battery life on devices...

- Colin.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I resolved this issue:
1. The server ignores the fact that it did not receive the ping when expected, and keep the connection open.
2. As part of the client connection class on the server there is a health timer and ticks every 10 seconds an sends a single byte to the connection
3. If point 2 fails then I know the connection is dead, and I close it.

This was implemented this morning and seems to be working quite well.
 
Upvote 0
Solution
Top