Developing in Mac OSx

In the last months I have being busy building a application for the iPhone using the offical Apple iPhone SDK.

I think this new plataform and the bussiness model associated is one step in the right direction. It still has some issues, like the impossibility to create  background applications, but is way ahead of what have available in the Java ME plataform. You can check some tip and tricks in this sister site iPhone Adventures.

Meanwhile because i was assigned a Mac to create applications for the iPhone, I decided to try to develop for MIDP using the Mac OSx. My first idea was to replicate my Windows developing enviroment using Eclipse and EclipseME plug-in, after I had all these  installed I had my first (big) surprise, the Sun-WTK isn’t available for Mac OSx, then I also discovered that the NetBeans mobility version isn’t available, becouse is mobile module is based on the Sun WTK SDK.

After some googling around i found an alternative, MicroEmulator, is an open source LGPL Java implementation of J2ME in J2SE. The problem is that it doesn’t implement all the libraries the WTK supports, so you need to add some extra libraries to have full compability with WTK. Check out the ones below:

  • BlueCove , a open source Bluetooth JSR-82 implementation
  • OpenLApi, a open source Location JSR-179 implementation.

I also found out that Eclipse Me was adopted as the official tool for mobile development in Eclipse, you can check out the new features being developed here and download a RC version in here. I tested it with my projects and it works great.

Update:

After some more trials i discovered that my setup wasn’t 100 percent, i couldn’t built jar/jad packages because I didn’t have a working preverifier. After some more googling around i found the solution for this:

  • Download the Sun WTK for Linux, install it in some folder in your machine.
  • Rename the folder /wtklib/Linux to /wtklib/Mac
  • Donwload and install MPowerPlayer SDK. The official page is crashed but you can use this direct link.
  • Install the MPowerPlayer and go inside the folder osx/preverify and copy the file preverify to you WTK folder inside the bin folder ( it’s a good idea to backup the existing file).
  • After this you can configure you EclipseME/MJT  to use the WTK folder.  This way you can build your project and create the packages.
  • To emulate the application you will use the MicroEmulator, just create a run configuration and specify this emulator.

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 ideia is to use some images files to represent our game entities instead of using Graphics draw/fill methods.
With this in mind i created the some images, based on the free SpriteLib resource pack, to use in our game.

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