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.fxcode (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;

import net.java.dev.marge.inquiry.DeviceDiscoverer;
import net.java.dev.marge.inquiry.InquiryListener;

    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