Sun Java System Mobile Enterprise Platform 1.0 Developer's Guide for Client Applications

Developing Client Applications for the BlackBerry Using NetBeans IDE

This guide cannot describe how to develop client applications for every possible device using every possible development tool. This section, however, describes how to develop a client application for one of the most commonly used devices, the BlackBerry, using one of the most commonly used development tools, NetBeans IDE. It contains the following sections:

Prerequisites

Before you can develop a client application for the Blackberry using NetBeans IDE, you must install the following software on a Microsoft Windows system:


Note –

You must have a BlackBerry Developer Community account in order to download the BlackBerry software. If you do not have an account, follow the instructions on the website to obtain one.


ProcedureTo Configure BlackBerry JDE v4.2.1

  1. Click Start->All Programs->Research In Motion->BlackBerry JDE 4.2.1->JDE.

  2. Click Edit->Preferences.

  3. Click the Simulator tab and perform these steps:

    1. Select the 8800-JDE Profile from the pull-down menu.

    2. Select the Launch simulator checkbox.

    3. Select the Launch Mobile Data Service (MDS) with Simulator checkbox.

  4. Click the MDS Simulator Tab. Make sure that the MDS Simulator directory location is pointing to the v4.1.4 MDS directory you installed. For example:

    C:\Program Files\Research In Motion\BlackBerry Email and MDS Services Simulators 4.1.4\MDS
  5. Click OK.

    You can leave the JDE running, because you may need it later on.

ProcedureTo Configure NetBeans IDE for BlackBerry Application Development

  1. Start a text editor and copy the following text into an empty file.


    Note –

    If you installed BlackBerry JDE in a non-default location (for example, not on the C:\ drive), edit the contents of the home property setting for the platform element.

    Make sure that the contents of the preverifycmd property setting for the platform element all appear on one line of the file. The contents are broken up here for readability only.


    <?xml version='1.0'?>
    <!DOCTYPE platform PUBLIC '-//NetBeans//DTD J2ME PlatformDefinition 1.0//EN' 
              'http://www.netbeans.org/dtds/j2me-platformdefinition-1_0.dtd'>
    <platform name="BlackBerry_JDE_421" 
              home="C:\Program Files\Research In Motion\BlackBerry JDE 4.2.1" 
              type="CUSTOM" 
              displayname="BlackBerry JDE 421" 
              srcpath="" 
              docpath="${platform.home}/docs/api," 
              preverifycmd="&quot;{platformhome}{/}bin{/}preverify&quot; 
                            {classpath|-classpath &quot;{classpath}&quot;} 
                            -d &quot;{destdir}&quot; &quot;{srcdir}&quot;" 
              runcmd="cmd /C &quot;cd /D {platformhome}{/}simulator&amp;{device}&quot;" 
              debugcmd="cmd /C &quot;cd /D {platformhome}{/}bin&amp;jdwp&quot;">
        <device name="8800" description="8800">
            <optional name="JSR75" version="1.0" 
                      displayname="File Connection and PIM Optional Packages" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <optional name="MMAPI" version="1.0" 
                      displayname="Mobile Media API" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <configuration name="CLDC" version="1.1" 
                           displayname="Connected Limited Device Configuration" 
                           classpath="${platform.home}/lib/net_rim_api.jar" 
                           dependencies="" default="true"/>
            <optional name="OBEX" version="1.0" 
                      displayname="Object Exchange APIs" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <optional name="JSR82" version="1.0" 
                      displayname="Java APIs for Bluetooth Wireless Technology" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <optional name="WMA" version="1.1" 
                      displayname="Wireless Messaging API" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <optional name="JSR179" version="1.0" 
                      displayname="Location Based APIs" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <optional name="JSR177" version="1.0" 
                      displayname="Security and Trust Services APIs" 
                      classpath="${platform.home}/lib/net_rim_api.jar" 
                      dependencies="" default="true"/>
            <profile name="MIDP" version="2.0" 
                     displayname="Mobile Information Device Profile" 
                     classpath="${platform.home}/lib/net_rim_api.jar" 
                     dependencies="" default="true"/>
        </device>
    </platform>
  2. Save the file, giving it the name BlackBerry_JDE_421.xml.

  3. Copy the file to the following location in your home directory under C:\Documents and Settings:

    .netbeans\6.1\config\Services\Platforms\org-netbeans-api-java-Platform
  4. If NetBeans IDE is running, stop it.

    You will be prompted to start (or restart) NetBeans IDE in the next task, To Import the SecureMusicDB Sources into NetBeans IDE as a BlackBerry Project.

