The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


My First JRuby Bluetooth App

Posted by brunogh on January 31, 2008 at 6:54 PM PST
TV, Coke and a laptop, it is time to have fun!

We are always trying to integrate Marge (Java Bluetooth Framework) with other things. If you remember, in a last post, JavaFX Script was used, now it's time for JRuby! May you don't know, JRuby is Java implementation of the Ruby programming language. I am not a Ruby expert, but I hope I can become, because I am enjoying the language so much. Actually, I enrolled in a pretty interesting free online course by Satish Talim. If you enjoyed the content, be sure you join on March too!

Here we go, no more delays! The basic idea of this post is to show a simple sample application that inquiry for Bluetooth devices, but more than just creating the application, be aware of thinking in the possibilities you can have with JRuby.

What you will need:
  • Download Netbeans IDE 6 (Select All package version, because it comes with Java and Ruby).
  • Get marge-core in Marge repository (you can use Netbeans to checkout from Subversion and generate a jar, because the last version is under development and was not launched yet, we are working on it :) ). Otherwise download the last launched version (0.4.0) and fix the inquiry method.
  • Get a JSR 82 implementation, I have used Avetana for Linux. I have explained it in another post, you can take a look here.

Time to code!
  1. Create a Ruby Project: File -> New Project -> Ruby -> Ruby Application. Click Next, configure your project (create a main.rb file and use a JRuby interpreter) and then click Finish.
  2. Add marge-core and Avetana. Right click in the project -> Properties -> Java and then add the jars in the JRuby classpath.
  3. Add the following code into main.rb. The code is pretty intuitive, because Ruby is! It basically imports some Marge classes (that are going to use Avetana internally), defines a listener that will be called during the inquiry and will print all the devices found. Also, the inquiry will be started by the singleton DeviceDiscoverer.

  4. 
    include Java
    
    include_class 'net.java.dev.marge.inquiry.DeviceDiscoverer'
    include_class 'net.java.dev.marge.inquiry.InquiryListener'
    
    puts 'Inquiring for Bluetooth Devices'
    discoverer = DeviceDiscoverer.instance;
    
    class InquiryListenerAppender 
        include InquiryListener
      def inquiryCompleted(remoteDevices)
        remoteDevices.each { |device| print "#{device.bluetooth_address} - #{device.get_friendly_name true} \n"  }
      end
      def inquiryError
        puts 'Inquiry Error'
      end
      def deviceDiscovered(device,deviceClass)
      end
    end
    
    discoverer.start_inquiry_giac InquiryListenerAppender.new
    

Project Window:
btjruby_1.png

Just insert your Bluetooth USB adapter press F6 to run!
btjruby_2.png

My mobile was found! Before we finish, take a look in this nice effort to integrate Bluetooth/Ruby.

The possibilities are endless...they are always endless...

Cheers,
Bruno Ghisi
Related Topics >> Netbeans      
Comments
Comments are listed in date ascending order (oldest first)

The subject of a very wonderful and distinct I thank you for continuing excellence Thank you verey gooooooooood ======================================================================== ليبيا شباب ليبيا libya منتديات منتديات ليبية غرائب وحقائق أحاديث شريفة برامج اسلامية للجوال مفاتيح الديجيتل الشيرنج الرسيفرات كتب إسلامية خلفيات للموبيل الشعر الشعبي الصحة والطب طب اسنان كتب طب اسنان مجانية برامج طبية تعلم الإنكليزية اللغة الفرنسية طب الإعشاب الخواطرالادبية الازياء والمكياج تعليم الطبخ الاثاث الحديث مقاطع كرة قدم المصارعه الحرة اهداف كوره الفوتوشوب اروع البرامج الدوري الليبي خلفيات رياضية المصارعة كورة عربية كرة قدم عالمية الدوري الإيطالي الدوري الاسباني الدوري الإنجليزي صور المشاهير انواع الحلويات افلام كوميدية احدث الافلام افلام التقنية تحميل افلام برامج اخر برامج الجوال kaspersky أفلام كرتون عربية برامج برامج كمبيوتر برامج حماية برامج اختراق برامج صوت برامج تحميل برامج احدث البرامج محادثة خلفيات الطبيعة برامج مبايل للتحميل اخبار الفن احدث الافلام للتحميل تحميل افلام رعب ترجمة أفلام الكامات برامج جوال برامج محاسبة برامج kasper games برامج برامج انترنت برامج صوتية شبكات الحاسوب خلفيات للويندوز تطويرالمواقع العاب العاب الفيديو games شفرات برامج مسنجر خلفيات شاشة صور ترحيبيه الفوتوشوب خلفيات طبيعة تطويرالمواقع الفوتوشوب مقاطع البلوتوت مسجات ليبية خلفيات الفلاش التصميم الثلاثي برامج الجوال العاب الجوال فيديو كليب مسجات ترددات ستالايت نغمات

ruby is really fantastic! easy to programming, fexible... nice to see how easy is use jruby... I don't try it yet...
next step!! cheers

well done
_