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 Android devices, but there are a few where the compass is not
working. From the descriptions from the users it seems the device is
not feeding us any onSensorChanged() messages.  It is *not* that the
direction is just off, but rather the live display doesn't change at
all as you move the phone around.

A couple of devices that seem problematic are: HTC Desire on Android
2.2 Motorola Droid on Android 2.2.3.  In fact all our problems seem to
be with 2.2.x devices.

They claim that other apps that make use of the compass seem to work
just 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 is such 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 certain 2.2 devices?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to