Next Steps

After you start NetBeans IDE, The Blackberry JDE will appear in the list of platforms when you choose Java Platforms from the Tools menu.

ProcedureTo Import the SecureMusicDB Sources into NetBeans IDE as a BlackBerry Project

To build and run a SecureMusicDB project for the BlackBerry from sources in NetBeans IDE, follow these steps.

  1. To obtain the MEP client library bundle, go to the following URL: http://www.sun.com/software/products/mep/get.jsp.

  2. Click Download, provide the requested information, then click Log In and Continue.

  3. Download the sjsmep-client-1_0_02-fcs.zip bundle.

  4. Unzip the bundle in a location of your choosing (for example, under C:\).

  5. Start NetBeans IDE.

    The first time you start NetBeans IDE, you are prompted to install some updates. Install them.

  6. In NetBeans IDE, follow these steps to create a Mobility Project and import the secure-musicdb sources.

    1. From the File menu, select New Project.

      The Choose Project screen appears.

    2. Click Mobility, then click Mobile Project from Existing MIDP Sources.

    3. Click Next.

      The Specify MIDP Sources Screen appears.

    4. In the Sources Location field, specify the location of the secure-musicdb sources in the unzipped bundle. For example, if you unzipped the bundle to the C:\ directory, specify the following:

      C:\sjsmep-client-1_0_02-fcs\samples\mcbo\secure-musicdb\src
    5. Click Next.

      The Name and Location Screen appears.

    6. Type a name for the Project or keep the default name.

    7. Click Next.

      The Default Platform Selection Screen appears.

    8. Set the Emulator Platform to “BlackBerry JDE 421” and verify that the Device is 8800.

    9. Click Finish.

      The project appears in the Projects pane.

  7. To add the file mep_client_api.jar to the supported Libraries & Resources, follow these steps.

    1. Right-click the project and select Properties.

    2. Click Libraries & Resources.

    3. Click Add Jar/Zip.

    4. Browse to the location of the unzipped bundle above the lib directory to add mep_client_api.jar.

      For example, if you unzipped the bundle to the C:\ directory, the file name would be C:\sjsmep-client-1_0_02-fcs\lib\BlackBerry\mep_client_api.jar.

    5. Click OK.

    6. Click the Files tab (next to the Projects tab) and open the project.properties file under the nbproject directory.

    7. Edit the file.reference.mep_client_api.jar property to contain the fully qualified path name of the mep_client_api.jar file.

      For a BlackBerry project, the pathname must be absolute, not relative.

      For example, if you unzipped the bundle to the C:\ directory, edit the property definition to look like this:

      file.reference.mep_client_api.jar=C:/sjsmep-client-1_0_02-fcs/lib/BlackBerry/mep_client_api.jar

      Use forward slashes (/) instead of the usual Windows file separator.

  8. Click the Files tab and open the project's build.xml file.

  9. Add the following code fragment immediately before the </project> tag at the end of the file:

        <target name="do-preprocess">
            <fail unless="libs.ant-contrib.classpath">
         Classpath to Ant Contrib library (libs.ant-contrib.classpath property) is not set.
            </fail>
            <taskdef resource="net/sf/antcontrib/antlib.xml">
                <classpath>
                    <pathelement path="${libs.ant-contrib.classpath}"/>
                </classpath>
            </taskdef>
            <available file="${platform.home}/bin/rapc.exe" property="do.rapc"/>
            <if>
                <isset property="do.rapc"/>
                <then>
                    <property name="jpda.port" value="8000"/>
                    <path id="antlib.classpath">
                        <fileset dir="${user.dir}/mobility8/modules/ext/" 
                                 includes="ant-contrib-1.0b3.jar"/>
                    </path>
                    <mkdir dir="${dist.dir}"/>
                    <path id="src-files">
                        <fileset dir="${src.dir}" includes="**/*.*"/>
                    </path>
                    <property name="srcs" value="${toString:src-files}"/>
                    <for list="${srcs}" param="file" delimiter=";" trim="true">
                        <sequential>
                            <echo message="@{file}${line.separator}" 
                                  file="${src.dir}/${name}_build.files" append="true"/>
                        </sequential>
                    </for>
                    <touch file="${dist.dir}/${dist.jar}"/>
                    <nb-overrideproperty name="buildsystem.baton" 
                                         value="${preprocessed.dir}"/>
                </then>
                <else>
                    <nb-overrideproperty name="buildsystem.baton" value="${src.dir}"/>
                    <antcall target="${name}-impl.do-preprocess"/>
                </else>
            </if>
        </target>
        <target name="do-compile">
            <if>
                <isset property="do.rapc"/>
                <then>
                    <antcall target="create-jad"/>
                    <antcall target="update-jad"/>
                    <copy file="${dist.dir}/${dist.jad}" toDir="${src.dir}"/>
                    <exec dir="${src.dir}" 
                          executable="${platform.home}/bin/rapc.exe" failonerror="true">
                        <arg value="-quiet"/>
                        <arg value="import=${platform.bootclasspath};${libs.classpath}"/>
                        <arg value="codename=${name}"/>
                        <arg value="-midlet"/>
                        <arg value="jad=${dist.jad}"/>
                        <arg value="@${name}_build.files"/>
                    </exec>
                    <delete file="${basedir}/${src.dir}/${name}_build.files"/>
                    <copy file="${name}.alx" todir="${dist.dir}"/>
                    <nb-overrideproperty name="buildsystem.baton" 
                                         value="${build.classes.dir}"/>
                </then>
                <else>
                    <nb-overrideproperty name="buildsystem.baton" 
                                         value="${preprocessed.dir}"/>
                    <antcall target="${name}-impl.do-compile"/>
                </else>
            </if>
        </target>
        <target name="pre-obfuscate">
            <nb-overrideproperty name="buildsystem.baton" value="${build.classes.dir}"/>
        </target>
        <target name="post-jar" if="do.rapc">
            <move todir="${dist.dir}">
                <fileset dir="${src.dir}">
                    <include name="**/${name}*.*"/>
                </fileset>
            </move>
            <copy todir="${platform.home}/simulator" verbose="true">
                <fileset dir="${dist.dir}">
                    <include name="**/${name}*.*"/>
                </fileset>
            </copy>
        </target>
        <target name="post-clean">
            <delete failonerror="false" includeemptydirs="true">
                <fileset dir="${platform.home}/simulator">
                    <include name="**/${name}*.*"/>
                </fileset>
                <fileset dir="${dist.dir}">
                    <include name="**/*.*"/>
                </fileset>
                <fileset dir="${src.dir}">
                    <include name="**/${name}*.*"/>
                </fileset>
            </delete>
        </target>
  10. Create an .alx file for this project.

    1. Click the Files tab.

    2. Right-click your project and select New->Other.

    3. In the Choose File Type screen, click Other, then click Empty File.

    4. Click Next.

    5. In the Name and Location screen, give the file the same name as your project, with the extension .alx.

      For example, if bb-secure-musicdb is the project name, name the file bb-secure-musicdb.alx.

    6. Click Finish.

      The empty file opens.

    7. Copy and paste the following text into the file, replacing myProject with your project name, and including any vendor and copyright information needed for your application.

      <loader version="1.0">
          <application id="myProject">
              <name>
                  myProject
              </name>
              <description/>
              <version>
                  1.0
              </version>
              <vendor>
              </vendor>
              <copyright>
              </copyright>
              <fileset Java="1.3">
                  <directory/>
                  <files>
                      myProject.cod
                  </files>
              </fileset>
              <application id="mep_client_api">
                  <name/>
                  <description/>
                  <version>
                      1.0
                  </version>
                  <vendor>
                      Sun Microsystems Inc.
                  </vendor>
                  <copyright>
                      Copyright (c) 2008 Sun Microsystems Inc.
                  </copyright>
                  <fileset Java="1.3">
                      <directory/>
                      <files>
                          mep_client_api.cod
                      </files>
                  </fileset>
              </application>
          </application>
      </loader>
    8. Save and close the file.

  11. Copy the file mep_client_api.cod from the directory C:\sjsmep-client-1_0_02-fcs\lib\BlackBerry to the simulator directory of the BlackBerry JDE (for example, C:\Program Files\Research In Motion\BlackBerry JDE 4.2.1\simulator).

  12. Click the Projects tab, then right-click your secure-musicdb project and select Clean & Build.

  13. Right-click your secure-musicdb project and select Run.

    The BlackBerry Device Simulator appears.


    Note –

    When you select Run, NetBeans IDE automatically loads the application on the Simulator using the .jad and .jar files (not the .cod file). To load the application from the .cod file created, use the Load Java Program option in the Simulator.


  14. Launch the MepSecureJdbcMIDlet application and perform a Sync.


    Note –

    The MDS must be running for the client to perform syncs. If you started the JDE, MDS should get launched automatically. Otherwise, start MDS manually as follows: From the Windows Start menu, choose All Programs->Research in Motion->BlackBerry Email and MDS Services Simulators 4.1.4->MDS.


