Wednesday, April 21, 2010

Build LWUIT BlackBerry CLDC application

Last week finally I have time to review the Shai's blog  about building LWUIT project on Blackberry platform. At first I tried to follow the step by step in his article, but I am  disappointed because of so many errors of his build.xml. After a couple hours fix, I still could not make the build, so I decided to give up Shai's script, and choose an easier way.
My solution is just using NetBeans and BlackBerry JDE, it is much easier to build a demo.
Here is the step by step description:

Build LWUIT BlackBerry library
1. Checkout the LWUIT source code from SVN, (https://lwuit.dev.java.net/svn/lwuit/trunk), the LWUIT source includes the NetBeans BlackBerry port project.
2. Install JDE 4.7 and Configure BlackBerry support in NetBeans, you can follow this link to configure;
3. Start NetBeans, open the "BlackBerryPort" poject under the "BlackBerry" sub folder of lwuit project source, there are two configuration for this project, the default is for non touch screen build, the "touch" configuration is for touch screen device like STORM.for non touch device, just choose DefaultConfiguration, build the project, you will see BlackberryPort.jad and BlackberryPort.jar in \dist folder, they are the LWUIT library for BlackBerry devices;

Build LWUIT Demo into BlackBerry CLDC application
1. Open Blackberry JDE, create a new workspace, called "lwuitdemo.jdw";
2. add a new project under the workspace, name it "lwuit",  choose its Application type as "Library", import the BlackberryPort.jad and BlackBerryPort.jar file into the project. then build it, the JDE will generate lwuit.cod and update BlackBerryPort.jad file.  
3. download LWUIT 1.3 from sun website, it comes with the famous LWUITDemo source code, open the LWUITDemo project using NetBeans, build it;
4. add another new project in the JDE workspace, name it "lwuitdemo", choose application type as "CLDC Application";
   set up the correct value with title, version and Vendor information;
   next you need to add project dependency to "lwuit" project, right click project, choose "Project Dependencies..", choose "lwuit project";
   copy all the java source code from LWUITDemo src folder, add them into the project, ignore those png files and .res files;
   go to  build\preverified folder, copy all the png files and .res files, add them into your JDE project;
   right click icon.png, choose "use as application icon";
5.Modify the UIDemoMIDlet.java
Made following changes:
make sure the UIDemoMIDlet extends from uiApplication, like this:
public class UIDemoMIDlet extends net.rim.device.api.ui.UiApplication  {...}
Add the following methods, copied from Shai's blog:
public static void main(String[] argv)
    {
        new UIDemoMIDlet().startApp();
    }
 
    public void notifyDestroyed()
    {
        System.exit(0);
    }
 
    public void platformRequest(String s) {
       net.rim.blackberry.api.browser.Browser.getDefaultSession().displayPage(s);
    }
    
In startApp() method, comment out the following statement, because non touch device need to disable the virtual keypad:
// VKBImplementationFactory.init();
6. Build the lwuitdemo project.
7. Sign both lwuitdemo.cod and lwuit.cod fle, load them both into your blackberry device.
8. If you want to provide OTA download, that is a litttle bit tricky:
   You need to download two artifacts, one for lwuit libray, the other for lwuitdemo, which means user need to download two links:
   1) lwuti library:
      BlackBerryPort.jad
      lwuit.cod
      lwuit-1.cod
      ...
   2) lwuitdemo application:
      lwuitdemo.jad
      lwuitdemo.cod
      lwuitdemo-1.cod
      ...


You can download the whole source code of the JDE project from here.


In future I would like to share:
  1. Provide an updated build.xml based on Shai's blog;
  2. provide how to build LWUITDemo MIDlet project for BlackBerry device.

Thursday, April 8, 2010

How to send email in BlackBerry Simulator

Recently I am working on a BlackBerry Email app, I need to setup a test environment so I can test mail send in the BlackBerry simulator.
I did some research, I read the RIM related document it does not help, then I find a solution from a good book about blackberry - Advanced BlackBerry Development, by Chris King.
I just follow the method from the book , and finally get it working, the solution is quite straight forward.

You need BlackBerry ESS ( Email Server Simulator) in order to simulate mail from your simulator, it comes with the JDE SDK and JDE component, currently I used JDK 4.5 , I prefer it because it's simulator is much faster to launch.


There are two mode you can choose - Connected mode or Standalone mode.
I tired the connected mode, it does not working.
The book recommend using the Standalone mode:  ESS will listen the localhost port, send out email to your smtp email client. If you set up your email client, choose localhost as server address, and configure the same pop3 and smtp port as ESS configured, your email client will receive the email.
 Choose "Standandalone mode",  set Pop3 port and Smtp port,  I choose default value, which are 995 and 465.
Click Launch, the ESS will start, and you will say a dos command window displaying the server log information.

Then we need to configure your email client,  I choose OutLook Express.
Add a new account,  you can set up any email address,
Set localhost  for both SMTP and POP3 server.
for POP3 port number ,use the same value of ESS: 465
for SMTP port number, use the same value of ESS:995


So far the configuration is finished, all you can do is launching the simulator.
You can start the blackberry email client, type any address,  then send.  Check your OutLook Express, you will find an incoming email.
And you can write an email from your outlook express, type any address, then send it.  You will find the email in your blackberry simulator.
The only drawback is you can only send and receive email from your local machine. which is not real, but it is good enough to test the mail function in the simulator.
It is really good, I tested over 100 email using the solution above.
Finally I want to talk about a little about the book, it covers many good topics, such as BES, BIS which confuses me for a long time.  Even I haven't finished it, but I think it is one of the best  blackberry book so far.