-
Notifications
You must be signed in to change notification settings - Fork 25
A.6. JS Communication WIth AIR
When building your AR experience, there might be many reasons why you would need to communicate between your AR world and your AIR logic. As an example, you may want to update geo locations in your AR world from your AIR app. Or maybe you want to close the AR window from JS. There might be many other reasons you may need this communication. No matter what reason you need for this communication, this can easily happen and you will know how this workd below.
After your AR is initialized, you should listen to the ArEvents.JS_TALK
event to receive String messages from Javascript.
AR.listener.addEventListener(ArEvents.JS_TALK, onJsTalk);
private function onJsTalk(e:ArEvents):void
{
trace("onJsTalk: " + e.msg);
// The best practice is to send a json string from js to AIR
var obj:Object = JSON.parse(e.msg);
}
To actually send a message from JS to AIR, all you need is to send a string like this:
AR.platform.sendJSONObject(
{
action:"close_ar"
});
First create your function on the JS side, for example we need a function with two input params:
function remoteJSfunction(data1, data2)
{
alert("received data from AIR:\n"+data1+", "+data2);
};
Then, call AR.callJS("")
on the AS3 side to call that function.
AR.callJS("remoteJSfunction('value1', 'value2')");
Enjoy building Air apps – With ♥ from MyFlashLabs Team