Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wifiLock and wakeLock not working correctly on Android

I am developing an app that needs to use both wifiLock and wakeLock so the audio streaming when the screen is off does not get disturbed. I have tried my app on Android 2.3 and wakeLock and looks like wifiLock work as expected so there is no difference between when the screen is on or off, but the same app on Android 4.2 (Jelly-bean) when the screen goes off is not working as well and the audio gets choppy which shows either wakeLock or wifiLock are not working correctly. Is there any reason for this?

Here is how I acquire and release the locks, in my main activity I have:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyWirelessHeadphone");

    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
    ...
}



@Override
protected void onDestroy() {
    super.onDestroy();
    if (wakeLock.isHeld()==true) 
        wakeLock.release();
    if (wifiLock.isHeld()==true) 
        wifiLock.release();
}   
like image 316
TJ1 Avatar asked Feb 16 '13 15:02

TJ1


2 Answers

I only just stumbled across this question - probably a couple of years too late for the OP, but just in case it helps someone else, you probably need to use WIFI_MODE_FULL_HIGH_PERF instead of WIFI_MODE_FULL, as that can cause streaming issues:

wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "MyWifiLock");

I'd probably look at streaming the audio in a Service instead of an Activity too, especially as you appear to be designing it with the idea of running in the background.

like image 190
HexAndBugs Avatar answered Oct 04 '22 04:10

HexAndBugs


It is so late to post the answer but maybe it helps someone. As i faced the same problem and got the solution as :

        WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "MyWifiLock");
        wifiLock.acquire();
        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();

And release the locks in destroy method:

if (wakeLock != null) {
            if (wakeLock.isHeld()) {
                wakeLock.release();
            }
        }
        if (wifiLock != null) {
            if (wifiLock.isHeld()) {
                wifiLock.release();
            }
        }

What i think is missing in your case is you're not acquiring locks. See this link.

like image 30
user434 Avatar answered Oct 04 '22 05:10

user434