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 


1 person has this problem

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


Hi,

thank you for this informations. I work with ionic cordova instead of android native. I try to apply this functions but it doesn't work. I get this error message " ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'callJavascript' of undefined " 

this is my native code (.ts)

 

import { Component } from '@angular/core';

import { Platform } from '@ionic/angular';
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})


export class HomePage {

  scannedCode:any = 'V';

  wikitudePlugin: any;
  architectView: any;
  requiredFeatures = [ '2d_tracking', 'geo' ];
  arExperienceUrl = 'www/assets/experience/world/01_ImageTracking_1_ImageOnTarget/index.html';
  startupConfiguration = {
    camera_position: 'back'
  };
  isdevicesupported: false;

  constructor(public platform: Platform) {
  platform.ready().then(() => {
    this.wikitudePlugin = cordova.require('com.wikitude.phonegap.WikitudePlugin.WikitudePlugin');
    this.architectView.callJavascript("World.getValue(" + this.scannedCode + ")");
    console.log('The wikitude object: ', this.wikitudePlugin);
    console.log('The wikitude test method: ', this.wikitudePlugin.isDeviceSupported);

    this.wikitudePlugin._sdkKey = '   '
    this.wikitudePlugin.isDeviceSupported(
      this.onDeviceSupported(),
      this.onDeviceNotSupported(),
      this.requiredFeatures);
    });
  }

  onDeviceSupported() {
    console.log('device supported');
    this.wikitudePlugin.loadARchitectWorld(
      this.onARExperienceLoadedSuccessful(),
      this.onARExperienceLoadError,
      this.arExperienceUrl,
      this.requiredFeatures,
      this.startupConfiguration
    );
  }

  onARExperienceLoadError(err) {
    console.log('error load', err)
  }

  onARExperienceLoadedSuccessful () {
    console.log('good load')
  }

  onDeviceNotSupported () {
      console.log('device not supported');
  }
}

 And my JS code (.js)

 

var myPassedValue = null;
var World = {

 getValue: function getValueFn(scannedCode) {
        myPassedValue = scannedCode;
        console.log(myPassedValue);
    }

};

World.getValue();

 

Can you help me with this? 

thank you again

Login or Signup to post a comment