BluetoothSender (or yet another Native Process post)


Bluetooth communication in Flash is not possible natively (yet), so using Java (with Bluecove library) you can achieve this by using Native Process in AIR.

It works like this: FlashApp < — > AIRApp< — > Java. Of course, the AIRApp needs to be working on the background, and the FlashApp will be you main application (or it can be another AIR application).

I created an API to let Flash send commands to bluetooth, by the way it uses AS3 Signals for events dispatching. This is a code sample for searching devices:

If the image looks a bit blurry, then you need glasses, nah just kidding, it actually looks like that


“Yeah that’s cool, but how do I set everything up to create my app?” ok here it goes:

The first thing you need to do is to install BluecoveAIR

When you run it for the first time, select the path to javaw.exe by clicking the “Hey! Select Javaw.exe” button, the location is stored (via SharedObject) so you don’t have to set that again.

A window will aid you to navigate to choose the exe file.

Once it’s done you can see the “hello, Java stared” text (and a blue light indicator) that confirms that Java is good and ready to go.

That’s pretty everything you need to do in order to start using API. You can notice some options for Local Connection, these are default settings, if you wanna change them, type the new connection names in the input fields, but also remember to change them in the FlashBluetooth constructor in your Flash app.

constructor params

Local Connection default settings in the constructor (you may want to click the image).

This is a demo application to show how it works. You don’t have to worry much about the java output, it just displays what’s going on.

Note: First, place an mp3 file called “myFile” on drive C:\

Let’s start by doing a device inquiry, for that click “Search devices”. When finished, copy an address and paste it in the input text.  Then press the Download button.

When transfering the file, you’ll receive some permission notifications in your device, just accept them. Also will ask you to type PIN or code, it is 0000 (four zeros). The PIN code is hardcoded in the Java app, but yes, I will add the option to change that.

Download: AS3 API (with a few docs), sample app, BluecoveAIR and Java executables and source codes here

This was tested on Windows XP, Vista and 7, 32 bit. When I tried it on a Win7 x64 it didn’t work, so beware. (On that x64 machine, I later removed  Win7 64 and then installed a 32 bit Win XP and it went ok)

It breaks my heart not being able to generate a .dmg since I don’t have a Mac =P but if someone creates it, let me know. Also if you wanna clean and/or optimize the Java source code.. please do.

Here’s a video showing it in action

(sorry for my bad english :X lol, not that “bad english”, I mean my pronunciation)

