Android accelerometer not working when screen is turned off
Asked Answered
R

6

76

I'm developing an application for my final thesis on computer science, and I need to collect and log accelerometer data. I need to acquire it for a whole day long, so there are serious battery constraints (for instance, I cannot leave the screen on). Also, this isn't a market targeted application, so it is pretty acceptable to do some serious hacking, even low level C/C++ coding, if required.

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

So what happens is this: When you register an event listener for android accelerometer sensor in a Service, it works fine until the screen is turned off. I have already tried to register the eventListener on a Service, on an IntentService, tried to acquire WakeLocks. Regarding wakelocks, I can verify that the service is still running watching the LOGcat output, but it seems the accelerometer is put into sleep mode. One of the workarounds presented in some of the links is to unregister and re-register the event listener periodically using the thread of an IntentService like in this code snippet bellow

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 indeed makes the onSensorChange be called every time we unregister/register the listener. The problem is that the event received contains always the same values, regardless of me shaking the device.

So, basically my questions are: ( bear with me, I'm almost finishing :P )

  1. is it possible to have low level access (C/C++ approach) to the accelerometer hardware WITHOUT registering to an event listener?

  2. is there any other workaround or hack?

  3. could anyone with a more up-to-date phone kindly test if the problem persists in firmware 3.0 and above?

[UPDATE]

Unfortunately, it seems to be a bug with some cellphones. More details in my answer.

Resurrectionist answered 2/4, 2012 at 19:15 Comment(9)
Hi. I just tested the issue with my HTC Sensation (ICS). Both the accelerometer and the magnetic field sensor kept reporting values with the screen off. Also, have you checked the workaround described in comment 46?Sliver
Thank you for the feedback Renard. About the comment 46, I have also tried that, it's almost the same approach like running a thread that periodically unregisters/registers an event listener.. But with my device I only get fixed values for the accelerometer each time I turn off my display.. Also thank you for testing it on ICS... its good to know it works with it like it should, sadly there is no ICS for LG P990 out yet =/Resurrectionist
May be would be useful try to solve the problem from other side - acquire screen lock, but in the same time reduce screen's brightness to zero to save the battery? Sure, it's not work when you turn screen off with power button, but for this case you can try to wake device to prevent screen from locking.Accordant
I recently did accelerometer and magnetometer logging on multiple different model androids and never ran across this problem. My models included a few HTCs and one Samsung Captivate, so I guess it wasn't that broad a selection. But perhaps if this is your thesis it is worth at least trading temporarily phones with your buddy to get your logs. You might also consider running with a DIM screen wakelock and logging less often, putting the phone to sleep for 5 minutes at a time and waking up regularly to grab some samples. You use AlarmManager and BroadcastReciever for this.Gorgonzola
Martin, i think you have answered the question yourself so you should create and accept an answer to your question and put all the findings in it.Sliver
Can you periodically turn the screen on and query the accelerometer? Or does it need to be a constant stream of data? Polling isn't ideal, but might be the only compromise between getting data over a period of time and not killing the battery.Feminize
Is it an option to use a standalone accelerometer for this, such as the ones made by Shimmer Research which send the data over bluetooth? You can even easily write and flash your own firmware onto these: shimmer-research.com I have worked with them myself and they work (and work with Android) --- although you will have to research their battery life.Lashio
@Feminize I cannot query the accelerometer from times to times, I need to constantly measure it. For now, i'll just leave the screen dimmed on, and then perform the battery tests with another cellphone.Resurrectionist
@TomDignan I thought about using a dedicated accelerometer measuring using bluetooth, I even have Arduino/Bluetooth/Accelerometer for that, but for daily usage (eventually this could some day become a product) it's not practical. On top of that, it would also consume more power but then again I would have to check if it consumes less than letting the screen on... So many things to test =P but thanks for the tip!Resurrectionist
R
77

Basically, it is a problem with my phone. Other users have reported this also happens with their phones, from different brands but same Android version. Other persons have no problem at all - strongly indicating that this is not a problem with the stock version of android but from the implementations of each company for their hardware drivers.

I need constant accelerometer data delivered and cannot have a dongle measure this data for me - I have an Arduino with Bluetooth and accelerometer, so I could have implemented this solution. So I decided that the temporary solution for my cellphone was to let the screen on (dimmed) and ignore battery consumption. Later on I will perform the tests for battery usage using another android phone which works with the screen turned off.

More information about the bug

I've researched some more and found reports from other Android users and I think maybe I understand what is happening. The library libsensors.so which has the drivers for the phone sensors is not developed by Google but by each cellphone vendor - of course, because each cellphone has its own specific hardware. Google only provides a C header file so that the developers know what they have to implement. On some implementations for these drivers, the developers simply turn the accelerometer off when the screen goes off, thus preventing the sensor event listener to receive new events.

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

E-mails exchanged with HR department of LG

I sent an e-mail to the developers of the LG P990 and finally got some concrete answers! This may be of great help to some people like me that are experiencing these issues with Android. I wrote the following question

Hello! I am developing my thesis in computer science and currently I am fetching data from accelerometer hardware. As of now, I found out that the accelerometers do not send events when the screen is off, so even when I grab a wakelock from within one of my programs, I can verify that my program is still running (through LOGcat output) but no accelerometer event comes out. I have to dim my screen on (which I cannot afford, the battery drains too fast) to start receiving accelerometer events again. I also tried accessing it through native C code, registering on the accelerometer events but the result was the same, the accelerometer did not throw any values, even though I was rotating my device. So I was wondering if I could have direct access to the hardware, with native code, without having to register to a listener. Is this possible? If so, could you kindly give some further advice? I would appreciate very much any help! Martin

For what I received this response:

Dear Martin, We received the answer from Dev. Team. They said that you can’t get accelerometer event while your phone screen is off. Because HAL layer didn’t implement sysFS path to get H/W event such as accelerometer and there is no public API to get event. Thank you. Best Regards. (Sean Kim)

I then sent an e-mail back, saying among other things, that I considered this a bug, since one should have access to all the hardware when acquiring a wake lock:

[...] I asked this question because I have some friends that also have Android phones with the same gingerbread version but from other cellphone brands, and some of them reported they receive events from the accelerometers when the screen is turned off. I read on some forums that this bug - I consider it a bug, since when I acquire a Wakelock I would expect to have some processing going on - depends on the sensor drivers that the vendors implement for their cellphones. Is there any possibility that these drivers can be updated or will this bug be corrected at some point? This would help me enormously with my ongoing work [...]

And then I received this answer:

In my knowledge from Dev. Team, That isn’t bug. That is a limitless of this phone because of H/W architecture. We need to redesign the HAL architecture and device driver to support your request. But, as you know that is too difficult due to lack of resource. We are trying to help you with our all efforts but we cannot support your request as I mentioned. (Sean Kim)

So they apparently know about this but are not trying to correct this because either they don't think it is a bug - which I still strongly believe is a logical flaw - or they don't have the time/resources to correct it.

Bottom line If you have a cellphone that does not send accelerometer events with the screen off, try updating your firmware. If this does not solve and you really want to do some serious hacking, re implement your hardware layer - hint: it's probably something to do with libsensors.so.

Resurrectionist answered 3/5, 2012 at 23:34 Comment(5)
Thanks martin for this very complete information. What we really need is a list stating which phones support accelerometer events on screen off, because people might want this info before they buy (like me - I need this). Maybe a wiki, people could test their phones and update the list. Is there any list like this anywhere?Craze
OK I made a list: saltwebsites.com/2012/android-accelerometers-screen-offCraze
Amazing and really comprehensive answer. Would give more than one thumbs up :) To a certain extend you can solve the problem with listening to SCREEN_OFF and SCREEN_ON events, acquiring a new wake lock and restarting the accelerometer service. But again, it only works on some phones.Judon
subperb way to ques and AnsArnoldarnoldo
Can jailbreaking remove this bug?Tusk
S
7

