Android Accelerometer does not work when screen is off

I am developing an application for my last thesis on computer science, and I need to collect and register accelerometer data. I need to purchase it all day, so there are serious battery limitations (for example, I can’t leave the screen on). In addition, this is not a market-oriented application, therefore it is perfectly acceptable to commit serious hacker and even low-level C / C ++ codes if necessary.

It is well known that on many devices, accelerometer event listeners stop generating events when the screen turns off (some links to this problem: http://code.google.com/p/android/issues/detail?id=3708 , Accelerometer stops sample delivery, when the screen is off on the Droid / Nexus One even with WakeLock ). I carefully searched for some alternatives, some of them include workarounds that do not work on my device (LG P990, storage ROM).

So what happens: When you register an event listener for the android accelerometer sensor in the Service, it works fine until the screen turns off. I already tried to register eventListener on the service, in IntentService, tried to get WakeLocks. As for wakelocks, I can verify that the service is still running, watching the LOGcat exit, but it seems that the accelerometer is in sleep mode. One workaround provided in some links is to unregister and re-register the event listener periodically using the IntentService stream, as in this code snippet below.

synchronized private static PowerManager.WakeLock getLock(Context context) { if (lockStatic==null) { PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE); lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,NAME); lockStatic.setReferenceCounted(true); } return(lockStatic); } @Override protected void onHandleIntent(Intent intent) { sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE); sensorManager.unregisterListener(this); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); synchronized (this) { boolean run = true; while (run){ try { wait(1000); getLock(AccelerometerService.this).acquire(); sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE); sensorManager.unregisterListener(this); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); Log.d("Accelerometer service", "tick!"); } catch (Exception e) { run = false; Log.d("Accelerometer service", "interrupted; cause: " + e.getMessage()); } } } } @Override public void onSensorChanged(SensorEvent event) { Log.d("accelerometer event received", "xyz: "+ event.values[0] + "," + event.values[1] + "," + event.values[2]); } 

which really makes onSensorChange called every time we unregister / register the listener. The problem is that the received event always contains the same values, regardless of how I shake the device.

So, basically, my questions are: (bear with me, I almost finish: P)

  • Is it possible to access low-level access (C / C ++ approach) to the accelerometer apparatus WITHOUT registering with an event listener?

  • Is there any other workaround or hack?

  • can someone with a more modern phone pass the test if the problem persists in firmware 3.0 and higher?

[UPDATE]

Unfortunately, this seems like a bug with some cell phones. More details in my answer.

+62
android screen accelerometer sensor
Apr 02 '12 at 19:15
source share
6 answers

Basically, this is a problem with my phone. Other users report that this also happens with their phones, from different brands, but with the same version of Android. Other people have no problems - this convincingly indicates that this is not a problem with the version of Android, but with the implementation of each company for their hardware drivers.

I need constant accelerometer data, and I cannot measure this data for me - I have an Arduino with Bluetooth and an accelerometer, so I could implement this solution. Therefore, I decided that a workaround for my mobile phone was to allow the screen (darkened) and ignore the battery consumption. Later I will conduct battery tests using another Android phone that works with the screen off.

Additional error information

I researched a few more and found reports from other Android users, and I think maybe I understand what is going on. The libsensors.so library, which has drivers for telephone sensors, was not developed by Google, but by every mobile phone manufacturer - of course, because each cell phone has its own specific equipment. Google provides only the C header file so that developers know what they need to implement. In some implementations of these drivers, developers simply turn off the accelerometer when the screen turns off, thereby preventing the receiver of sensor events from receiving new events.

I also tested this with CyanogenMod RC7.2, but it did not work either, because the accelerometer drivers are original from LG.

Emails exchanged with HR LG

I sent an email to the LG P990 developers and finally got some specific answers! This can be of great help to some people like me who are experiencing these problems with Android. I wrote the following question

Hello! I am developing a dissertation in computer science, and currently I am receiving data from accelerometer equipment. At the moment, I found out that accelerometers do not send events when the screen is off, so even when I grab wakelock from one of my programs, I can make sure that my program is still working (via the LOGcat output), but there is no accelerometer happening. I have to fade my screen (which I cannot afford, the battery drains too fast) to start receiving accelerometers. I also tried to access it through my own C code, registering on the events of the accelerometer, but the result was the same, the accelerometer did not give out any values, although I was turning my device. So I was wondering if I could have direct access to hardware, with my own code, without having to register with a listener. Is it possible? If so, could you give some advice? I would really appreciate any help! Martin

