1

I am using Samsung Galaxy with ICS.

I am developing a SIP client. the client loses connection with the server once the phone goes in sleep mode. Have tried the "WIFI Lock" app from android market and have tried setting WIFI_SLEEP_POLICY_NEVER but no success so far. Have anybody solved this kind of connectivity issue programatically in the past? I need to have internet connectivity on. Bet it mobile data / WIFI because I cannot afford to lose the connection with the server.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
BTR Naidu
  • 1,063
  • 3
  • 18
  • 45
  • 2
    I know this is really old, but did you ever solve this issue? – Jeremy K Feb 28 '14 at 20:11
  • It was not an android issue. There was one sleep in one of our thread which was causing the the LOCK to be released. I vaguely remember the exact solution though. – BTR Naidu Nov 07 '16 at 16:55

3 Answers3

1

Make sure that in your manifest you have the permission:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

It will be used to set the Wi-Fi sleep policy to never sleep while connected to a trusted network.

http://developer.android.com/reference/android/Manifest.permission.html

Then you only need to do this:

Settings.System.putInt(getContentResolver(),
                Settings.System.WIFI_SLEEP_POLICY, 
            Settings.System.WIFI_SLEEP_POLICY_NEVER);
Filipe Batista
  • 1,862
  • 2
  • 24
  • 39
  • did you have the permission in your manifest? I'm saying this because i've tested this code and its working... – Filipe Batista Jul 16 '12 at 13:45
  • What is the result of the setting when you do this: `android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT)` – Filipe Batista Jul 16 '12 at 13:53
  • There is no error. The android.provider.Settings.System.getInt returns true and app behaves as it was before. Connectivity lost after phone sleeps. – BTR Naidu Jul 16 '12 at 14:10
  • returns true? it should return an integer... The method above is `getInt` not `putInt`. It should return what is setting defined. – Filipe Batista Jul 16 '12 at 14:14
  • Apologies, overlooked the get part. I called it after setInt and the value returned is 2. – BTR Naidu Jul 16 '12 at 14:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13946/discussion-between-btr-naidu-and-filipe-batista) – BTR Naidu Jul 16 '12 at 14:24
0

Try going to the Wifi settings, pressing the menu button and selecting advanced. There you'll find an item to manage the wifi sleep policy.
Also, if the WIFI_SLEEP_POLICY_NEVER is not working, check out if you have permission for that on http://developer.android.com/reference/android/Manifest.permission.html. I dunno which one you should use, maybe WRITE_SYNC_SETTINGS, ACCESS_WIFI_STATE, CHANGE_NETWORK_STATE, CHANGE_WIFI_MULTICAST_STATE or CHANGE_WIFI_STATE.

mvitsor
  • 93
  • 1
  • 8
  • In Wifi settings, the sleep policy is already set to NEVER. I have tried WIFI_SLEEP_POLICY_NEVER with WRITE_SETTINGS & WRITE_SECURE_SETTINGS and printed the return of Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER); which returns true. Not sure if the return has any significance here. – BTR Naidu Jul 16 '12 at 13:42
0

I found this SO post apparently there are numerous bugs about that on the Android Bug Tracker which might occurs even with Locks and permissions.


In my case, I manage to make the Lock work with all the device I can test on. Not sure how you tried to implement it, so here is a version I found over internet (Sorry if I cannot find back the author).

Note: my version may slightly differs with the Java version since I am using Xamarin Android in C#, but the logic should remain the same.

First you have to add the permission for locking the Wifi

<uses-permission android:name="android.permission.WAKE_LOCK" />

Second you have to get the wifiManager

wifiManager = (WifiManager) GetSystemService(WifiService);

Then you must write code to aquire and release the lock at the proper time.

private void AquireWifiLock() 
{
    if (wifiLock == null) 
    {
        wifiLock = wifiManager.CreateWifiLock(WifiMode.Full, "aTagForYourLock");
        wifiLock .Acquire();
    }
}


private void ReleaseWifiLock() 
{
    if (wifiLock == null) 
    {
        return;
    }

    wifiLock .Release();
    wifiLock = null;
}
Community
  • 1
  • 1
ForceMagic
  • 6,230
  • 12
  • 66
  • 88