Background Applications on the BlackBerry

Useful articles about background applications on the blackberry.

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/796557/800738/800901/How_To_-_Setup_an_alternate_entry_point_for_my_application.html?nodeid=800820

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800639/How_to_-_Make_a_running_UI_application_go_to_the_background_and_resume_in_the_foreground.html?nodeid=800545&vernum=0

http://docs.blackberry.com/en/developers/deliverables/6625/Dimensions_for_screens_images_and_icons_476251_11.jsp

http://rim.lithium.com/rim/board/message?board.id=java_dev&message.id=2519

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=392

Blackberry JME has very limited threads

I had written a CRON scheduler for the blackberry for one of my clients.  This CRON scheduler was failing seemingly randomly where the thread that checks the schedules would just die.  In order to make sure a task is launched exactly when it should, I would spin off a thread and execute that code.  You can see where this is going right?  It worked just fine under my development tests.  When it went to QA, it would fail.  After tracing it down, it was failing on the line:

thread.start();

After finding the exact line of code where it was crashing, I surrounded this line of code with a try/catch on Throwable with this code:

System.out.println(“Error: ” + exc.getClass().toString() + ” = ” + exc.getMessage());

The output from this was:

Error: class net.rim.vm.TooManyThreadsError = null

Which of course led me to my problem.  Now there is no mention of a limitation in the Blackberry documentation telling me how many threads I can have.  In hind sight, I should have know given that even in desktop apps you should use threads sparingly.  These threads I created were supposed to be very short lived.  They spin up, do their action and then go away.  In my testing that’s exactly what was happening but I wasn’t stressing the system with lots of tasks.  Consequently under real-world use, trying to start that last thread was killing the thread that created it.

I can’t simply use the ThreadPool class provided in java.util.concurrent because that is not available in J2ME/JME.  I just created my own based on an article written by IBM about Thread pools and work queues.  I had to use Vector instead of a linked list (didn’t feel like writing my own but I should because removing an element from a vector is expensive and I have to do it every time I pop an element out).  Worked like a champ!

Java ME Date add functionality

Programming for the blackberry can be frustrating.  It seems that they have taken every useful function I’ve used in the past and thought “is there a harder way to do this already?”  If the answer was yes, they ripped out the function.  The latest example I’ve uncovered while developing is simply adding time and dates to arrive at some time in the future.  I am trying to add a number of seconds to an existing timestamp.  In standard Java, there is a handy add function that does this for you.  Unfortunately, in Java ME, they have ripped it out.  To get that functionality back, you need to understand when you call getTime() on a date, it returns the number of milliseconds since January 1, 1970, 00:00:00 GMT as a long. The value “1″ represents one millisecond.  So, to add one day to your date, you simply need to determine how many milliseconds in one day and add that to the value returned by getTime().

int milliseconds = 1 * 1000 * 60 * 60 * 24;  // 1 day * 1000 milliseconds/second * 60 seconds/minute * 60 minutes/hour * 24 hours/day
Date newDate = new Date(myDate.getTime() + milliseconds);

This will add one day to your date.

Making an HTTP Request on BlackBerry

Seems like it is rather complex to make a simple HTTP request on BlackBerry.  The code for opening a connection is straightforward:

String url = "http://10.0.0.1";
HttpConnection conn = conn = (HttpConnection)Connector.open(url);

The complexity comes in because I am developing on a  BlackBerry device without service.  When I try to execute the above code, I get an IOException stating “Tunnel failed”.  From searching, it appears that I get this message because APN is not set.  I can not set it because I do not have service for this phone.  If you don’t have the APN set, it does not automatically try and use the WIFI connection.  Apparently there are six different methods for using a connection.  Each one of these requires a different string appended to the URL to determine which one gets used.

BlackBerry MDS

The first method is using BlackBerry MDS.  This forces all traffic to go through the BlackBerry enterprise server.  It encrypts all traffic using AES or Triple DES.  My task is to specifically avoid using the MDS because we are creating a cloud service where the customer does not need to have any server let alone a BlackBerry enterprise server.

String url = "http://10.0.0.1;deviceside=false";

BlackBerry Internet Service

The second method is to use the BlackBerry Internet Service.  This is only available to approved BlackBerry Alliance Program members.  There are different tiers. The public tier which is free does not offer the BlackBerry Internet service feature.  The minimum tier that supports this is the BlackBerry Alliance Associate Member which costs $2,000/yr and requires 45 member points.  To get this, you basically have to make losts of money using the BlackBerry platform or promot it in other ways.  You can look at their FAQ for more details, but basically we don’t have this option available to me right now.

TCP Stack

The third method is to use the TCP stack directly.  In order to use this, you need to have the APN username and password.

String url = "http://10.0.0.1;deviceside=true";

Wi-Fi