For which I received this answer:

Dear Martin, We have received a response from Dev. Team. They said that you cannot get the accelerometer when the phone screen is off. Because the HAL layer did not implement the sysFS path for receiving an H / W event, such as an accelerometer, and there is no open API for receiving an event. Thank. Best Regards. (Sean Kim)

Then I sent the email back, saying, among other things, that I considered this a mistake, since you should have access to all the hardware when purchasing a tracking lock:

[...] I asked this question because I have friends who are also Android phones with the same version of gingerbread, but from other brands of mobile phones, and some of them reported that they receive events from accelerometers when the screen is off. I read about some forums that this error - I consider this a mistake, because when I purchase Wakelock, I would expect the processing to continue - it depends on the sensor drivers that manufacturers implement for their mobile phones. Is it likely that these drivers can be updated or will this error be fixed at some point? It would help me a lot with my current job [...]

And then I got this answer:

In my knowledge from Dev. Team, this is not a mistake. This phone is limitless due to the H / W architecture. We need to reverse engineer the HAL architecture and device driver to support your request. But, as you know, this is too complicated due to lack of resources. We are trying to help you with all our efforts, but we cannot support your request, as I mentioned. (Sean Kim)

Thus, they obviously know about it, but they don’t try to fix it because either they don’t think that this is a mistake - I still believe that this is a logical flaw - or they don’t have the time / resources to fix it.

Bottom line If you have a mobile phone that does not send accelerometer events with the screen off, try updating the firmware. If this does not solve, and you really want to make a serious hack, repeat your hardware level - tell me: maybe this is something related to libsensors.so.

+61
May 3 '12 at 23:34
source share

I'm not sure if this will really help you, but I found a job (not sure how good it would be if it helps to conserve battery).

Using bash I have a while cat /sys/devices/virtual/accelerometer/accelerometer/acc_file , and when I turn off the screen using the power button, the output continues but is frozen. (I had sshd working in chroot, hence the ability to see it.)

However, echoing 0 > /sys/devices/platform/msm_fb.196609/leds/lcd-backlight/brightness . The screen will turn off and the output will be continuous.

This is the SGH-T589W version, version 2.3.6 for Android.

+6
Jul 09 '13 at 19:02
source share

I encountered similar problems with Samsung Nexus running Android 4.0.2, using other system services that stop / pause while the screen is turned off, even if PARTIAL_WAKE_LOCK purchased. My solution was to use SCREEN_DIM_WAKE_LOCK , as in:

  lockStatic = mgr.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,NAME); 

It would be much better to turn off the screen, but at least this solution works, although it would be even better if I could limit the use of SCREEN_DIM_WAKE_LOCK only those devices / operating systems that require it.

+2
May 9 '12 at 18:34
source share

I applied PARTIAL_WAKE_LOCK and it worked like a charm for me. Tested on OS 5.0, 6.0 and 7.0. Here is my code to acquire and unlock a lock. Be careful, this increases drainage of the battery, so purchase and dispose of it wisely.

 public void acquireWakeLock() { final PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); releaseWakeLock(); //Acquire new wake lock mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK"); mWakeLock.acquire(); } public void releaseWakeLock() { if (mWakeLock != null && mWakeLock.isHeld()) { mWakeLock.release(); mWakeLock = null; } } 

Link: https://developer.android.com/training/scheduling/wakelock.html#cpu

I am working on sdk prediction, which uses device sensor data (accelerometer / gyroscope) and predicts user events. I experienced the same problem.

+2
Nov 21 '16 at 9:10
source share

I hate to disappoint you, but some devices simply do not support the accelerometer when they are in sleep mode. Some do, others do not. You can check any application to reduce the weight of the pedometer in the store, most of which clearly indicate that this will not work on some devices.

0
May 2 '12 at 8:38
source share

If the option to partially block the trace is not available for your phone, this means that the driver for the sensor has early_suspend .

There are two options.

1: disable early_suspend in the driver

2: add a run-time flag that can enable / disable early_suspend to work at the driver level.

ex. cat / sys / module / earlysuspend / sensor 1/0

IMO the second option was supposed to be from the very beginning.

0
Apr 25 '14 at 6:29
source share



All Articles