Author Topic: How to read compass orientation?  (Read 2557 times)


  • Full Member
  • ***
  • Posts: 192
How to read compass orientation?
« on: May 02, 2013, 01:37:56 pm »

I'm writing a map-app and want to be able to read north direction to rotate the map as a user changes direction.

Using jni I managed to get hold of the sensorManagerObject but got stucked when trying to retrieve methodId for defaultSensor needed to get magnetometer.

Is there some way to get a list of methods and methodId:s via jni?

Documentation for SensorManager is like: public Sensor getDefaultSensor (int type) and
the sensorManagerClass is retrieved OK through:
  sensorManagerClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/hardware/SensorManager');

To my understanding that would indicate a call like following to retrieve methodId:
  javaMethodId_getDefaultSensor :=
    javaEnvRef^^.GetMethodID(javaEnvRef, sensorManagerClass, 'getDefaultSensor', '(I)Landroid/hardware/SensorManager;');

Making a call with that code gives: javaMethodId_getDefaultSensor = nil

Could someone give a clue to whats is wrong?

stab %)
« Last Edit: May 02, 2013, 04:16:56 pm by stab »


  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: How to read compass orientation?
« Reply #1 on: May 06, 2013, 08:14:27 am »
The method parameters descriptor is wrong, it should be: (I)Landroid/hardware/Sensor;

It returns a Sensor, not a SensorManager


  • Full Member
  • ***
  • Posts: 192
Re: How to read compass orientation?
« Reply #2 on: May 06, 2013, 03:21:33 pm »
Thanks for answering Felipemdc,

Noticed that I would be able to achieve what I want through SensorManager.getOrientation.
Read and found:
  public static float[] getOrientation(float[] R, float values[])

Modified my code to:
procedure TfrmJNITestMain.GetOrientation;
  sensorManagerClass : jclass;
  javaMethodId_getOrientation : JMethodID;
  // get sensorManagerClass
  sensorManagerClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/hardware/SensorManager');
  if sensorManagerClass = nil then
    debugln('******** sensorManagerClass NIL ********')
    debugln('******** sensorManagerClass NOT NIL ********');

  // get MethidId
  javaMethodId_getOrientation :=
  javaEnvRef^^.GetStaticMethodID(javaEnvRef, sensorManagerClass, 'getOrientation', '([F[F)F');
  if javaMethodId_getOrientation = nil then
    debugln('******** javaMethodId_getOrientation NIL ********')
    debugln('******** javaMethodId_getOrientation NOT NIL ********');
But still I get method not found.

Could you please tell me what I'm doing wrong?

stab %)


TinyPortal © 2005-2018