Recent

Author Topic: Modified code to get compass direction  (Read 3337 times)

stab

  • Full Member
  • ***
  • Posts: 187
Modified code to get compass direction
« on: May 07, 2013, 04:53:01 pm »
Modified the code to get compass direction and got a little further.

Changed to code to use SensorManager.getOrientation but returned values are all zero.

Modified code as:
Code: [Select]
procedure TfrmJNITestMain.btnCompassClick(Sender: TObject);
var
  sensorManagerClass, sensorClass : jclass;
  sensorManagerObject, sensorObject : jobject;
  javaMethodId_getRotationMatrix, javaMethodId_getOrientation : JMethodID;
  javaString_SENSOR_SERVICE, javaString_getRotationMatrix, javaString_getOrientation : jstring;
  lParams : array[0..4] of JValue;
  IsCopy : JBoolean;
  mGData,MData, mOrientation : JDoubleArray;
  mR, mI : JDoubleArray;
  arraydata : PDouble;
  arraylen : jsize;
  i : integer;
begin
  // get sensorManagerClass
  sensorManagerClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/hardware/SensorManager');
  if sensorManagerClass = nil then
    debugln('******** sensorManagerClass NIL ********')
  else
    debugln('******** sensorManagerClass NOT NIL ********');

  // get the string Context.SENSOR_SERVICE remember that NewStringUTF does not require ReleaseStringUTFChars
  javaString_SENSOR_SERVICE := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('sensor'));

  // Get the sensorManagerObject
  lParams[0].l := javaString_SENSOR_SERVICE;
  sensorManagerObject :=
    javaEnvRef^^.CallObjectMethodA(javaEnvRef, javaActivityObject, javaMethod_getSystemService, @lParams[0]);
  if sensorManagerObject = nil then
    debugln('******** sensorManagerObject NIL ********')
  else
    debugln('******** sensorManagerObject NOT NIL ********');

  // get MethidId for getRotationMatrix
  javaMethodId_getRotationMatrix :=
    javaEnvRef^^.GetStaticMethodID(javaEnvRef, sensorManagerClass, 'getRotationMatrix', '([F[F[F[F)Z');
  if javaMethodId_getRotationMatrix = nil then
    debugln('******** javaMethodId_getRotationMatrix NIL ********')
  else
    debugln('******** javaMethodId_getRotationMatrix NOT NIL ********');

  mR := javaEnvRef^^.NewDoubleArray(javaEnvRef, 16);
  mI := javaEnvRef^^.NewDoubleArray(javaEnvRef, 16);
  mGData := javaEnvRef^^.NewDoubleArray(javaEnvRef, 3);
  MData := javaEnvRef^^.NewDoubleArray(javaEnvRef, 3);
  lParams[0].l := mR;
  lParams[1].l := mI;
  lParams[2].l := mGData;
  lParams[3].l := MData;
  debugln('******** After loading lParams for getRotationMatrix ********');

  javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaActivityObject,
    javaMethodId_getRotationMatrix, @lParams[0]);
  debugln('******** After getRotationMatrix ********');

  // Get the elements and length
  IsCopy := 0;
  arraydata := javaEnvRef^^.GetDoubleArrayElements(javaEnvRef, mR, IsCopy);

  arraylen := javaEnvRef^^.GetArrayLength(javaEnvRef, mR);
  debugln('******** arraylen-mR: ' + IntToStr(arraylen) + ' ********');
  i := 0;
  while i < arraylen do
  begin
    debugln('******** mR[' + IntToStr(i) + ']: ' + FloatToStrF(arraydata[i], ffFixed, 15, 3) + ' ********');

    Inc(i);
  end;

  // Get the elements and length
  IsCopy := 0;
  arraydata := javaEnvRef^^.GetDoubleArrayElements(javaEnvRef, mI, IsCopy);

  arraylen := javaEnvRef^^.GetArrayLength(javaEnvRef, mI);
  debugln('******** arraylen-mI: ' + IntToStr(arraylen) + ' ********');
  i := 0;
  while i < arraylen do
  begin
    debugln('******** mI[' + IntToStr(i) + ']: ' + FloatToStrF(arraydata[i], ffFixed, 15, 3) + ' ********');

    Inc(i);
  end;
  // Release ArrayElements
  javaEnvRef^^.ReleaseDoubleArrayElements(javaEnvRef, mI, arraydata, 0);

  // Get the elements and length
  IsCopy := 0;
  arraydata := javaEnvRef^^.GetDoubleArrayElements(javaEnvRef, mGData, IsCopy);

  arraylen := javaEnvRef^^.GetArrayLength(javaEnvRef, mGData);
  debugln('******** arraylen-mGData: ' + IntToStr(arraylen) + ' ********');
  i := 0;
  while i < arraylen do
  begin
    debugln('******** mGData[' + IntToStr(i) + ']: ' + FloatToStrF(arraydata[i], ffFixed, 15, 3) + ' ********');

    Inc(i);
  end;
  // Release ArrayElements
  javaEnvRef^^.ReleaseDoubleArrayElements(javaEnvRef, mGData, arraydata, 0);

  // Get the elements and length
  IsCopy := 0;
  arraydata := javaEnvRef^^.GetDoubleArrayElements(javaEnvRef, MData, IsCopy);

  arraylen := javaEnvRef^^.GetArrayLength(javaEnvRef, MData);
  debugln('******** arraylen-MData: ' + IntToStr(arraylen) + ' ********');
  i := 0;
  while i < arraylen do
  begin
    debugln('******** MData[' + IntToStr(i) + ']: ' + FloatToStrF(arraydata[i], ffFixed, 15, 3) + ' ********');

    Inc(i);
  end;
  // Release ArrayElements
  javaEnvRef^^.ReleaseDoubleArrayElements(javaEnvRef, MData, arraydata, 0);

  // get MethidId for getOrientation
  javaMethodId_getOrientation :=
    javaEnvRef^^.GetStaticMethodID(javaEnvRef, sensorManagerClass, 'getOrientation', '([F[F)[F');
  if javaMethodId_getOrientation = nil then
    debugln('******** javaMethodId_getOrientation NIL ********')
  else
    debugln('******** javaMethodId_getOrientation NOT NIL ********');

  mOrientation := javaEnvRef^^.NewDoubleArray(javaEnvRef, 3);

  lParams[1].l := mOrientation;
  debugln('******** After loading lParams for getOrientation ********');
  javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaActivityObject,
    javaMethodId_getOrientation, @lParams[0]);
  debugln('******** After getOrientation ********');

  // Get the elements and length
  IsCopy := 0;
  arraydata := javaEnvRef^^.GetDoubleArrayElements(javaEnvRef, mOrientation, IsCopy);

  arraylen := javaEnvRef^^.GetArrayLength(javaEnvRef, mOrientation);
  debugln('******** arraylen-mOrientation: ' + IntToStr(arraylen) + ' ********');
  i := 0;
  while i < arraylen do
  begin
    debugln('******** mOrientation[' + IntToStr(i) + ']: ' + FloatToStrF(arraydata[i], ffFixed, 15, 3) + ' ********');

    Inc(i);
  end;

  // Release ArrayElements
  javaEnvRef^^.ReleaseDoubleArrayElements(javaEnvRef, mOrientation, arraydata, 0);
  javaEnvRef^^.ReleaseDoubleArrayElements(javaEnvRef, mR, arraydata, 0);

  javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
  javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[1].l);
  javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[2].l);
  javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[3].l);
end;
Thankful for advices

Regards
stab %)
« Last Edit: May 07, 2013, 05:49:01 pm by stab »

Blaazen

  • Hero Member
  • *****
  • Posts: 2994
  • POKE 54296,15
    • Eye-Candy Controls
Re: Modified code to get compass direction
« Reply #1 on: May 07, 2013, 04:59:42 pm »
Please edit your post, use tags
Code: [Select]
[code] instead of italic.
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

stab

  • Full Member
  • ***
  • Posts: 187
Re: Modified code to get compass direction
« Reply #2 on: May 07, 2013, 05:08:14 pm »
Excuse a dummie, but I don't know what you mean by tag in this context

/stab

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Modified code to get compass direction
« Reply #3 on: May 07, 2013, 05:37:13 pm »
See attached.

 

TinyPortal © 2005-2018