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)

Comments
  1. iamrein says:

    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. biorex21 says:

    Whoops! heh,I’ve uploaded it again (now including those classes, they are in the Flash directory) sorry for the inconvenience =P

  3. Paulo says:

    It’s prepared to receive files or just send?

  4. Paulo says:

    Thanks. I will try to implement it. ;)

  5. biorex21 says:

    Great!, let us know if you make it available for anyone =D

  6. Bala says:

    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?

    • biorex21 says:

      That’s weird, when exactly are you getting this error?

      • Bala says:

        right after I call process.start(nativeProcessStartupInfo);
        the call back fires with that message

      • biorex21 says:

        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”);

      • Bala says:

        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.

  7. Bala says:

    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

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 )

Connecting to %s