Wrong result calling Android method from Delphi





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to call Android's class GeomagneticField method getDeclination() from my Delphi 10.2.3 app.



First I imported the class GeomagneticField using Java2OP:



unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;

[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;

implementation

initialization
end.


Then I called the method this way:



procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970

GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;


When I run this and click, Label1 shows -174.3635711, which is wrong



On Android Studio I built a similar app in Java:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);

Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}


When I run this and click the button, I got -21.839144, which is correct.



Why is my Delphi app getting this wrong result?










share|improve this question




















  • 1





    Is your Delphi code giving you the same value for "tm" as System.currentTimeMillis()?

    – Barns
    Nov 16 '18 at 18:07











  • I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly

    – Omar Reis
    Nov 16 '18 at 18:28






  • 1





    This is a great question and deserves more upvotes.

    – J...
    Nov 16 '18 at 19:07






  • 1





    System.currentTimeMillis() is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now() returns a TDateTime expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...) for TDateTime, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween() instead, which uses TTimeStamp internally for more accurate date/time calculations.

    – Remy Lebeau
    Nov 17 '18 at 1:02








  • 1





    Note, Delphi's RTL has a System.DateUtils.DateTimeToUnix() function to convert a TDateTime to a Unix timestamp, but it has seconds precision (it uses SecondsBetween() internally), but it would be very easy to copy it and adjust it to use milliseconds instead.

    – Remy Lebeau
    Nov 17 '18 at 1:09




















1















I'm trying to call Android's class GeomagneticField method getDeclination() from my Delphi 10.2.3 app.



First I imported the class GeomagneticField using Java2OP:



unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;

[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;

implementation

initialization
end.


Then I called the method this way:



procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970

GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;


When I run this and click, Label1 shows -174.3635711, which is wrong



On Android Studio I built a similar app in Java:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);

Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}


When I run this and click the button, I got -21.839144, which is correct.



Why is my Delphi app getting this wrong result?










share|improve this question




















  • 1





    Is your Delphi code giving you the same value for "tm" as System.currentTimeMillis()?

    – Barns
    Nov 16 '18 at 18:07











  • I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly

    – Omar Reis
    Nov 16 '18 at 18:28






  • 1





    This is a great question and deserves more upvotes.

    – J...
    Nov 16 '18 at 19:07






  • 1





    System.currentTimeMillis() is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now() returns a TDateTime expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...) for TDateTime, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween() instead, which uses TTimeStamp internally for more accurate date/time calculations.

    – Remy Lebeau
    Nov 17 '18 at 1:02








  • 1





    Note, Delphi's RTL has a System.DateUtils.DateTimeToUnix() function to convert a TDateTime to a Unix timestamp, but it has seconds precision (it uses SecondsBetween() internally), but it would be very easy to copy it and adjust it to use milliseconds instead.

    – Remy Lebeau
    Nov 17 '18 at 1:09
















1












1








1








I'm trying to call Android's class GeomagneticField method getDeclination() from my Delphi 10.2.3 app.



First I imported the class GeomagneticField using Java2OP:



unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;

[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;

implementation

initialization
end.


Then I called the method this way:



procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970

GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;


When I run this and click, Label1 shows -174.3635711, which is wrong



On Android Studio I built a similar app in Java:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);

Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}


When I run this and click the button, I got -21.839144, which is correct.



Why is my Delphi app getting this wrong result?










share|improve this question
















I'm trying to call Android's class GeomagneticField method getDeclination() from my Delphi 10.2.3 app.



First I imported the class GeomagneticField using Java2OP:



unit Androidapi.JNI.Interfaces.JGeomagneticField;
interface
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
// ===== Forward declarations =====
JGeomagneticField = interface;//android.hardware.GeomagneticField
// ===== Interface declarations =====
JGeomagneticFieldClass = interface(JObjectClass)
['{77F2155B-1F9A-40E0-89FA-FE3422336577}']
{class} function init(gdLatitudeDeg: Single; gdLongitudeDeg: Single; altitudeMeters: Single; timeMillis: Int64): JGeomagneticField; cdecl;
{class} function getHorizontalStrength: Single; cdecl;//Deprecated
{class} function getInclination: Single; cdecl;//Deprecated
{class} function getX: Single; cdecl;//Deprecated
end;

