The Source for Java Technology Collaboration
User: Password:



Bruno Ghisi

Bruno Ghisi's Blog

My First JRuby Bluetooth App

Posted by brunogh on January 31, 2008 at 06:54 PM | Comments (1)

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

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

  • 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

    Posted by: pedrobachiega on February 01, 2008 at 08:30 AM



Only logged in users may post comments. Login Here.


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