Next Steps

To remove the application from the Simulator, delete the .jad, .jar, and .cod files from the Simulator directory within the JDE and execute the three erase options in the JDE under File->Erase Simulator File.

ProcedureTo Create a New BlackBerry Project to Use the MCBO API

To create a new NetBeans IDE project that uses the MCBO API, follow these steps.

  1. In NetBeans IDE, choose New Project from the File menu.

  2. Choose Project Screen.

  3. Click Mobility->MIDP Application.

  4. Click Next.

  5. In the Name and Location screen:

    1. Type a name for the project or keep the default name.

    2. Select Set as Main Project.

    3. Select Create Hello MIDlet.

    4. Click Next.

  6. In the Default Platform Selection Screen:

    1. Specify BlackBerryJDE421 as the Emulator Platform.

    2. Specify 8800 as the Device.

    3. Click Finish.

  7. Add the mep_client_api.jar file to your Libraries & Resources for this project in order to call and have access to the MCBO API.

    1. If you have not done so before, go to http://www.sun.com/software/products/mep/get.jsp and download the sjsmep-client-1_0_02-fcs.zip bundle.

    2. Unzip the sjsmep-client-1_0_02-fcs.zip bundle (for example, under C:\).

    3. In NetBeans IDE, right-click Project->Properties.

    4. Click Libraries & Resources.

    5. Click Add Jar/Zip.

    6. Browse to the location of the unzipped bundle above the lib directory to add mep_client_api.jar.

      For example, if you unzipped the bundle to the C:\ directory, the file name would be C:\sjsmep-client-1_0_02-fcs\lib\BlackBerry\mep_client_api.jar.

    7. Click the Files Tab (next to the Projects tab) and open the project.properties file under the nbproject directory.

    8. Edit the file.reference.mep_client_api.jar property to contain the fully qualified path name of the mep_client_api.jar file.

      For a BlackBerry project, the pathname must be absolute, not relative.

      For example, if you unzipped the bundle to the C:\ directory, edit the property setting to look like this:

      file.reference.mep_client_api.jar=C:/sjsmep-client-1_0_02-fcs/lib/BlackBerry/mep_client_api.jar

      Use forward slashes (/) instead of the usual Windows file separator.

    9. Click the Files Tab and open the project's build.xml file. Immediately before the </project>tag at the end of the file, add the same code fragment you added in Step 9 of To Import the SecureMusicDB Sources into NetBeans IDE as a BlackBerry Project.

  8. Create an .alx file for this project:

    1. Click the Files tab.

    2. Right-click your project and select New->Other.

    3. In the Choose File Type screen, click Other, then click Empty File.

    4. Click Next.

    5. Give the file the same name as your project name, with the .alx extension.

    6. Click Finish.

    7. Copy and paste into the file the content from Step g under Step 10 of To Import the SecureMusicDB Sources into NetBeans IDE as a BlackBerry Project, replacing myProject with your project name.

    8. Save and close the file.

  9. Copy the file mep_client_api.cod from the directory C:\sjsmep-client-1_0_02-fcs\lib\BlackBerry to the simulator directory of the BlackBerry JDE (for example, C:\Program Files\Research In Motion\BlackBerry JDE 4.2.1\simulator).

  10. Click the Projects tab, then right-click your project and select Clean & Build.

  11. Right-click your project and select Run.

    The BlackBerry Device Simulator appears.


    Note –

    When you select Run, NetBeans IDE automatically loads the application on the Simulator using the .jad and .jar files (not the .cod file). To load the application from the .cod file created, use the Load Java Program option in the Simulator.


  12. Launch the MIDlet application.

Next Steps

At this point, you have a boilerplate application that says Hello World. You can now add code to implement and call the MCBO API classes and methods, and you can edit the MIDlet code to provide a user interface and to perform synchronizations.