18 Nov 2011

Lazy porting - porting J2me apps to Blackberry in 2 easy steps

Our latest work is a mobile app for feature phones and blackberry we have to deliver within a very limited time frame. Due to the time limitation, the fastest route is to develop on a platform (j2me for the feature phones) and port to the other (blackberry) instead of building something different for both. Here I share 2 (3 actual) easy steps to porting j2me apps to BB.

1. Kill the menubar
App UI on Blackberry (and some other OS like Android) mostly doesn't have the normal bottom menu bar you find in java mobile apps. The reason you find them on j2me apps is because supporting feature phones do have left and right soft keys that can be pinned to specific actions on the app. On BB for example, what you'd find is the 'Menu' and 'Back' key. And these however have their own defined functions and is awkward to be attached to a different action. It is therefore clear that clicking the Menu key should access the menu options and the back should go to the previous screen. There is no need of a menu bar to denote this. Since your already made j2me app most probably come with a menubar, the first step is to remove these bars. If there are calculations related to the bar (calculating scrollbars and positions for example), you should adjust as well.

2. Handle the keys
Here is where it gets complex. Ugly truth: your key handling will break in BB especially if you are using a high level UI design. Where keycodes like -6 and -7 mean left and right soft keys on feature phones, they mean something different on the BB. Even Canvas.FIRE isn't triggered for BB trackpad/soft ball click or the Enter key. Canvas.FIRE is attached to the spacebar key :/. Where it gets worst is you can't listen to the menu and back key codes with normal j2me midp UI methods. To handle this, you will have to go low level and use Commands and CommandListner. The menu key lists out attached commands for the screen. BACK, CANCEL and EXIT commands are automatically picked up with the Back key while (with just one command attached to the screen) SCREEN, OK and ITEM commands are picked up by Click action and Enter key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Splash extends GameCanvas implements CommandListener
{
    // ...
    Command OK = new Command("Launch", Command.OK, 1);
    
    public Splash()
    {
        super(false);
        // ....
        this.addCommand(OK);
        setCommandListener(this);
        // ....
    }
    
    //...

    public void commandAction(Command command, Displayable d)
    {
        if (command == OK)
        {
            new Gui();
            return;
        }            
    }
}

[For a sweeter experience, I still tried listening to clicks and enter keys via the keyPressed. Here is my way to it. You don't have to do this though]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//...
import net.rim.device.api.ui.Keypad;

public class Gui extends GameCanvas implements CommandListener
{
    //...
    public void keyPressed(int keyCode)
    {
        if (keyCode == Keypad.KEY_ENTER || keyCode == -8)
        {
            ok();
            return;
        }
        
        //...
    }
}

Also note that if you will be doing this, you will be needing the net.rim.device.api.ui.Keypad class. If you have and using the Blackberry JDE, you already have it. If not, you can download just that library here.

3. [Optional] Convert to cod
While most BB phones support raw jad/jar files, converting to Blackberry's app format (cod and its counterpart application loader file - alx and jad descriptor) is a good thing. There are a couple of ways to do this. If you already have the Blackberry JDE, just migrate your java codes to the toolkit and build from there (if you are not using this already). If you don't have the JDE and don't want to go through the slow/poor download experience, you can download Jar2Cod a little cool utility for this.

And by the way, you can checkout the apps at http://smetoolkit.digitalcraftstudios.com/

 

Looking for a simple marketing automation tool to automate your customer onboarding, retention and lifecycle emails? Check out Engage and signup for free.

 

My name is Opeyemi Obembe. I build things for web and mobile and write about my experiments. Follow me on Twitter–@kehers.

 

Next post: And so I got a BB