Start a new topic

Can't pass values from native code to javascript

Hi, I used callJavaScript function but i can't get any change in my JS class.


here is my android code


double value = 12.0;
this.architectView.callJavascript("getValueFn(value)");


and here is my Js code 


var myPassedValue = 0;
getValue: function getValueFn(value) {
myPassedValue = value;
this.createModelAtLocation();
},

the myPassedValue doesn't updates with the value I gives in the native code


any one fix this please 


Hello Ahmed,


I believe this is the 3rd forum post you create with the same issue. So can you please stick only to this forum post here https://support.wikitude.com/discussions/topics/5000083255 and put any relevant information only there? I will close this forum post as well as this one https://support.wikitude.com/discussions/topics/5000086936.


Thanks

Eva

I see the post but it doesn't help with the issue!

the post you mentioned doesn't show the java code example !

Hi Ahmed,


This is why I asked you to update it with your java code example. The reason why we ask from our users to create unique forum posts and not duplicated ones is that it helps us tracking the issues more efficiently. Since you continue replying to this forum post then I will close the other. But please, update only this one from now on and do not create another post with similar issue.


Thank you for understanding

Eva


1 person likes this

Hello Eva, I apologize for not understanding the terms.


Now this post includes my code, and the issue as you see that I want to use a value in the .js file from the native code. I tried the answer you provided me but still the same issue.

I read the references but didn't find what I want.


Thanks.

Good morning,



your code is incorrect. What you need is the following.


double value = 12.0;
this.architectView.callJavascript("getValue(" + value + ")");


The property you are trying to invoke is called getValue, not getValueFn. You also need to pass the value of value as a parameter, not the string "value".


EDIT: Pretty sure you also need to refer to the object the getValue property is on explicitly. If you're following what our samples are doing, that would be World, like so:


double value = 12.0;
this.architectView.callJavascript("World.getValue(" + value + ")");



- Daniel



1 person likes this

Thank you very much Daniel ! it works now, also I can pass multiple parameters that's great.


another question, if i want to pass a value from the .js to native  is that possible ?


Thanks in advance

Hi,



yes, that direction of communication is also possible. Here's what you need for that:


Android


(untested code)


public class SomeActivity extends Activity implements ArchitectJavaScriptInterfaceListener {

    @Override
    public void onCreate() {
        architectView.addArchitectJavaScriptInterfaceListener(this);
    }
    
    @Override
    public void onJSONObjectReceived(JSONObject jsonObject) {
        // access jsonObject
    }
}



JavaScript


AR.context.onScreenClick = function() {
    AR.platform.sendJSONObject({"hello": "world"});
};



- Daniel


Login or Signup to post a comment