Important: Please read the Qt Code of Conduct -

[SOLVED] Compass on Android - Magnetometer values problem?

  • Hi, I have a short question:

    According to this table: the compass feature is not supported for android systems

    Therefore I think it is useless to use the QCompass class.
    But I need a compass!

    According to the "QRotationReading Class": , the values returned by this class does not give you a direction to the magnetic north.

    So I have to use the QMagnetometer and QAccelerator class to build my own compass.

    -Question: Is there something like the SensorManager RotationMatrix and getOrientation function available in Qt?-
    -If not, should I implement it by myself or should I better use the function through JNI?-

    Maybe you have also some other ideas how I could easily build a compass in Qt.


    I just decided to "convert" the needed java methods into c++.
    Now I have another problem!

    You can find the following code snippet in the getRotationMatrix java method:
    @public static boolean getRotationMatrix(float[] R, float[] I,
    float[] gravity, float[] geomagnetic) {
    // TODO: move this to native code for efficiency
    float Ax = gravity[0];
    float Ay = gravity[1];
    float Az = gravity[2];
    final float Ex = geomagnetic[0];
    final float Ey = geomagnetic[1];
    final float Ez = geomagnetic[2];
    float Hx = EyAz - EzAy;
    float Hy = EzAx - ExAz;
    float Hz = ExAy - EyAx;
    final float normH = (float)Math.sqrt(HxHx + HyHy + Hz*Hz);
    ----> if (normH < 0.1f) {
    // device is close to free fall (or in space?), or close to
    // magnetic north pole. Typical values are > 100.
    return false;

    My problem is that "if (normH < 0.1f) " is always false. (I just added an arrow in the code to show you the line).

    In my opinion the problem is that the magnetometer sensor returns values like this:

    magn[]={ -6.9375e-06 , -9.125e-06 , 6.25e-06 }
    magn[]={ -6.6875e-06 , -9.375e-06 , 6.5e-06 }
    magn[]={ -6.1875e-06 , -9.375e-06 , 6.5e-06 }
    magn[]={ -5.9375e-06 , -8.875e-06 , 6.5e-06 }
    magn[]={ -5.6875e-06 , -9.375e-06 , 6.75e-06 }
    magn[]={ -5.6875e-06 , -9.125e-06 , 6.75e-06 }
    magn[]={ -5.9375e-06 , -8.875e-06 , 6.25e-06 }
    magn[]={ -5.9375e-06 , -8.875e-06 , 6.5e-06 }

    with the acceleration values:
    accel[]={ -2.10605 , 0.919373 , 9.61426 }
    accel[]={ -2.1435 , 0.880215 , 9.65342 }
    accel[]={ -2.1435 , 0.880215 , 9.61426 }
    accel[]={ -2.10605 , 0.842759 , 9.65342 }

    normH is always something around this: normH = 0.000110721
    So the getRotationMatrix function will always return false.

    Info: I'm using the QMagnetometer for the values shown as magn[]=...
    setReturnGeoValues doesnt change something.
    The magn values are with setReturnGeoValues(true).

    My questions are:
    Is the magnetometer working properly?
    Do I have to convert the "tiny" values into a different "electromagnetic unit" or something else?

  • The problem was that Qt returns values in T and native android returns values in µT.

    I just wrote an native Android Magnetometer app to verify that my sensor is working. Andorid returns values like: 3.??? or 5.???. Qt returns values like: 3.???e-06 or 5.???e-06

    I dont know why Qt didnt wrote anything about the meausring units in their documentation... (Maybe I was too stupid to find it)
    Android documentated the measuring units for every sensor.

  • Lifetime Qt Champion


    After a quick look I found the unit here "here":

    Hope it helps

  • Hello,
    do you have a working compass object you can share with us?

  • I have ported some java code and built Qt native compass (with same interface of QCompass) using QMagnetometer and QAccelerometer.

  • A have a same problem.
    Is someone have a working compass code (get azimuth, pitch and roll) from Magnetometer and Accelerometer?

  • Yes sure! :)
    Just check my contribution within my sign!

Log in to reply