指南针在某些设备上无法工作

发布于 2024-12-23 02:24:54 字数 1384 浏览 1 评论 0原文

我们最近发布了 iPhone 应用程序的 Android 版本。该应用程序的某些功能依赖于数字指南针。这在大多数设备上都运行良好,但也有一些设备无法使用指南针。对于描述,设备似乎没有向我们提供任何 onSensorChanged() 消息。

一些看起来有问题的设备是: Android 2.2 上的 HTC Desire Android 2.2.3 上的 Motorola Droid

他们声称其他使用指南针的应用程序运行良好。

我们使用以下方法启动罗盘:

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

其中磁力计和加速度计在程序启动时使用以下方法进行初始化:

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

现在上面的代码表明,如果加速度计 == null 或磁力计 == null,则不会发生注册。我很确定磁力计不为空,因为如果是,我们会隐藏指南针功能。那么这些手机上的加速度计可能是空的吗?

我的 onSensorChanged() 中的代码非常标准,并且基于您可以在网络上找到的无数示例。

那么有人知道为什么指南针代码可能无法在这些设备上运行吗?

We recently released an Android port of an iPhone app. 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.

A couple of devices that seem problematic are:
HTC Desire on Android 2.2
Motorola Droid on Android 2.2.3

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. I'm pretty sure the magnetometer is not null because if it is we hide the compass feature. 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.

So does anyone have any thoughts on why the compass code might not be working on those devices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-12-30 02:24:54

我确实弄清楚了这一点。事实证明,在 2.2 中,除了标准 SENSOR_DELAY_UI、SENSOR_DELAY_NORMAL 等之外,您不能使用传感器速率。文档声称您还可以指定通知之间的微秒数,但他们在撒谎。

一旦我使用了标准常量,它就开始在 2.2 上工作

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.

Once I used the standard constants it started working on 2.2

折戟 2024-12-30 02:24:54

你还没有解释这意味着什么,指南针不起作用。

如果您没有获得有效数据,则可能是该设备存在众所周知的问题。对于我的,我需要始终在手中滚动(水平和垂直)以解锁指南针。有些应用程序称之为校准,但对我来说这是荒谬的......我有 HTC Desire。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文