The fourth method is to use wi-fi.  This is what I’m interested in right now.

String url = "http://10.0.0.1;interface=wifi";

WAP 1.x

The fifth method is to use WAP 1.x gateway.  This is supported over the carriers networks.  You have to contact the carriers to get a list of their WAP gateway parameters.

String url = "http://10.0.0.1;WAPGatewayIP=127.0.0.1;WAPGatewayAPN=carrier.com.gprs";

WAP 2.0

The sixth way is to use WAP 2.0.  You need to loop through the records and find the uid of the desired ServiceRecord in the ServiceBook.  This is simpler than WAP 1.x because you don’t need all the gateways and settings from the carriers.  This is supported as of 4.2 on the BlackBerry.

// String uid = get from the device
String url = "http://10.0.0.1;ConnectionUID=" + uid;

Going Forward

I would really like the device to try wi-fi first before attempting to use the potentially expensive data plan.  I am going to have to write some code to test the wi-fi first, and then attempt the data connection if this fails.  It is conceivable that the user wouldn’t want the app to transmit unless the wi-fi connection is available.  Not as simple as just opening up a connection and firing off a GET request.

Blackberry development environment

Blackberry applications are written in Java.  Since the java mantra is write once, test everywhere.  er.  sorry, run everywhere.  You would think that you could choose whatever OS you would like to develop for the Blackberry.  While the documentation claims you can write Blackberry code on any platform, the reality for a new developer is quite different.  The Eclipse plugin they offer only runs on Windows.  Very irritating considering I can run eclipse on my Mac as well as my Windows machine.  Rather than fight it, I am going with the flow and using Windows XP 32bit inside my Parallels virtual machine.

Very important that you pay attention to the version of eclipse that the plugin works with.  I didn’t and installed the lastest eclipse (3.5 as I write this).  I went to “Help > install new software…” using “http://www.blackberry.com/go/eclipseUpdate”.  I got the following error:

Cannot complete the install because one or more required items could not be found.
  Software being installed: BlackBerry JDE Plug-in for Eclipse 1.0.0.67 (net.rim.EclipseJDE.feature.group 1.0.0.67)
  Missing requirement: BlackBerry JDE Plug-in for Eclipse 1.0.0.67 (net.rim.EclipseJDE.feature.group 1.0.0.67)
  requires 'org.eclipse.debug.ui [3.4.0,3.5.0)' but it could not be found

I had to download the gannymede 3.4.2 version of eclipse and install it.  Then I did the software update using the http://www.blackberry.com/go/eclipseUpdate URL.  It prompted me half a dozen times for my Blackberry Developer zone ID.  I dutifully entered it each time (annoying).  After this, I had a working environment.

In the BlackBerry JDE Plug-in for Eclipse Installation and Configuration demo video, They show configuring the BlackBerry workspace.  Well, this option is not enabled until you created a BlackBerry project.  Also in this video, you will see them run the simulator just by clicking run.  This did not work for me.  In order to actually launch the application in the simulator, you need to right click the project and select “Activate for Blackberry”.  If you don’t do this, Eclipse will launch the simulator and you will see debug output, but your project will not show up on the device.

The next step is to get it working on an actual BlackBerry device.  When I first attempted this, it would not work with my old version of parallels.  I updated to the latest build of parallels and then Windows could recognize the USB device.  My mac took control and parallels did not prompt me like it usually does when I connect a USB device.  There is an icon in the lower right of the frame.  Click the USB icon there and select “Research in Motion”

I haven’t been able to get the Eclipse IDE to install and run the application on my device.  I’ve had to use the tool JavaLoader.exe which is located in the bin directory of your JDE installation.

JavaLoader.exe -usb load HelloWorld.jad
JavaLoader.exe -usb erase -f HelloWorld.jad

These commands will install and remove your application.  This assumes that the bin directory is in your path and you are in the location where your *.jad and *.cod file.

Blackberry preferred development platform is Windows

Well, I am in the middle of learning to write my first blackberry application. Since Blackberry uses the Java platform so heavily, I was excited to be able to develop natively on the Mac. Apparently there is no easy way to actually do this. I am used to using Eclipse for my java development so I was excited that Blackberry had an Eclipse plugin. I was very disappointed to learn that this plugin only works on a Windows machine. I believe it is the Simulators that cause the problem. I didn’t want to spend days fighting this issue, so I gave up and installed Eclipse on a Windows 32bit virtual machine. I was quite bummed to have to do this. I don’t understand why they go to so much trouble implementing the JME which is platform neutral, and then totally bomb when it comes to forcing developers to develop on a Windows box. I think it is crucial for them to support more than Windows so they can attract the thousands of iPhone developers that love their Macs (like me). Or maybe they know that if you really want to develop for the blackberry, you will just install Windows. I am looking forward to some Android development…