Android magnetic sensor

984 Views Asked by At

I'm developing a compass for Android OS. When I use TYPE_MAGNETIC_FIELD, some devices have this sensor, some devices don't. I don't know how to handle this problem. Should I not support these devices or use another sensor approach. Can you give me an advice?

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            Log.d(TAG+" Magentic", String.valueOf(event.values[0]));
            mGeomagnetic[0] = mGeomagnetic[0] + alpha_mag*(event.values[0]-mGeomagnetic[0]);
            mGeomagnetic[1] = mGeomagnetic[1] + alpha_mag*(event.values[1]-mGeomagnetic[1]);
            mGeomagnetic[2] = mGeomagnetic[2] + alpha_mag*(event.values[2]-mGeomagnetic[2]);

        }

Thank you very much.

1

There are 1 best solutions below

2
On

handle it with an if statement ?

iF(#getDeviceSensor == true) {
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            Log.d(TAG+" Magentic", String.valueOf(event.values[0]));
            mGeomagnetic[0] = mGeomagnetic[0] + alpha_mag*(event.values[0]-mGeomagnetic[0]);
            mGeomagnetic[1] = mGeomagnetic[1] + alpha_mag*(event.values[1]-mGeomagnetic[1]);
            mGeomagnetic[2] = mGeomagnetic[2] + alpha_mag*(event.values[2]-mGeomagnetic[2]);

        }
} else { //use another sensor for who don't have it.

}