[JavaSignature('android/hardware/GeomagneticField')]
JGeomagneticField = interface(JObject)
['{47CF41EC-AAAB-4EE2-867A-884A3EF00407}']
function getDeclination: Single; cdecl;//Deprecated
function getFieldStrength: Single; cdecl;//Deprecated
function getY: Single; cdecl;
function getZ: Single; cdecl;
end;
TJGeomagneticField = class(TJavaGenericImport<JGeomagneticFieldClass, JGeomagneticField>) end;

implementation

initialization
end.


Then I called the method this way:



procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; t0,t:TDatetime;
begin
t0 := EncodeDatetime(1970,1,1,0,0,0,0);
t := now;
tm := Trunc( (t-t0)*24*3600*1000); // <-- ms since 1/1/1970

GeoField := TJGeomagneticField.JavaClass.init(-23,-46,750,tm );
Label1.Text := FloatToStr( GeoField.getDeclination );
end;


When I run this and click, Label1 shows -174.3635711, which is wrong



On Android Studio I built a similar app in Java:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btnShowDecl);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeomagneticField geoField = new GeomagneticField(
(float) -23,
(float) -46,
(float) 750,
System.currentTimeMillis()
);
float f = geoField.getDeclination();
String s = Float.toString(f);

Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG)
.show();
}
});
}


When I run this and click the button, I got -21.839144, which is correct.



Why is my Delphi app getting this wrong result?







android delphi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 18:34









J...

24.5k446108




24.5k446108










asked Nov 16 '18 at 17:02









Omar ReisOmar Reis

137110




137110








  • 1





    Is your Delphi code giving you the same value for "tm" as System.currentTimeMillis()?

    – Barns
    Nov 16 '18 at 18:07











  • I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly

    – Omar Reis
    Nov 16 '18 at 18:28






  • 1





    This is a great question and deserves more upvotes.

    – J...
    Nov 16 '18 at 19:07






  • 1





    System.currentTimeMillis() is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now() returns a TDateTime expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...) for TDateTime, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween() instead, which uses TTimeStamp internally for more accurate date/time calculations.

    – Remy Lebeau
    Nov 17 '18 at 1:02








  • 1





    Note, Delphi's RTL has a System.DateUtils.DateTimeToUnix() function to convert a TDateTime to a Unix timestamp, but it has seconds precision (it uses SecondsBetween() internally), but it would be very easy to copy it and adjust it to use milliseconds instead.

    – Remy Lebeau
    Nov 17 '18 at 1:09
















  • 1





    Is your Delphi code giving you the same value for "tm" as System.currentTimeMillis()?

    – Barns
    Nov 16 '18 at 18:07











  • I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly

    – Omar Reis
    Nov 16 '18 at 18:28






  • 1





    This is a great question and deserves more upvotes.

    – J...
    Nov 16 '18 at 19:07






  • 1





    System.currentTimeMillis() is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now() returns a TDateTime expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...) for TDateTime, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween() instead, which uses TTimeStamp internally for more accurate date/time calculations.

    – Remy Lebeau
    Nov 17 '18 at 1:02








  • 1





    Note, Delphi's RTL has a System.DateUtils.DateTimeToUnix() function to convert a TDateTime to a Unix timestamp, but it has seconds precision (it uses SecondsBetween() internally), but it would be very easy to copy it and adjust it to use milliseconds instead.

    – Remy Lebeau
    Nov 17 '18 at 1:09










1




1





Is your Delphi code giving you the same value for "tm" as System.currentTimeMillis()?

– Barns
Nov 16 '18 at 18:07





Is your Delphi code giving you the same value for "tm" as System.currentTimeMillis()?

– Barns
Nov 16 '18 at 18:07













I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly

– Omar Reis
Nov 16 '18 at 18:28





I checked that. There is a small difference of about 9 hours ( I don't know why ). But this should not be the problem, as the value of the Declination changes very slowly

– Omar Reis
Nov 16 '18 at 18:28




1




1





This is a great question and deserves more upvotes.

– J...
Nov 16 '18 at 19:07





This is a great question and deserves more upvotes.

– J...
Nov 16 '18 at 19:07




1




1





System.currentTimeMillis() is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now() returns a TDateTime expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...) for TDateTime, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween() instead, which uses TTimeStamp internally for more accurate date/time calculations.

– Remy Lebeau
Nov 17 '18 at 1:02







