WifiLock and wakeLock are not working properly on Android

I am developing an application that should use both wifiLock and wakeLock so that audio streaming when the screen is off is not disturbed. I tried my application on Android 2.3 and wakeLock, and it seems that wifiLock works as expected, so there is no difference between the screen turned on or off, but the same application on Android 4.2 (Jellybean) when the screen turns off does not work, and the sound becomes intermittent, which indicates that wakeLock or wifiLock are not working properly. Is there a reason for this?

This is how I acquire and release 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(); } 
+1
source share
4 answers

I just stumbled upon this question - maybe a couple of years is 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 this may cause streaming problems:

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

I would probably look at streaming audio in Service instead of Activity , especially since you seem to be developing it with the idea of ​​working in the background.

+2
source

You acquire a lock in action, and when your activity goes on the stack, the system can delete and destroy your activity, and this may be the cause of your problem. Try to purchase a lock in the service. But the combination of PARTIAL lock and WIFI does not work properly on some devices, and also sees this question: Android WifiLock not working?

0
source

It's too late to post an answer, but maybe it will help someone. Since I ran into the same problem and got a solution like:

  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 the destruction method:

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

In your case, I think you do not have enough locks. See this link .

0
source

How can I turn off wifi at a specific time ??

0
source

Source: https://habr.com/ru/post/1213805/


All Articles