How to get the orientation from Android?

Hi guys,

I am trying to retrieve data orientation from Android, but I do not want to use the accelerometer because I think it could be not that accurate. I am thinking on using the gyroscope instead. What do you guys think?
Is there anything already done on this?
Does anybody know how to do it or Do you have any hint about that?

Basically I’d like to know where the phone is pointing taking into consideration the magnetic north.
Therefore I can get the angle.

I found this addons but it is just for iOS:

I found these JAVA implementation for a compass, but I do not know how to link openframeworks and JAVA code.


1 Like

You need to get only “currentDegree” value from Java to openFrameworks. Then you can do the rest in the C++ side.

If you kindly check the code under oFdir/addons/ofxAndroid/src, you can find a lot of samples about how to link Java and openFrameworks.

Hi Rancs,

is it something like this?

extern "C"{
Java_cc_openframeworks_OFAndroidAccelerometer_updateAccelerometer( JNIEnv*  env, object 
thiz, jfloat x, jfloat y, jfloat z )


You can;

  • Send the data from Java side or
  • Call the data from C++ side.

The choice is up to your design.

This one is to send the data from Java side and it is very ok as a template.

Now I understand better, thank you very much.
So, basically I’d like to do something like this:

  1. add to my ofApp.cpp a snippet of code that calls for a variable in java, e.g. the currentDegree one;

  2. insert the main Java code as explained here;

  3. compile with:

    ./gradlew assembleDebug

I’m assuming I have already the gradle library inside the project directory.
Is that correct in your opinion?

Sorry I don’t use Android Studio yet, why I don’t know the gradle thingies. We still use here Eclipse.