The Source for Java Technology Collaboration
User: Password:



Bruno Ghisi

Bruno Ghisi's Blog

My First FX Bluetooth App

Posted by brunogh on September 11, 2007 at 07:21 PM | Comments (13)

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).

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...

btinquiryfx1.png

and after the inquiry is completed...

btinquiryfx2.png

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

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • The subject of a very wonderful and distinct
    I thank you for continuing excellence
    Thank you


    =====================================================================================================


    ليبيا
    شباب ليبيا
    libya
    منتديات
    منتديات ليبية
    غرائب وحقائق
    أحاديث شريفة
    برامج اسلامية للجوال
    مفاتيح الديجيتل
    الشيرنج
    الرسيفرات
    كتب إسلامية
    خلفيات للموبيل
    الشعر الشعبي
    الصحة والطب
    طب اسنان
    كتب طب اسنان مجانية
    برامج طبية
    تعلم الإنكليزية
    اللغة الفرنسية
    طب الإعشاب
    الخواطرالادبية
    الازياء والمكياج
    تعليم الطبخ
    الاثاث الحديث
    مقاطع كرة قدم
    المصارعه الحرة
    اهداف كوره
    الفوتوشوب
    اروع البرامج
    الدوري الليبي
    خلفيات رياضية
    المصارعة
    كورة عربية
    كرة قدم عالمية
    الدوري الإيطالي
    الدوري الاسباني
    الدوري الإنجليزي
    صور المشاهير
    انواع الحلويات
    افلام كوميدية
    احدث الافلام
    افلام
    التقنية
    تحميل افلام
    برامج
    اخر برامج الجوال
    kaspersky
    أفلام كرتون عربية
    برامج برامج كمبيوتر
    برامج حماية
    برامج اختراق
    برامج صوت
    برامج تحميل برامج احدث البرامج
    محادثة
    خلفيات الطبيعة
    برامج مبايل للتحميل
    اخبار الفن
    احدث الافلام للتحميل
    تحميل افلام رعب
    ترجمةأفلام
    الكامات
    برامج جوال
    برامج محاسبة
    برامج
    kasper
    games
    برامج
    برامج
    انترنت
    برامج صوتية
    شبكات الحاسوب
    خلفيات للويندوز
    تطويرالمواقع
    العاب
    العاب الفيديو
    games
    شفرات
    برامج مسنجر
    خلفيات شاشة
    صور ترحيبيه
    الفوتوشوب
    خلفيات طبيعة
    تطويرالمواقع
    الفوتوشوب
    مقاطع البلوتوت
    مسجات ليبية
    خلفيات
    الفلاش
    التصميم الثلاثي
    برامج الجوال
    العاب الجوال
    فيديو كليب
    مسجات
    ترددات ستالايت
    نغمات

    Posted by: libyan on May 30, 2008 at 01:32 PM

  • From within the EDT, I suppose. If you got interested in questions related to that, I would suggest you to subscribe openjfx users list, which is pretty active and good mailing list with a lot of FX Script experts.
    Cheers,
    Bruno

    Posted by: brunogh on September 14, 2007 at 11:30 AM

  • I know bind takes care of updating the value. But does it update the value from within the Event Dispatch thread? For example:
    Button { label: bind model.buttonLabel }

    If model.buttonLabel is changed from a thread different to EDT, is the button's label updated from within the event dispath thread? Now that I think of it, it probably does, but I did not read anything confirming this.

    Posted by: fatihc on September 14, 2007 at 09:23 AM

  • Hi fatihc, nice comments! :) The inquiry method does not block and the bind feature is very cool, it automatically cares about updating the values.
    Cheers,
    Bruno

    Posted by: brunogh on September 14, 2007 at 08:51 AM

  • Update to my previous comment. How does bind exactly work? Does it take care of updating the values from within the Event Dispatch thread? If so, cool, and forget my previous comment.

    Posted by: fatihc on September 14, 2007 at 07:43 AM

  • Does calling the startInquiry() method hang the GUI? If no, does it start a new thread in the background? If yes, the operation deviceDiscovered() does not run in the EventDispatch thread, does it? I believe you should surround the body of this operation with a do later {...} block, am I right?

    Posted by: fatihc on September 14, 2007 at 07:39 AM

  • Got it! I had some problems with carriers here in Brazil, but not related to Bluetooth... pretty boring it happened, because in an emulator is not the same feeling as in a real mobile :(
    Cheers,
    Bruno

    Posted by: brunogh on September 13, 2007 at 06:44 PM

  • Bruno,

    Sorry for the rant. I guess my point of frustration comes not from JSR 82, but that some carriers require signed certificates in order to use Bluetooth socket connections...At least that is my personal experience when trying to write BT apps. I can write the detection/lookup portion of an app, but when it comes to doing anything exciting (getting a socket, reading, writing) those operations require certificates - certificates diffiult/expensive to obtain if you are the average Joe doing this on your spare time.

    Posted by: wsnyder6 on September 13, 2007 at 06:05 PM

  • Hi wsnyder6 ,
    I am not sure if I got your point... can you explain it more? Are you complaining about how complex JSR 82 is? Or that you do not find phones that implements it or that have bugs in its implementation?
    Thanks,
    Bruno

    Posted by: brunogh on September 13, 2007 at 11:31 AM

  • Very nice. What kills me about bluetooth is how incredibly difficult it is for the avergage developer to even get bluetooth enabled apps on phones. I can write JSE clients, but if I can't get my other devices to talk to it, waht's the point?

    Posted by: wsnyder6 on September 13, 2007 at 06:40 AM

  • cool!!simple and easy!! good job!!now you could integrate with Lucas application

    Posted by: pedrobachiega on September 12, 2007 at 08:58 PM

  • Hi Ivan!
    I think maybe you are right in a real application. This demo was pretty small and decided to use Java interfaces inside it to see how things works :)
    Thanks,
    Bruno

    Posted by: brunogh on September 12, 2007 at 03:46 AM

  • nice :)
    however, do you think it is a good idea to keep the bluetooth-related code (which is a bit clumsy) inside the FX script? maybe it's better to move it to java and give a nice model front-end to FX?

    Posted by: ivan_memruk on September 11, 2007 at 11:35 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds