Skip to main content

My First FX Bluetooth App

Posted by brunogh on September 11, 2007 at 7:21 PM PDT

Hey! Today I felt a desire to create something with JavaFX Script and Bluetooth. If you are not familiar with JavaFX, JavaFX Script and so on, take a look in a new and very good Joshua Marinacci's post about it.

I have created an application that allows you to inquiry for devices that are with Bluetooth turned on. It is pretty simple in terms of UI (user interface) and functionality, but the goal here is to show a little of JavaFX Script working with Java.

Here is what you need in this tutorial:

Insert the Bluetooth USB Adpater in your computer, start Netbeans and create a JavaFX project (File->New Project... and select JavaFX and then JavaFX Application), add the two jars in the project classpath and paste the following BTInquiryFX.fx code
(note in the code that Marge is used just to start an inquiry - passing an InquiryListener - and to cancel it).


import javafx.ui.*;
import java.lang.*;

import javax.bluetooth.RemoteDevice;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;


    class Model {
        attribute inquiryListener: InquiryListener;
        attribute foundDevices : RemoteDevice[];
        attribute inquiryBtnEnabled : Boolean;
        attribute cancelBtnEnabled : Boolean;

    var model = Model {
        var: self
        inquiryBtnEnabled : true
        cancelBtnEnabled : false
        inquiryListener : new InquiryListener() {
                    operation deviceDiscovered(device: RemoteDevice, deviceClass: DeviceClass) {
                        insert device as last into self.foundDevices;

                    operation inquiryCompleted(devices: RemoteDevice[]) {
                        self.inquiryBtnEnabled = true;
                        self.cancelBtnEnabled = false;

                    operation inquiryError() {
                        self.foundDevices = null;
                        MessageDialog {
                            title: "Error"
                            message: "Inquiry Error"
                            visible: true
                        self.inquiryBtnEnabled = true;
                        self.cancelBtnEnabled = false;

    Frame {
            title: "BTInquiryFX"
            width: 400
            height: 200
            resizable: false
            content: BorderPanel {
                top: Label {
                    text: "Found Devices:"
                center: ListBox {
                     cells: bind foreach (device in model.foundDevices)
                        ListCell {
                                text: "{device.getFriendlyName(false)} - {device.getBluetoothAddress()}"
                bottom: FlowPanel {
                        [Button {
                             text: "Inquiry"
                             enabled : bind model.inquiryBtnEnabled
                             action: operation() {
                                DeviceDiscoverer.getInstance().startInquiry(DiscoveryAgent.GIAC, model.inquiryListener);
                                model.foundDevices = null;
                                model.inquiryBtnEnabled = false;
                                model.cancelBtnEnabled = true;
                        }, Button {
                             text: "Cancel"
                             enabled : bind model.cancelBtnEnabled
                             action: operation() {
                                model.inquiryBtnEnabled = true;
                                model.cancelBtnEnabled = false;
            visible: true


Then... run it! Here is what we got...


and after the inquiry is completed...


my mobile was found! My mobile runs Java too!!! ;)

PS: Learn more about JavaFX Script in Project OpenJFX. You can find good tutorials, fresh releases, plugins for Eclipse and Netbeans, a lot of good support, demo applications... Try it now!


Bruno Ghisi

Related Topics >>


It doesn´t work now

Hi, I tried to run the application but it seems to be old and doesn´t work in new releases of Java FX. In spite of that, the main error that I´ve found is that you´re trying to instantiate a new InquiryListener when it´s an interface and not a public class. I hope someone can help me with this. Very good article!!!! Regards Leandro

It doesn´t work now

hi..that's a good article..i tried to run it but it doesn't work..why?? it seems that the declaration for inquirylistener, foundDevices, inquiryBtnEnabled, and cancelBtnEnabled using attribute doesn' work.. i try to find tutorials regarding capture bluetooth signal using java fx as i'm new in java anybody can help me??

Thaks for the tutorial