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:
private final static Logger log = Logger.getLogger(TestMidlet.class);
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: