Archive for the 'How to' Category

Log

One of the problems of developing for mobile phones is that functionalities that work in the emulators don’t work in the real devices. Some devices manufacturers offer on device debug functionality for their SDKs but the majority don’t.

On regular Java development programmers normally use the “System.out.println” to print variables, state information or even exceptions. The problem with this approach is that it doesn’t help on the actual devices because there isn’t any console to print on.

After trying different methods on several projects I finally ended up creating a small static class called Log. This simple class as one main method:

  public static void log(String text) {
    if (logText == null) {
      logText = new String();
    }
    Date date = new Date();
    logText = date.toString() + ":" + text + "\n" + logText;
    // also print to console in simulators
    System.out.println(text);
  }

So now in my projects instead of using the “System.out.println” i always use the Log.log method.
But now you ask how can i see this information in the device? for that i added a new method that creates a form and shows it on the screen.

public static Displayable showConsole(){
    if (formLogConsole == null) {
      formLogConsole = new Form("Log");
      itemLog = new StringItem("Log:", "");
      formLogConsole.append(itemLog);
      formLogConsole.addCommand(new Command("Back", Command.BACK, 1));
      formLogConsole.addCommand(new Command("Clear", Command.ITEM, 1));
      formLogConsole.setCommandListener(instance());
    }
    current = Display.getDisplay(midlet).getCurrent();
    itemLog.setText(Log.logText);
    return formLogConsole;
  }

So whenever i need to check the console i just call the method Log.showConsole() and there it’s a form with all my logging info. I normally associate this action to a key event.
After you check the log you just press back option and return to your previous “Displayable”.

In order to use this functionality you just need to set the midlet variable of the log once in your project. I normally do this in the constructor of the project Midlet.

I also created some helper methods for handling logging of exceptions, you can check them out in the full code.

Happy logging and see you soon.

Downloads:

Optimization

There are three key factors to considered when you want to optimize your Java ME applications

  • Performance
  • Size

You should delay optimization until the last minute, after you create the main features of our application, but you need to keep in mind all the key factors throughout the development cycle to avoid huge changes at the end.

Continue reading ‘Optimization’

Read location from Bluetooth GPS

One of the most sold accessories for mobile phones and PDAs are Bluetooth GPS (BT-GPS). These simple devices connect to the GPS satellite system and allows to pinpoint your position with 5 meters precision. It’s possible to access this information trough a JavaME application you just need to have Bluetooth phone with the JSR 82 Bluetooth API available on it.

To read location information from a bluetooth GPS, we need to implement the following tasks:

  1. Search for the Bluetooth GPS device
  2. Connect to the GPS device
  3. Read and Parse NMEA sentences

Continue reading ‘Read location from Bluetooth GPS’

Search for Bluetooth devices and services

To use the Bluetooth protocol on an JaveMe application your mobile device must implement the JSR 82 Bluetooth API.
One of the main challenges on bluetooth applications is to find out the connection address, for devices with an specific services available on them.

In the following sample we are going to see how to implement this code using an class called BtManager.
Continue reading ‘Search for Bluetooth devices and services’