Wakelock / wifilock different on Ice Cream Sandwich from Gingerbread? - android

We have an industrial control app that operates in communication with a PC. The app can run on two devices - a Samsung Media Player (like a cellphone without the phone) and a Samsung Galaxy 10.1 tablet.
We had a problem with the CPU and WiFi turning off when the display timed out, which was disrupting communication with the PC. It's OK for the display to go off if the user puts the device down, say, to take a break or go to the bathroom, and it saves battery power, but we want WiFi to still work. So we added . . .
private PowerManager.WakeLock wakelock;
private WifiManager.WifiLock wifilock;
...then, during initialization,
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CPUOnly");
wakelock.acquire();
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifilock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "WifiLock");
wifilock.acquire();
And in the manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
This works on the media player. But the same code on the tablet seems to have no effect - when the display goes out the WiFi also goes out. The media player is running Android 2.3.5 (Gingerbread), and the tablet is running Android 4.0.3 (Ice Cream Sandwich).
Any idea why this would behave differently on these two devices and what we can do to fix it? Thanks in advance!

Related

Bluetooth service optimized, Android

I have an app that uses BLE to exchange data with a BLE-capable device.
I used to develop and test it using Asus Zenfone Max 3 (Android 8.1) and I had no problems.
Then, I got an Asus Zenfone Max Pro M1 (Android 8.1). The app connects to the device but could not exchange any data.
After long investigation, it turns out that I had to go to Battery optimization in the smartphone's settings and change Bluetooth and Bluetooth MIDI Service to Not optimized and then my app worked fine.
I don't know whether this default setting is related to the OS (Android One) or to the phone model. But it is really a crappy thing because I am not supposed to tell every customer to do this (in his phone's settings) in order for the app to work !
So, my question is, is their a way to know, from the code, whether these services are optimized or not, and whether I can change these settings by code or any other ideas that could better fix this issue.
Add this permission to your manifest file:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
and test it like this :
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
reference

NFC goes to sleep mode on Samsung devices

I'm developping an app for Samsung devices with an activity that has to handle a NFC tag..
This activity is called when the app is launched and on resume.
I noticed that, when the phone stays awake with the NFC tag connected to it, after a while the device stops recognizing/scanning for NFC tags.
After trying with some other applications from Play Store, I noticed that this doesn't happen only with my app.
All I can do to make it work again is to switch phone's screen on/off or to switch nfc on/off.
I tried different ways to fix it, as to keep the cpu running, but none of these methods worked :
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
wl.acquire();
and
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The only post about my problem that I found is : onTagDiscovered() not called any more when nfc tag already there after updating from 4.4.4 to 5.1.1 Samsung
Unfortunately, I'm working over non-rooted phones, and I'm not able to get a Samsung Knox licence (unless it is free ?)
Thanks in advance
EDIT
Tried with
setKeepScreenOn(true);
But still no change about this bug

Android audio streaming getting interrupted when lock screen

