Hi,
My app relies heavily on having a compass bearing (it's an open source app
at https://github.com/ShowMeHills/ShowMeHills). Most of the devices work
ok, but on one of my devices I don't get any events from the magnetic field
sensor.
The device is an Xperia Arc (Lt15i), Android 2.3.4. Quite possibly the
sensor is broken as after googling I've found no-one else seeing the
problem. So I didn't worry about it too much initially.
However I'm seeing users of other phones reporting that the app doesn't
work while other compass apps do, which obviously makes my app look
terrible (an HTC esspresso phone apparently). I'm assuming either the other
apps are falling back to using the deprecated Orientation sensor, or I've
missed something subtle which most phones are not worrying about (but some
do).
As far as I know I don't think I'm doing anything wrong - my main activity
class sets up the listener with;
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer =
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer =
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, magnetometer,
SensorManager.SENSOR_DELAY_GAME);
(unregistering and reregistering in pause/resume), then has the listener
which is like;
public void onSensorChanged(SensorEvent event) {
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
return;
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity
= event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float[] rotationMatrixA = mRotationMatrixA;
if (SensorManager.getRotationMatrix(rotationMatrixA, null,
mGravity, mGeomagnetic)) {
do my compass bearing calculation...
}
The problem is that I never get a magnetic field event (only accelerometer
events), so never get to do my bearing calculation. I assume I can't do
SensorManager.getRotationMatrix without BOTH of those values? Otherwise I
think that's all normal stuff.
However, the phone's TYPE_ORIENTATION events do work. The trouble is I'm
confused as to how to transform the coordinates so that I get a compass
bearing in the direction of the camera (ie handset isn't flat, it's held up
while looking at the view - landscape mode). I've managed this with the
accelerator/magnetic matrices, but then there were lots of help online for
that - I've not found any examples of transposing the orientation matrix.
Plus it's deprecated, so I doubt it's the smart thing to fall back to.
So has anyone any ideas why some phones don't fire magnetic sensor events
(which is broken - my code or my phone)? Or failing that a snippet of code
to orient;
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
float azimuth_angle = event.values[0];
float pitch_angle = event.values[1];
float roll_angle = event.values[2];
..bearing = ?
into a compass bearing when the handset is orientated as
Surface.ROTATION_90 (landscape, camera facing away from you looking at the
view).
Thanks,
Nik
--
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