Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

A.6. JS Communication WIth AIR

Hadi Tavakoli edited this page Aug 22, 2017 · 4 revisions

JS Communication

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.

Send data from JS to AIR

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"
});

Call JS functions from AIR

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')");