Java ME Logging over Bluetooth using MicroLog

When developing in Java ME and wanting to support several different devices, you wil no doubt run into problems with devices behaving differently. In some cases it is only the appearance that changes and in others the actual functionality varies or breaks. The first step in solving this is to run your Midlet in the emulators for the devices that you want to support and hopefully you will be able to solve some of the problems here, but unless you are really lucky there will be some problems remaining that needs to be debugged on the actual device. Depending on which device it is you might be able to run On Device Debugging to solve it, but in many cases you don’t have that option or the On Device Debugger is not stable enough. So now you are left with logging as your only tool to debug the midlet. Logging on a small screen device with very limited ram is not trivial however.

What you really want is to have the midlet write the log to your development PC instantaneously and free of charge, then you can log whatever you want to pinpoint your bug. This could be accomplished by logging over Bluetooth. Previously you had to write your own code to handle the logging and Bluetooth communication as well as a server to which the midlet connects and sends the log messages, but now there is a powerful logging framework for Java ME that offers this support out of the box. The framework is MicroLog and is open source under the Apache Software License, so it is free to use as you see fit. MicroLog can be configured with one or more appenders that takes care of the logging messages and sends them to the right destination and each appender can have its own formatter to format the messages appropriately. This is very similar to log4j, so if you have used log4j before, MicroLog is very easy to learn. If you are using Maven 2 you will be happy to know that MicroLog is built using Maven 2 and is available in a public repository.

Setting up MicroLog to use Bluetooth and log to your PC is simple. It only requires that you setup your Logger:

and that you configure it to use a BluetoothSerialAppender, which is done like this:

Now your midlet is all set to start logging over Bluetooth. For a full midlet example see the following code:

Build and deploy this midlet to your Bluetooth enabled device, supporting JSR-82, the Bluetooth API and make sure that Bluetooth is turned on.

Next you need to get the Bluetooth server that will receive and print the logging messages to standard out. The server is also available in a public Maven repository. Just download the version that ends with -jar-with-dependencies.jar. This file contains all its dependencies. Now make sure that your Bluetooth unit is on and discoverable on your PC. Then start the server with the following command:

You might have to change the jar file name if it is not the same as the one you downloaded, then just replace it with the file name of your own microlog-server jar file.

The server starts up and registers a logging service with a unique UUID that the phone will try to locate and connect to. Now start the midlet and see how it will start logging over the Bluetooth connection.

The first phone I tested was a SonyEricsson C905 and it worked perfectly from the start. Then I tried to use a SonyEricsson P990i and for some reason it failed to locate the logging server with the unique UUID. I was unable to determine the reason why it failed to locate the service, so instead I added support to the BluetoothSerialAppender to specify the url of the Bluetooth logger server. Using this and setting the server url to my PC it connected flawlessly to it and started logging. So if you experience problems when your midlet connects to the PC or if you are using multiple logger servers within the Bluetooth range of your device try specifying the server url that the BluetoothSerialAppender should connect to. The url of the server is shown when starting the logger server. This url does however contain a channel that is not possible to determine through the used API’s, but it does start on 1 and increases. For each session I always got the same channel and it was only the next day that it changed and I got channel 2 instead, so try with 1 to start with and if that does not work increase it by one and try again. It will most likely work first or second time.

Now you are free to log as much as you want and do not have to restrict your logging during the development phase of your project. Neither is there a reason for writing your own Bluetooth logger, just use MicroLog.

9 Comments

  1. Hi,

    I need a Java ME developer, please contact me.

  2. Yunnyyy

    Just one note.
    From this point it wouldn’t be really easy to handle that exception what is thrown if the channel we specified is not the proper one, and increment the channel number automatically instead of the manual change in the property file and recompilation?

    For me the automatic detection wasn’t working on s60 devices, so this is a change would be more than welcome….
    Thanx anyway… Really useful and great job.

  3. I got the debugger working in a matter of minutes with a SonyEricsson W760i connecting to a MacBook running OS X 10.5.6

    Really useful.

  4. Currently having a lot of problems with the Bluetooth debugger while connecting to our server: it reboots the phone every time after a few calls. Sony Ericsson W760i. I haven’t seen what exactly causes the reboot.

  5. A simple alternative is to use the file appender:

    Change the file name to be something other than at the root level as most phones do not allow this:

    FileAppender appender = new FileAppender();
    appender.setFileName(“Other/microlog.txt”);

    The FileAppender class always chooses the first value returned from FileSystemRegistry.listRoots() so check the folders that are available internally in the phone.

  6. Paul

    Hi,

    I’ve found your blog post while searching for a logging possibility in J2ME. I gave it a try, but on my Vista x64 machine it doesn’t work. I’ve added the new libraries (2.0.0), and start also the new server version with the gui. But the status line says that no server has started and no url is displayed.
    Im using a Bluetooth dongle on my machine and a n96 (and of course the WTK emu) no combination works.

    Has any body a hint?

  7. Sreejith

    I’ve tried on a Windows 7 x64 machine. Symbian 60 device was not able to autodetect, however does work fine with an S40 device

  8. Java me was designed by sun microsystems, now a subsidiary of oracle corporation. This platform replaced a similar technology, personaljava. Sun provides a reference implementation of the specification, but has tended not to provide free binary implementations of its runtime environment for mobile devices, rather relying on third parties to provide their own. Thanks for sharing.

  9. The website with dynamic code refers to its construction or how it is built, and more specifically refers to the code used to create a single web page. A dynamic web page is generated on the fly by piecing together certain blocks of code, procedures. Thanks.

Leave a Reply