18 thoughts on “BluetoothSender (or yet another Native Process post)

  1. Hi, thanks for your sharing. It’s great and helpful !!
    But when I try your example files..the Class “BluetoothSender” seems not to be included .
    Could you please release it ?

    Thank you so much!

    Best Regards,
    Rein

  2. This is great work! I have a question (I am missing a key step), I am trying to communicate via bluetooth with my AIR app (javascript).

    I am getting this error:

    ERROR – [timestamp here] adl[92076:203] *** NSTask: Task create for path ‘[pathto]/bin/BluecoveBridge.jar’ failed: 22, “Invalid argument”. Terminating temporary process.

    and the event.exitCode is 5. any pointers you can share?

      1. Could you post your code where you set the arguments for the nativeProcessStatupInfo? I think the problem is when setting the BluecoveBridge.jar location.

        Don’t know how it is in Javascript, but It’s the part (in my code) that has:
        var args:Vector. = new Vector.();
        args.push(“-jar”);
        args.push(File.applicationDirectory.resolvePath(“BluecoveBridge.jar”).nativePath);

        Maybe you could try using a full path, to have something like this in the last line: args.push(“C:\\Users\\UserName\\AIR\\BluecoveAIR\\bin\\BluecoveBridge.jar”);

      2. Aaah! My problem was actually: the java executable file, I was passing the wrong path. ( I am on a mac, and I needed to provide the path to java, which in my case was: /System/Library/Frameworks/JavaVM.framework/Home/bin/java)

        My code looks like:
        var java_file = new air.File(PATH TO JAVA);
        var np_file = air.File.applicationDirectory.resolvePath(“bin/BluecoveBridge.jar”);

        var processArgs = new air.Vector[“”]();
        processArgs.push(“-jar”);
        processArgs.push(np_file.nativePath);

        var nativeProcessStartupInfo = new air.NativeProcessStartupInfo();
        nativeProcessStartupInfo.executable = java_file;
        nativeProcessStartupInfo.arguments = processArgs;

        nativeProcess.start(nativeProcessStartupInfo);
        nativeProcess.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
        nativeProcess.addEventListener(air.ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
        nativeProcess.addEventListener(air.NativeProcessExitEvent.EXIT, onExit);
        nativeProcess.addEventListener(air.IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
        nativeProcess.addEventListener(air.IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);

        and then I call

        if (nativeProcess.running) {
        nativeProcess.standardInput.writeMultiByte(“doOperation searchDevices\n”, “utf-8”);
        }

        now I have to start working through the errors like:
        hello, Java startedCommand received: doOperation searchDevices

        starting searchonAgentError ERROR detected and all operations stopped.javax.bluetooth.BluetoothStateException: BlueCove library bluecove not available No discovery agent available.
        ERROR – Native Library bluecove not available
        Exception in thread “main” java.lang.Error: No discovery agent available.
        at BluecoveBridge.getAgent(BluecoveBridge.java:455)
        at BluecoveBridge.startDeviceInquiry(BluecoveBridge.java:76)
        at BluecoveBridge.main(BluecoveBridge.java:561)

        Thank you for you help.

  3. hello, sorry to crowd your comments. Any pointers you can give for this error: “No discovery agent available”

    starting searchonAgentError ERROR detected and all operations stopped.javax.bluetooth.BluetoothStateException: BlueCove library bluecove not available No discovery agent available.
    ERROR – Native Library bluecove not available
    Exception in thread “main” java.lang.Error: No discovery agent available.
    at BluecoveBridge.getAgent(BluecoveBridge.java:455)
    at BluecoveBridge.startDeviceInquiry(BluecoveBridge.java:76)
    at BluecoveBridge.main(BluecoveBridge.java:561)

    Thanks again

    1. Thats when you don’t have bluetooth support on your computer, but I think your Mac obviously has. Don’t know if yours is, but looks like there are some problems in 64 bit machines (http://code.google.com/p/bluecove/issues/detail?id=35), so you would need to disable the 64-bit java VM (how to do it: http://sbagleysd.wordpress.com/2011/02/03/bluecove-and-snow-leopard-mac-os-x-10-6-6/ and taken from the comments: http://www.uweschmidt.org/wiimote-whiteboard/comment-page-21#comment-686 , http://support.apple.com/kb/TS2827). Hope that works.

  4. Hi, thanks for sharing! Tell me something, I want to make an android app using AS3 and make this app take inputs from the accelerometer and send it in text form via Bluetooth to a bluetooth module connected to a arduino or mbed prototyping board. Do you think I’ll be able to do it with BluetoothSender ? Obrigado!

    1. Hi! I’m afraid it will not work, since Native Process class is not available for mobile. BUT! there are Native Extensions (ANE’s) that let Flash to communicate with the device and perform tasks that are normally not available in AS3 like access to camera, battery status. I haven’t seen any ANE that does bluetooth communicacion, so maybe you would have to code your own.

      1. Thanks man, for the fest reply. That’s unfortunate. And you are right about not having an ANE for bluetooth. But since I was going to learn AS3 just for this project, I will just code it in java. Cheers!

  5. Instead of asking for the 0000 confirm code I have to confirm a random number on the phone and I have to confirm the pairing on windows.
    And when searching for devices the air application displays the list but it is not shown in the flash application. Is the download version the same version that you have shown in the video?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s