I'm having a problem with my app when I try to stream musics on background and it only occurs on some devices.
When the app plays any music on foreground it works without problems, but in some devices, when I press the power button, the stream immediately loses its quality (looks like when I'm on a low speed internet connection). When I turn on the screen the stream gets better again.
I've already tried WakeLocks but it didn't work.
Edit 1:
This is how I used the wake locks:
OnCreate of my activity:
//Setting the wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
and than I do this when the music starts:
wl.acquire();
and this when the stream stops:
if(wl.isHeld()){
wl.release();
}
Edit 2:
Tried this as well:
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
Edit 3:
Tried with WiFi Locks too:
//Setting the proper lockMode depending on the android version:
int wifiLockMode = WifiManager.WIFI_MODE_FULL;
int sdkVersion = Build.VERSION.SDK_INT;
//WIFI_MODE_FULL_HIGH_PERF was added on Android 3.1 so
//I need to implement this to make sure the wifi will execute on its full power(even if it consumes more battery)
if (sdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR1) {
wifiLockMode = WifiManager.WIFI_MODE_FULL_HIGH_PERF;
}
//Setting the WifiLock
WiFiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiLock mWiFiLock = wm.createWifiLock(wifiLockMode, "MyFlag");
mWiFiLock.acquire();
//Releasing the WifiLock
if(mWiFiLock.isHeld()){
mWiFiLock.release();
}
With the WifiLock it seems to be a little better(or I'm getting used to the interrupted sound)
Any ideas?
I had the same quality issues with Nexus 4 using android 4.3 and discovered that the problem was solved if I turned off the "wifi optimization": Wi-Fi-> Advanced -> Wi-Fi optimization.
Seems consistent with this other threads:
https://code.google.com/p/android/issues/detail?id=42272#c319
http://forum.xda-developers.com/showthread.php?t=2072930&page=55
If this is true, wifi lock is not preventing the wifi optimization. I also tested that the wake lock is not required to stream quality music with screen turned off, at least with Nexus 4.
I think that you cannot do something really because I think that each OEM rewrite it's "battery optimization" rules that will try to preserve battery when the screen is OFF.
The best thing to do is to follow Android Standard about MediaPlayer's wakelock system and cross your finger :)

How to keep CPU from 'sleeping' when screen is turned off in Android?

I have an application in which I am sending network data over WiFI. Everything is fine until I turn the display off or the device goes to 'sleep'. I'm already locking the WiFi however, it seems to be the case that the CPU speed ramps down when in sleep which causes my streaming to not behave properly (i.e. packets don't flow as fast as I would like as they do when the device is not sleeping).
I know that I possibly can/possibly should address this at the protocol level however, that might possibly not be possible as well...
Is there any way to "prevent the CPU from going to 'sleep' when the screen is off"? If so, how? If not, any advice on how to keep the speed of my WiFi stream consistent whether the device is in sleep mode or not?
Grab a PARTIAL_WAKE_LOCK from the PowerManager. You'll also need to add the WAKE_LOCK permission to your manifest.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
wl.acquire();
//do what you need to do
wl.release();
Okay, so, after much more research and experimenting, it seems that the real issue is the fact that, at least on some phones, their WiFi goes into a 'partial sleep' mode EVEN IF you've taken the WiFi lock. It seems that this is what the 'WIFI_MODE_FULL_HIGH_PERF' flag was invented for when taking the WiFi lock... unfortunately, this flag is only available on some devices/Android versions (I have no clue as to which but, it wasn't available to me). So, therefore, it isn't a fix for all devices.
The only "solution" (which is actually a kludge) seems to be to 'detect when the screen is turned off and then, set an alarm that turns the screen back on immediately thereafter'. The links that helped a little bit with this are:
How to keep a task alive after phone sleeps?
and
http://android.modaco.com/topic/330272-screen-off-wifi-off/
I hope that this helps people who are experiencing WiFi disruption when the phone goes to sleep/screen is turned off (and the phone is unplugged/disconnected [e.g. you won't see this effect when connected to adb; only when the phone is running with nothing connected to it]).

How to receive Multicast packets on Android

I'm trying to receive data from a multicast address, but the call to MulticastSocket.receive() blocks until a timeout takes place.
I did some network sniffing and found out that my device (and the emulator) never send a MulticastSocket.joinGroup request.
I tried running the same Java code from my PC as a standalone application and it worked well. Could it be that the Android platform blocks IGMP join requests?
Has anyone succeeded with Multicast on Android before?
My manifest file contains the following permission:
I am running my application on 2.1 (Both emulator & device).
Any ideas anyone?
Thanks,
Lukas gives the best explanation and examples that I've seen on his blog: http://codeisland.org/2012/udp-multicast-on-android
In summary:
1. You need the permissions:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
2. You need a lock:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("mylock");
lock.acquire();
}
3. You have to have a device that supports receiving multicast. Or you can follow his work around for rooted devices.
As it seems, there is no proper multicast support in the emulator.
Here's a bug report and related thread. It is being fixed for froyo.
You need to do something like this
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null)
{
MulticastLock mcLock = wifi.createMulticastLock("mylock");
mcLock.acquire();
}
Reference:
http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html
I read all 2.1 devices not supporting IGMP stack.
IGMP was missing on different HTC, Samsung, and Motorola devices of all android version from 2.1 up to 3.2.
Link in which i read http://www.programmingmobile.com/2012/01/multicast-and-android-big-headache.html

Categories

Resources