Thursday, March 26, 2009

BlackBerry Jam for Beginners

Over the past several weeks I have been teaching myself BlackBerry development. I'm doing this for two reasons. First, I volunteered myself to present at Microsoft Code Camp, which I mentioned in an earlier blog entry. Second, I believe there is money to be made in the world of mobile applications. I have an idea that I would like to develop into an application for sale to the general public. Volunteering to present at Code Camp forced me to spend the time it took to learn this new set of skills.

Knowing absolutely nothing about Java or BlackBerry made my journey into the world of BlackBerry development quite a challenge. After some initial investigation I discovered that there were two different platforms for developing BB applications. MDS (Mobile Data System) applications, which require the BlackBerry Enterprise Server (BES), and CLDC/MIDP applications. MDS applications appear to be easier to design and write, based on information I have read on the BlackBerry site. However, BES is expensive and not available to your everyday BB user. So CLDC/MIDP was my only choice.

After more investigation I discovered that there is more than one development environment for creating CLDC applications. RIM provides the BlackBerry JDE and also a plug-in for Eclipse, which seems to be very popular among Java developers. I did some reading on various Blogs and forums and discovered that, although Eclipse is very popular, it does provide some additional challenges, especially around debugging and use of simulators. I really wanted to keep my learning curve to a minimum, so I chose to use the BlackBerry JDE, even though Eclipse may have provided some very nice editing features.

One of my main goals was to be able to call web services from the BlackBerry. For the application I intend to build, eventually, it will be required, so I decided to make that the goal for my Code Camp presentation. After many long hours of investigation, and many false leads, I discovered that JSR-172 had been implemented in the BlackBerry OS beginning with version 4.3. JSR-172 is a Java standard for accessing web services. I also found an Open Source project called K-SOAP. K-SOAP was built as a way call web services before JSR-172 had been implemented. K-SOAP 2 is supposed to be much easier to use than the original version and also easier than JSR-172. However, since I own a Curve with OS version 4.3 I decided to keep it "all in the family" and go with the built-in JSR-172 implementation.

I have built three sample applications, which demonstrate various facets of BlackBerry development. These are not intended to be an exhaustive demonstration of BlackBerry capabilities, but just to break the ice for those folks out there who are in the same boat I was, with little to no experience on this platform. The first example is a pretty simple application that just shows the basic layout of a BB Java app. The second example, shows how to incorporate localization through the use of resource files. Still, not a big jump ahead, but most mobile applications sold to the general public are sold on websites, which are available anywhere in the world, so... localization may eventually be needed. Also, by using resource files, you gain the ability to set the application title, which is displayed on the BB desktop. The third example makes a pretty big jump, since it incorporates both web service calls, via JSR-172, and also some simple threading. It turns out that threading is required because you can't call a web service using the same thread the processes events for your application. It really makes sense if you think about it, but it was a bit of a surprise when I first ran into it. Also, this is a good place to point out that it works fine without threading when using the simulator. The simulator also allows applications to run without code signing, where certain restricted APIs can only be used, on a real device, after the code is signed. Signing is also required, on a real device, in order to avoid warning messages to users when trying to call web services, something I didn't discover until after I had deployed the application to my Curve.

I'm a big SuperCross fan, and a play-school level, non-competitive, Moto-X rider. So, my sample code may seem a little unusual. No Northwind or Pubs database here. This calls a web service that provides the ability to look up a SuperCross rider by his number. It isn't very sophisticated and the data returned is minimal, but I think it is a good demonstration of the techniques required for mobile applications, and its more fun that Northwind.

I have uploaded my presentation slides and my sample code to my company site. Here's a link to the zip file. I have left the web service running, so anyone who downloads the sample code can see it run. Have fun!

4 comments:

  1. Thanks for the kind words. I intend to keep blogging about BlackBerry, and other software development, but I don't have time right now. I just took a full-time position at Duke Energy and I'm still trying to work the schedule out so I have some free time again. Keep checking back, I am working on a BB application almost every day while I ride the bus. I'm sure I will come across something BB related to blog about.

    ReplyDelete
  2. Marty, thank-you so much for posting this example. I download the zip file and read through the ppt many times. The materials are great and I actually completed the exercise and it worked!

    I used Eclipse Version 3.4.1 with the BB JDE 4.5.0.16. I made 4 attempts at setting the files up on my development machine. The following are the steps that I followed that worked:

    1. Downloaded and extracted your zip file.
    2. Created a folder C:\users\myid\BBSuperCross. This is the folder that I choose when creating a new workspace. I believe it's important that the final folder be named BBSuperCross. I determined this from you ppt, but was not exactly sure how to execute this.
    3. Choose File>Switch Workspace>Other -- browse to the folder noted in 2 above (C:\users\myid\BBSuperCross).
    4. Choose File>New>Project>BlackBerry Project --name the new project "CodeCampMarch09".
    5. Browse to the folder where you extracted the files in step 1 above. Inside is a folder named "CodeCampMarch09". Select all files inside this folder, except for the file named "CodeCampMarch09.jdw". Copy the selected files and paste to the folder c:\users\myid\BBSuperCross\CodeCampMarch09.
    6. Navigate back to Ecilpse and click on Package Explorer, then click on CodeCampMarch09, then select File>Refresh. This should attach all the newly copies files into the project.

    At this point, you can review the code for errors by clicking the "Problems" tab at the bottom of the Eclipse program. I had 2 warnings the time that it worked. The first 3 attempts I had many errors.

    I had problems with the Eclipse simulator tool for the 8330, this seems to be a common problem. Anyway, I downloaded the 2 simulator programs that are noted in your ppt. I believe one is the MDS simulator and the other is the actual 8330 standalone simulator. It took me a couple of times to select the correct code file, which was CodeCampMarch09. Once selected, it worked perfectly, I could view the icon for BBSuperCross from the simulator. I could select riders by number and it would return the rider name from your web service as designed. This is a great example! Thank-you so much!

    ReplyDelete
  3. Jeff,

    Thanks for looking at the sample, and especially thanks for getting it working in Eclipse. I want to begin using Eclipse, but have not had the time to overcome the hurdles and the learning curve. I have an application I'm working on (if infrequently) that I hope to turn into a product. I will defintely try the steps you pointed out and now may be able to make the switch to Eclipse. :)

    ReplyDelete
  4. Marty this is a great example! Thank you for posting it. Seems like there are so few out there for connecting to a web service from a blackberry java application.

    I got the application running in eclipse but get the "Data not loaded, timeout exceeded." message when I choose the "Rider Record" from the menu after choosing rider 1. Think it has to do with the following lines I'm getting in the console window:

    Refreshing connection on send
    Running refresh
    Local port: 19780
    APN: 'rim.net.gprs'
    APN username: 'null'
    APN password: 'null'
    Connection does not exist

    If anyone can help with that I would greatly appreciate it. Thanks again Marty for a great example.

    ReplyDelete