Repair Your Own BlackBerry

It seems to be a bit of a theme at the moment, and this one is hardware related.

The scrolly-wheely-thingy on my Curve 8900 was giving me hassles. Remember the days of the scrolly-wheell mouse? And how it all got tangled with grit and dust and hair… ew! Well, the same thing happens with the BlackBerry after miles of tracking that ball. So what’s a man to do when you can’t scroll down to read that email, tweet or web page? Why, take it apart and clean it, of course!

I double-checked for a step-by-step guide as to the disassembly of my phone here. Turns out, the instructions are very accurate 🙂

The result?

BlackBerry Curve 8900 Disassembled

If you going to do this yourself a couple tips:
* use the right tools (TX 6)
* everything you detach, put on sticky (or non-slip) surface
* keep all the tiny bits- they really are tiny
* try finish with no left-over bits
* try finish with no missing bits
* be patient

So if next you have a hardware issue with your phone, follow the tradition of fashion apparel and handymen around the world and Just Do It… yourself.

NOTE: you may void the warranty on your phone if you try this. Which is not such an issue unless you end up with an epic fail.


Not All Divisions Are Equal

If you don’t need to pay attention to a particular detail every day, it can sneakily bite you in the derriere. Division is one of N culprits floating around the programmer’s haven. And this little guy thrives on C# .NET code.

Let’s take something simple like

int a=9, b=0;
return (a/b);

You’re expecting something similar to a “can’t divide by zero exception”, right? And indeed you do get one. In fact, all good programming courses will teach you the following idiom:

int a=9, b=0;
return (0==b)?0:(a/b);

Which is to say; only divide by ‘b’ if i’t not zero otherwise return 0 (or any other logical value).

Back to the code; what happens if I change the type from int to a floating point (float or double)? Afterall, no self-respecting programmer would do any financial calculations using integers, right?

double a=9, b=0;
return (a/b);

Divide by zero exception?



This is not a divide by zero exception condition. In fact, dividing by 0.00000 (or even -0f, yes, that’s a minus floating point zero) is perfectly acceptable and does give you a result.


Yes. As I mentioned above, this is one of those “stickies” that you don’t often pay attention to, but definitely mess with your fundamental assumptions about life and the universe when you not getting the results you expect.

For a little more history on the problem, you can always refer to WikiPedia.

Is all hope lost? No. Just be careful out there because programming _is_ hard (for this and countless other reasons).


Repair Your X10 Android

Yes, I broke mine. Good and proper. Hey, I’m a mobile dev. If I don’t get to break a phone once in a while, I’m not doing my job properly 😉 Problem is, the X10 I have (had?) has only a virtual keyboard :/

So the first thing you need to do is install the PC Suite from Sony Ericsson. And then it’s straightforward: Tools > Phone repair. Simple.

The app will prompt further instructions; follow those (yes, you actually have to read the screen). And remember to press the BACK key while connecting the USB cable to the phone.

Of course, that doesn’t always work or solve ALL your problems. Sometimes you just brick it real good- like mine.


Manage Cookies with BlackBerry Java

When interacting with [generally and widely interpreted] web services, you may be required to communicate state (usually a login authentication token). This [usually] takes the form of a cookie and in our interconnected wide world of interwebs, browsers handle cookies just fine; custom-built clients not always. On the BlackBerry, if you’re putting together a Java app, and using the HttpConnection class to communicate with said server, you need to manage that cookie (and hence state) by yourself. Fortunately, it’s not rocket science.

The basic workflow is: you connect to the endpoint and POST some data. On the response, you need to read the headers and look for the “Set-Cookie” field. From that value, you can extra the data you need. On all subsequent requests, you set the “Cookie” field on the header. Simple. Time for some code.

NOTE: Exception handling omitted for brevity and readability.

The first request is just a regular POST in this instance. The buffer variable is a byte[].

HttpConnectionFactory factory = new HttpConnectionFactory(endpoint);
HttpConnection connection = factory.getNextConnection();
connection.setRequestProperty("User-Agent", "BlackBerry Client");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream output = connection.openOutputStream();

From that request, we wait for the response and read that. In this example, I am looking for the PHPSESSID field that was set as part of the response in the header.

String cookie = connection.getHeaderField("Set-Cookie");
if(null != cookie) {
  int php = cookie.indexOf("PHPSESSID=");
    if(0 <= php) {
      php += "PHPSESSID=".length();
      int term = cookie.indexOf(";", php);
      if(0 <= term) {
        _session = cookie.substring(php, term);

Now I have the token required (cookie) that I can now send on every subsequent request. The code for that is for all intensive purpose, the same as the first code snippet with one additional line:

connection.setRequestProperty("Cookie", "PHPSESSID=" + _session + ";");

The order of the setting of the request properties won’t make a difference, so just use that anywhere before you open the output stream for writing.


Radio Streaming

So of late, I’ve had the opportunity to work with the 2oceansvibe crew and one of the best advantages of the gig has been listening to 2oceansvibe Radio while coding. Major Plus!

So as it is, there’s practically no excuse for not getting in on the coolest radio station this side of the sun.
They have an iPhone app, a BlackBerry app, a web app and now a Facebook app in addition to being able to listen/stream from just about any media player (Real, Winamp, iTunes).

So, for kicks, here’s an audio streamer for the Mac, recompiled to default to 2oceansvibe Radio. Download StreamingAudioPlayer. Yes, you can listen through iTunes. And indeed, 90% of the times I would just use what’s already there (i.e. iTunes) but like most projects, it’s sometimes just cool to be able to roll your own, plus it’s a really decent sample codebase for an in-the-wild cocoa app.

All code on this project is originally developed by mattgallager and code can be found here on the project site.