My First FX Bluetooth App
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:
- Download Netbeans IDE 6 M10 and install JavaFX Script Plug-in.
- Download marge-core-0.4.0.jar in order to facilitate the use of JSR 82 (Java Apis for Bluetooth) that is provided by Project Marge.
- Download avetanabt-20070719.tgz and compile it (if you are using Linux) or bluecove-2.0.1.jar(if you are using Windows) in order to provide a JSR 82 implementation. Take a look in my last post for more explanation.
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).
BTInquiryFX.fx
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 {
content:
[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() {
DeviceDiscoverer.getInstance().cancelInquiry();
model.inquiryBtnEnabled = true;
model.cancelBtnEnabled = false;
}
}]
}
}
visible: true
};
Screenshots
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!
Cheers,
Bruno Ghisi
- Login or register to post comments
- Printer-friendly version
- brunogh's blog
- 2280 reads






Comments
It doesn´t work now
by leannico - 2010-06-02 11:21
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 LeandroIt doesn´t work now
by ccruzzer - 2011-01-21 03:56
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 fx..is anybody can help me??
Thaks for the tutorial
by shippy - 2010-04-16 05:41