简体   繁体   English

指南针在某些设备上不起作用

[英]Compass not working on some devices

We recently released an Android port of an iPhone app. 我们最近发布了iPhone应用程序的Android端口。 The app relies on the digital compass for some features. 该应用程序依赖于数字指南针的某些功能。 This has been working well on most devices, but there are a few where the compass is not working. 这在大多数设备上都运行良好,但是在某些情况下指南针无法正常工作。 For the description it seems the device is not feeding us any onSensorChanged() messages. 对于说明,设备似乎没有向我们提供任何onSensorChanged()消息。

A couple of devices that seem problematic are: HTC Desire on Android 2.2 Motorola Droid on Android 2.2.3 几个似乎有问题的设备是:Android 2.2上的HTC Desire Android 2.2.3上的摩托罗拉Droid

They claim that other apps that make use of the compass work fine. 他们声称其他使用指南针的应用程序可以正常工作。

We start the compass using: 我们使用以下命令启动指南针:

    if ( magnetometer != null && accelerometer != null )
    {
        if ( compassOn )
        {
            sensorManager.registerListener( this, accelerometer, (int) (1000000 / kSensorUpdateFrequencyFast) );
            sensorManager.registerListener( this, magnetometer, (int) (1000000 / kSensorUpdateFrequencyFast) );
        }
        else
        {
            sensorManager.registerListener( this, accelerometer, (int) (1000000 / kSensorUpdateFrequencySlow) );
            sensorManager.registerListener( this, magnetometer, (int) (1000000 / kSensorUpdateFrequencySlow) );
        }
    }

Where the magnetometer and accelerometer are initialized at program startup using: 在程序启动时使用以下命令初始化磁力计和加速度计的地方:

    sensorManager = (SensorManager) getSystemService( SENSOR_SERVICE );
    accelerometer = sensorManager.getDefaultSensor( Sensor.TYPE_ACCELEROMETER );
    magnetometer = sensorManager.getDefaultSensor( Sensor.TYPE_MAGNETIC_FIELD );

Now the above code indicated that registration doesn't happen if either accelerometer == null or magnetometer == null. 现在,上面的代码表明,如果加速度计== null或磁力计== null,则不会发生配准。 I'm pretty sure the magnetometer is not null because if it is we hide the compass feature. 我很确定磁力计不是null,因为如果是,我们将隐藏指南针功能。 So maybe the accelerometer is null on these phones? 因此,这些手机上的加速度计可能为空吗?

The code in my onSensorChanged() is pretty standard and is based on the countless examples you can find on the web. 我的onSensorChanged()中的代码非常标准,并且基于您可以在网上找到的无数示例。

So does anyone have any thoughts on why the compass code might not be working on those devices? 那么,有人对罗盘代码为何在这些设备上不起作用有任何想法吗?

I did figure this out. 我确实知道了。 Turns out that in 2.2 you can't use sensor rates other than the standard SENSOR_DELAY_UI, SENSOR_DELAY_NORMAL, etc. The documentation claims you can also specify the number of microseconds between notifications, but they are lying. 事实证明,在2.2中,除了标准SENSOR_DELAY_UI,SENSOR_DELAY_NORMAL等之外,您不能使用其他传感器速率。文档声称您还可以指定两次通知之间的微秒数,但它们是在撒谎。

Once I used the standard constants it started working on 2.2 一旦我使用了标准常量,它就开始在2.2上运行

You've not explained what it means, that compass is not working. 您没有解释这意味着什么,指南针不起作用。

If you don't get valid data, it may be well known problem with this devices. 如果您没有获得有效的数据,则可能是此设备存在的众所周知的问题。 With mine, I need always roll it in hands (both horizontally and vertically) to unlock compass. 对于我的,我需要始终将其滚动(水平和垂直)在手中以解锁罗盘。 Some apps call it calibration but for me it is something absurd... I have HTC Desire. 有些应用程序将其称为“ 校准”,但对我来说这是荒谬的……我有HTC Desire。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM