Background Midlet

I recently made a small discover in JavaMe that allows an application to work in the background.
You simple set the current display to null and the application will be set to the background. The interface thread will be stopped but it will keep executing any background thread that you have active.

Display.getDisplay(this).setCurrent(null);

This works on Nokia S60 phones at least from series 2 up, but it doesn’t work on S40 phones. Can somebody tell me if this works on other phone models?

See you soon

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:

Lesson 7 – Network

Now that we have our Arkanoid single player experience complete is time to connect to rest of the world. After all better than show off our high-scores to our friends is to show them to everybody in the Internet!

One of the main features of mobile devices is the ability to be connected almost everywhere anytime. Java ME allows easy access to these communications features of your device:

  • HTTP
  • Socket
  • SMS
  • Bluetooth

During this lesson we are going to learn to use a HTTP connection to send our highscores data to a central server and to use the SMS functionality to invite other friends to play our Arkanoid game.
Continue reading ‘Lesson 7 – Network’

2008 – News

Hi guys,

I’ve been somewhat quite for the last couple of months, the reason of my silence was a very busy holiday season and a beginning of the year with a lot of work.

Now that i have some free time i restarted to write for MIDP adventures, i already have ready one more lesson about networking and hope to write two more articles, one about “log and debug” and another about “post multipart forms”.

I you have any requests for new articles tutorials feel free to send me an email or a comment.

See you soon

P.S: One of the gifts i received this Christmas has a brand new N95 from the Nokia folks (thanks Ron). Now i can test some new features this device has: internal GPS, motion sensor and hopefully write some articles about these.

Lesson 6 – Multimedia

In our last lesson we learned how to save our game settings, the Sound On/Off screen, but why we have this option, if we don’t have any sound in our game? It’s time to learn about Java Mobile Multimedia API (MMAPI) and add some sound to our game. Let’s rock!

MMAPI offers a set of multimedia capabilities for mobile devices, including playback and recording of audio and video data from a variety of sources. Of course, not all mobile devices support all the options, but MMAPI is designed in such a way that it takes full advantage of the capabilities that are available, while ignoring those that it cannot support.
Continue reading ‘Lesson 6 – Multimedia’

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’

Lesson 5 – Files

After our last lesson we had a completely functional Arkanoid midlet but one important thing is still missing, can you guess what? To save the high scores! Until know our high scores were lost each time we exit the application (the same happens for the game settings). What’s the point of playing our game if we cannot save the scores to show off to our friends!!!

To implement this functionality you need to understand how the Midlet Input Output works and then how to use RecordStores.

Continue reading ‘Lesson 5 – Files’

Lesson 4 – Images, Sprites and Tiles

In our last lesson, we completed our GameCanvas class with all the main interaction between elements. Now that we have all gameplay elements build it’s time to improve the look of our game.
The idea is to use some images files to represent our game entities instead of using Graphics draw/fill methods.
With this in mind I created some images, based on the free SpriteLib resource pack, to use in our game.

Arkanoid - Graphics
Continue reading ‘Lesson 4 – Images, Sprites and Tiles’