I am not sure if this will actually assist you, but I have found a work around (Not sure how well if will help the battery saving though).

Using bash I have a while loop cat-ing /sys/devices/virtual/accelerometer/accelerometer/acc_file, and when I turn off the screen via the power button the output continues, but is frozen. (I had sshd running in a chroot hence the being able to see it.)

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

This is on a SGH-T589W, running android version 2.3.6.

Selfdeceit answered 9/7, 2013 at 19:2 Comment(1)
Huh! Well played, dind't think of bash scripting.. I think I'll try it later, even though I have already finished this work long ago.. Thanks for the input!Resurrectionist
T
4

I applied the PARTIAL_WAKE_LOCK and it worked like charm to me. Tested on OS 5.0, 6.0 & 7.0. Here's my code to acquire and release wake lock. Be careful it increased the battery drainage, so acquire and release it intelligently.

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;
    }
}

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

I am working on a prediction sdk, who use device sensors (accelerometer/gyroscope) data and predicts the user events. I experienced the same issue.

Temperamental answered 21/11, 2016 at 9:10 Comment(0)
A
3

I've ran into similar problems with the Samsung Nexus running Android 4.0.2 using other system services that stop/pause while the screen is off even though a PARTIAL_WAKE_LOCK is acquired. My solution was to use a SCREEN_DIM_WAKE_LOCK as in:

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

It would be far better to have the screen fully off, but at least this solution works although it would be even better if I could limit using a SCREEN_DIM_WAKE_LOCK to only those devices/OSes that require it.

Aelber answered 9/5, 2012 at 18:34 Comment(0)
B
1

I hate to disappoint you, but some devices just don't keep acceleromet on while they are in a sleep mode. Some do, others don't. You can check any pedometer weight loss app on store most of them explicitly state that this won't work on some devices.

Butterbur answered 2/5, 2012 at 8:38 Comment(0)
B
1

If the partial wake lock option is not available for your phone it means the driver for the sensor has early_suspend enabled.

There are two options.

1: Disable EARLY_SUSPEND in the driver

2: Add a run time flag that can enable/disable early_suspend functionality at the driver level.

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

IMO the second option should have been there from the beginning.

Birthday answered 25/4, 2014 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.