System.currentTimeMillis() is the number of milliseconds from the Unix epoch to the current time in UTC, but Delphi's Now() returns a TDateTime expressed in local time instead. So, you need to convert the local time to UTC before you can then calculate the milliseconds from the Unix epoch. Also, you really should not do things like Trunc( (t-t0) ...) for TDateTime, that performs floating-point arithmetic that can skew the results. Use System.DateUtils.MilliSecondsBetween() instead, which uses TTimeStamp internally for more accurate date/time calculations.

– Remy Lebeau
Nov 17 '18 at 1:02






1




1





Note, Delphi's RTL has a System.DateUtils.DateTimeToUnix() function to convert a TDateTime to a Unix timestamp, but it has seconds precision (it uses SecondsBetween() internally), but it would be very easy to copy it and adjust it to use milliseconds instead.

– Remy Lebeau
Nov 17 '18 at 1:09







Note, Delphi's RTL has a System.DateUtils.DateTimeToUnix() function to convert a TDateTime to a Unix timestamp, but it has seconds precision (it uses SecondsBetween() internally), but it would be very easy to copy it and adjust it to use milliseconds instead.

– Remy Lebeau
Nov 17 '18 at 1:09














1 Answer
1






active

oldest

votes


















2














I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.



And it worked !



function switchDWords(n:int64):int64;    // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;

procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;

tm := switchDWords(tm); // <-- hack tm

GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;


Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.






share|improve this answer


























  • reported to QC: quality.embarcadero.com/browse/RSP-21602

    – Omar Reis
    Nov 20 '18 at 11:24












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342348%2fwrong-result-calling-android-method-from-delphi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.



And it worked !



function switchDWords(n:int64):int64;    // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;

procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;

tm := switchDWords(tm); // <-- hack tm

GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;


Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.






share|improve this answer


























  • reported to QC: quality.embarcadero.com/browse/RSP-21602

    – Omar Reis
    Nov 20 '18 at 11:24
















2














I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.



And it worked !



function switchDWords(n:int64):int64;    // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;

procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;

tm := switchDWords(tm); // <-- hack tm

GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;


Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.






share|improve this answer


























  • reported to QC: quality.embarcadero.com/browse/RSP-21602

    – Omar Reis
    Nov 20 '18 at 11:24














2












2








2







I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.



And it worked !



function switchDWords(n:int64):int64;    // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;

procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;

tm := switchDWords(tm); // <-- hack tm

GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;


Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.






share|improve this answer















I narrowed the problem to the "timeMillis" parameter.
It is a Java long, which corresponds to Delphi's int64.
I suspected of some kind of endian problem, so I
tried to switch the long parameter high and lo dwords.



And it worked !



function switchDWords(n:int64):int64;    // switch hi <--> lo dwords of an int64
var i: Integer;
nn :int64; nnA:array[0..7] of byte absolute nn;
nn1:int64; nn1A:array[0..7] of byte absolute nn1;
begin
nn1 := n; // copy n
for i := 0 to 3 do // switch bytes hidword <--> lodword
begin
nnA[i] := nn1A[i+4];
nnA[i+4] := nn1A[i];
end;
Result := nn;
end;

procedure TForm.Button1Click(Sender: TObject);
var GeoField: JGeomagneticField; tm:int64; aD:Single;
begin
tm := System.DateUtils.DateTimeToUnix( Now, {InputAsUTC:} false )*1000;

tm := switchDWords(tm); // <-- hack tm

GeoField := TJGeomagneticField.JavaClass.init({Lat:}-23, {Lon:}-46, {Alt:}750, tm );
aD := GeoField.getDeclination(); // <-- this shows -21.8416 which is correct !
Label1.Text := FloatToStr( aD );
end;


Java2OP translation seems to be ok, so I wonder where the problem is.
Anyway, passing this hacked int64 parameter fixed it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 12:51

























answered Nov 19 '18 at 11:46









Omar ReisOmar Reis

137110




137110













  • reported to QC: quality.embarcadero.com/browse/RSP-21602

    – Omar Reis
    Nov 20 '18 at 11:24



















  • reported to QC: quality.embarcadero.com/browse/RSP-21602

    – Omar Reis
    Nov 20 '18 at 11:24

















reported to QC: quality.embarcadero.com/browse/RSP-21602

– Omar Reis
Nov 20 '18 at 11:24





reported to QC: quality.embarcadero.com/browse/RSP-21602

– Omar Reis
Nov 20 '18 at 11:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342348%2fwrong-result-calling-android-method-from-delphi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma