lørdag 20. juni 2009

Using MicroLog over Bluetooth with Ubuntu and Eclipse

MicroLog is a great tool for logging in Java ME. One of the cool features that is supported is logging over Bluetooth. This means that you can easily set up the application to connect to a lightweight server application running on your computer. MicroLog takes care of all the boilerplate code associated with creating the Bluetooth connection, sending the data and so on. All you have to do is add a BluetoothSerialAppender to you Logger-object and you are good to go (for more information on implementation details: http://blog.jayway.com/2008/12/17/java-me-logging-over-bluetooth-using-microlog/). The Bluetooth server is a small application that accepts connections from the clients and prints them out to the console.

The main purpose of this article is to show how you can use the Bluetooth feature of MicroLog with Ubuntu and Eclipse. There are some additional challenges when using Linux and MicroLog, but as presented here they are not too hard to fix. The jar-files used in this example are snapshot versions, so do not be surprised if you find a few bugs.


Making the MicroLog server compatible with Ubuntu

The first problem I had was with the MicroLog server. I use Ubuntu for all my tasks, including software development and the BlueCove version included in the MicroLog server package (microlog-servers-jar-with-dependencies.jar) was not compatible with the OS. It did not start and printed out this error message:

BluetoothSerialServerThread started.

Failed to init the Bluetooth connection. javax.bluetooth.BluetoothStateException: BlueCove com.intel.bluetooth.BluetoothStackBlueZ not available

We are finally closing the logging.


BlueCove is the Bluetooth library used in the MicroLog server library. It does support Linux, but the difference from the Windows version is that you have to add an additional runtime library called BlueCove-GPL. First, to get Bluetooth on Ubuntu working with BlueCove you have to download and install an extra package. Open up Synaptic Package Manager or a terminal window and install the “libbluetooth-dev” package. When this is installed go to http://www.bluecove.org and download the Java library. I would recommend the snapshot version (2.1.1) since the current stable version (2.1.0) has a few problematic bugs. Place these two files together with the Microlog server application.

The next step is to create a shell script that will start the server application. Open a terminal window and type “gedit start-microlog-server.sh” (you can call the file whatever you want). Copy and past this into the file:

java -cp bluecove-2.1.1-SNAPSHOT.jar:bluecove-gpl-2.1.1-SNAPSHOT.jar:microlog-servers-2.0.0-SNAPSHOT-jar-with-dependencies.jar net.sf.microlog.server.btspp.MicrologBtsppServer

Make sure all the version numbers are correct and that you have the jar-files in the same folder as the script. If you choose to use the microlog-servers-2.0.0-SNAPSHOT.jar file you have to update the script with this file. Also, if you get an error message like this:

Failed to init the Bluetooth connection. javax.bluetooth.BluetoothStateException: Bluetooth Device is not ready. [1] Operation not permitted

Try to add gksu in front and "-around the java -cp....

This will start the MicroLog Bluetooth server application with the correct BlueCove jar-files. Save and close gedit. This is the script that Eclipse will run and therefore it needs to be executable. In the terminal write “chmod u+x start-microlog-server.sh”. You can now test that the script works by entering the following command in the terminal “sh start-microlog-server.sh”. You should see something like this:

BluetoothSerialServerThread started.

BlueCove version 2.1.1-SNAPSHOT on bluez

Waiting for a client to connect to:
btspp://000000000000:;authenticate=false;encrypt=false;master=false
where should be replaced with the channel that is actually in use. This is usually a low number 1 or 2.


Integrate MicroLog with Eclipse

The second task is to get this integrated in Eclipse. It is good to have access to the terminal application, but much nicer to get the log output in Eclipse. All you need to do is add a new “External tools” configuration.

Press the “New launch configuration” and enter a name, like “microlog bluetooth server”. In the location input field, press either “Browse Workspace” (like in the picture below) or “Browse File System”. This depends on where you have saved the shell script. Select the “start-microlog-server.sh” file and press OK. In the working directory select the folder where the shell script is and press OK. The working directory must be the same folder as where all the jar-files were saved. Finally press Apply to save the changes.

Now you should be able to run the “External tools” configuration and start the MicroLog server in Eclipse. The output should be similar to this:

This has been a great help for me since I really like having everything included in Eclipse. Hopefully this can help others to get MicroLog up and running on Linux and in Eclipse as well. Please do not hesitate to add a comment if you have any questions.