当关闭屏幕的Andr​​oid加速度计无法正常工作 [英] Android accelerometer not working when screen is turned off

查看:211
本文介绍了当关闭屏幕的Andr​​oid加速度计无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展我的计算机科学毕业论文的应用程序,而我需要收集和记录加速度数据。我需要获得它一整天长,所以存在着严重的电池的限制(例如,我不能离开屏幕)。此外,这是不是市场有针对性的应用程序,所以它是pretty的可接受做一些严重的黑客攻击,即使是低级别的C / C ++编码,如果需要的话。

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.

众所周知,在许多设备侦听器的加速度事件停止生成事件时,屏幕熄灭(关于这个问题的一些链接:<一href="http://$c$c.google.com/p/android/issues/detail?id=3708">http://$c$c.google.com/p/android/issues/detail?id=3708 ,<一个href="http://stackoverflow.com/questions/2143102/accelerometer-stops-delivering-samples-when-the-screen-is-off-on-droid-nexus-one">Accelerometer停止提供样品时,屏幕关闭上的Droid / Nexus One的甚至带有WakeLock )。我已经彻底搜查了一些替代品,其中一些包括解决方法不为我的设备(LG P990,股票ROM)。工作

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).

那么,什么情况是这样的: 当您注册一个事件监听器用于在服务机器人加速度计传感器,它工作正常,直到屏幕关闭。我已经尝试注册一个服务的事件监听,在一个IntentService,试图收购WakeLocks。关于wakelocks,我可以验证该服务仍在运行看logcat的输出,但似乎在加速进入休眠模式。一个psented在某些环节的解决方法$ P $的是注销并重新注册定期使用IntentService的线程想在这个code段的事件侦听器波纹管

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

这的确使得onSensorChange叫我们每次注销时间/注册的监听器。问题是,该事件接收到的包含箱的总是相同的值,而不管摇动设备

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.

所以,基本上我的问题是:(忍耐一下,我几乎完成:P)

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

  1. 是有可能有低级别的访问(C / C ++的方式),以加速计硬件,而无需注册到一个事件监听器?

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

还有没有其他的解决办法或破解?

is there any other workaround or hack?

任何人都可以有一个更切合最新手机好心测试,如果问题仍然存在固件3.0及以上?

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

[更新]

不幸的是,这似乎是与一些手机的bug。更多细节在我的答案。

推荐答案

基本上,这是我的手机有问题。其他一些用户报告这也正好与自己的手机,来自不同的品牌,但相同的Andr​​oid版本。其他人都没有任何问题 - 强烈表明这是不符合股票版本的Andr​​oid的一个问题,但各公司实现他们的硬件驱动程序

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.

我需要提供恒定的加速度数据,并不能有一个加密狗测量这个数据,我 - 我有一个Arduino带有蓝牙和加速度计,所以我可以实现这个解决方案。所以,我决定,我的手机的临时解决方案是让上(灰色)屏幕,并忽略电池消耗。稍后我会执行使用另一个android手机其工作原理与屏幕关闭的测试电池使用。

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.

有关错误更多信息

我研究过一些,发现从其他Android用户的报告,我想也许我明白发生了什么。图书馆libsensors.so它具有驱动手机传感器没有开发的谷歌,而是由每个手机厂商 - 当然,因为每个手机都有其特定的硬件。谷歌只提供了一个C头文件,以便开发人员知道他们必须实现。在一些实现这些驱动程序,开发人员只需打开加速时关闭屏幕熄灭,因此preventing传感器事件侦听器来接收新的事件。

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.

我还测试了这款具有的CyanogenMod RC7.2,但没有任何工作,因为加速度传感器的驱动程序原装LG。

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

电子邮件交换与LG的人力资源管理部门

我发了一封邮件到LG P990的开发商终于得到了一些具体的答案!这可能是很大的帮助,有些人喜欢我,遇到这些问题,与Android。我写了下面的问题

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

您好!我发展我的论文在计算机科学,目前我   我从加速硬件获取数据。截至目前,我发现   该加速度计不发送事件,当屏幕是关闭的,所以   甚至当我抢到wakelock从内部的我的程序之一,我可以   验证我的程序仍在运行(通过logcat的输出),但没有   加速度计事件出来。我有我的调暗屏幕上(我   买不起,电池消耗过快)开始接收   加速度计的事件了。我也试图通过本机C访问它   code,登记在加速事件,但结果却是   同样,加速度计没扔任何值,即使我   转动我的设备。所以我在想,如果我能直接访问   硬件,与当地code,无需注册到   侦听器。这可能吗?如果是这样,你能请给一些进一步的   建议?我想AP preciate非常多的帮助!马丁

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:

亲爱的马丁,我们从开发得到的回答。球队。他们说你   不能得到加速度计的事件,而你的手机屏幕是关闭的。因为   HAL层没有实行SYSFS路径来获得H / W事件,如   加速度计和没有公开的API获取事件。谢谢。最佳   问候。 (肖恩金)

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:

[...]我问这个问题,因为我有一些朋友说,也有   Android手机使用相同的姜饼版本,但是从其他   手机品牌,其中一些报告说,他们收到来自事件   加速度计,当屏幕被关闭。我读了一些   我认为这是一个错误,当我取得,因为 - 这个错误的论坛   Wakelock我希望有一些处理事情 - 取决于   传感器驱动程序的供应商实现了他们的手机。是   有没有可能性,这些驱动程序可以更新或将在   臭虫在某些时候得到纠正?这将帮助我极大我   正在进行的工作[...]

[...] 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:

在从开发我的知识。团队,这不是错误。这是一个无限   这款手机由于H / W结构。我们需要重新设计的HAL   建筑和设备驱动程序来支持您的要求。但是,正如你   知道那是太困难了,由于缺乏资源。我们正在努力   帮助您与我们所有的努力,但我们不能支持你的要求,因为我   提及。 (肖恩金)

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.

底线 如果您有不发送加速度事件与屏幕掀起了手机,尝试更新固件。如果这没有解决,你真的想要做一些严重的黑客攻击,重新实现你的硬件层 - 提示:它可能是与libsensors.so

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.

这篇关于当关闭屏幕的Andr​​oid加速度计无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