HomeAbout Mark van HedelContact Mark van Hedel



Flex and local Hardware

A few times in the past few months I've been asked how to talk to local hardware on the client directly from the flex interface, like a printer or bar code scanner. Since the flash player is working inside a browser it is bound to the security sandbox of the browser and for that reason has no access to the local client machine at all.

However with the use of other software or drivers it is still possible to contact this hardware and retrieve information from it.

In a browser there are a few ways to contact client information, one is using a Java applet and the other one is using an active X control on the users machine. The way you can combine one of these with a Flex application is by using JavaScript. A flex application can call a JavaScript in the web page and that JavaScript can contact a java applet. A piece of JavaScript can also directly call Flex with information and we can use that in our Flex application. The solution is working way to do this, but of course it would be better if we could do this all without using extra software or without using JavaScript because we are again more browser dependent with it, but at this point I couldn't find another way to do it yet.

To make this work make an applet in Java and load that in the same page your Flex application is loading in, next create a JavaScript to be a bridge between the 2 applications in the browser and get all communication done.

Since I've already made a post with the information in it about how to contact JavaScript from Flex I'm not going to cover that entire part again, but let me just place one piece of code here showing how to contact JavaScript.

/**
* Fill with default values if a user has decided to remember it's settings.
*/
private function fillComments():void {
   commenterName.text = ExternalInterface.call("getName");
   email.text = ExternalInterface.call("getEmail")
   website.text = ExternalInterface.call("getWebsite");
   if (commenterName.text.length >= 2 || email.text.length >= 5 || website.text.length >= 10){
      remember.selected = true;
   }
}

This is just a copy from another post and shows how to call JavaScript methods and get return values from them. Now the other way around if I want to call the my Flex application from my Javascript I can do this in my HTML.

function callAppSend() {
      document.getElementById("FlexApp").receiveStringsFunction('This is a javascript String','This is the second one');
   }

function callApp() {
var x = document.getElementById("FlexApp").myFlexFunction();
alert(x);
}

Now in ActionScript I declare my 2 functions as being able to receive information from HTML.

import flash.external.*;

       public function initApp():void {
       ExternalInterface.addCallback("myFlexFunction",myFunc);
       ExternalInterface.addCallback("receiveStringsFunction", receiveStrings);
       }


       public function myFunc():Number {
       return 42;
       }

       public function receiveStrings(param1:String, param2:String):void
       {
          content.htmlText = content.htmlText + "<br/>" + param1 + "<br/>" + param2;
          Alert.show(param1, param2);
       }

So in my initApp that I call on creationComplete I declare 2 methods that can be called from JavaScript, one by adding the name I use in JavaScript and after that the Flex function that is going to be called. Now the applet part I'll leave up to you, this should get you started already.

Related Blog Entries

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner