Friday, June 23, 2006

My kingdom for an SMS Port

I'm currently awake, putting the finishing touches to the MIDP
application I've been working on since last week. I posted last week
about the rapid speed with which I quickly whipped up a 90% working
demo. At that point, what was left was finally sending collected
information via SMS to a remote application server listening via SMS.

To do this, you'd use the J2ME Wireless Messaging API (WMA). The api
seems rather simple and I looked at the code in the WMA Demo example
that comes with the j2me Wireless Tool Kit. I adapted the main sending
functionality there into a function of mine to send text... and that's
where my problems began (I know that in retrospect).

Apparently, according to the SMS specification, more than one SMS
application can recieve at the same phone number simoultaneously, just
like TCP/IP sockets. To differentiate the different applications, the
sms url addressing scheme looks just like the usual http scheme we're
all used to, complete with the port numbers, so I can send an SMS to a
phone number at sms://+123456:10 . When the SMS gets to the phone, an
application listening at sms port 10 will recieve this. All well and
good it seems, but not so for me... when I suddenly discovered that SMS
allows for port numbers, I started wondering what would happen if I
don't specify the port number? Or which port the default sms recipient
application listens at? Basically, b/cos I knew that in HTTP, not
specifying a port number means you're specifying port 80, I got worried
and tried to find out. I didn't find this out directly, instead what I
found out was that if your app is LISTENING, and it couldn't careless
about the port the message comes in on, it should be set to listed at
port 0.

Armed with this half baked information, I went on to assume that sending
to port 0 then should be okay, if you're not interested in sending to
any particular port! Well, needless to say, my first version didn't
work. I didn't get an exception or anything, the application just
behaved as if it were sending, and quit rapidly, but nothing happened.
On the Sony-Ericsson phone I was initially testing on, the phone kept
asking me if I wanted the application to RECIEVE messages! This was odd,
since i was trying to SEND, anyways, I just thought the phone was being
stupid and that all was ok, and I went on... it didn't work.

I decided to try on a Nokia 6600, but when I tried to move the jar over
via bluetooth, it didn't even install. In my "infinite" wisdom ;), I
believed we were having problems with code signing (hilarious huh?).
Anyways, I convinced my boss to start the process of obtaining a
verisign certificate last week, and I went on to relax and wait.

By Monday this week, I realized the certificate would probably not be
getting to us b4 Wednesday, when I was due for a second client demo. On
tuesday then, I decided to go stay over with some of my friends (Bugzy,
Nnamo, ROM and co :P) who also write j2me apps on and off. That nite,
Bugzy and Myself first tried to deploy his j2me apps on another 6600 and
they deployed... that was when I realized the Nokia issue was due to the
MIDP version. I had been building for MIDP2.0 all the while, so I
changed my setting to build for MIDP 1.0, CLDC 1.0 with WMA 1.0, and
rebuilt the app, fixing what needed fixing, then I deployed to the Nokia
properly... and boom!!! It worked!!! The sms left the Nokia phone... we
were ecstatic.

We then deployed to a sony-ericsson and met disappointment... it didn't
send. After some tinkering, we changed the port number from 0 to 1, just
for the sake of trying something different... and w00t!!! It worked!!!
PHEW! I tidied up the code for the demo and went for the demo yesterday.
It went well BUT there was a problem, the SMS message arrived at the
application with some 8 extra bytes infront of the commands we were
sending! Incidentally, sending the message to another phone, there were
no extraneous bytes... so what could be wrong :(.

I was just frustrated and annoyed, but hey... I had to figure it out. I
started scouring materials, and probably read over 10 WMA tutorials
today, that all said the same thing, and who's example code looked
exactly like my current code, though they were all using port 5000. I
decided to use port 5000, but nada... no show. Very depressing. Finally,
my collegue at the office, Lanre suggested we try to setup NowSMS on his
box, since he has a trial version and see if that could help. Grudgingly
I agreed.

10 minutes latter, we have NowSMS listening for messages, and I send a
command from the application, and check the incoming messages to NowSMS

That is sooooo suprisingly amazing... but finally I at least know why
the bytes. I try to read thru the specs to see where I may have made a
mistake, and finally, I come to a conclusion that J2ME WMA
implementation has a bug, since I had specified
MessageConnection.TEXT_MESSAGE!!! Finally, I do what I do sometimes when
all logical explanations have flown out the window... I deleted the sms
sending function I had, and proceeded to write another one. I just
cleared the page, and without thinking of my former implementation I
wrote a smaller one, and just tested again... and BOOM!!! IT WORKS!!! I
see the messages into NowSMS come in as TEXT and not as BINARY!!!
Amazing... I look thru my former code and the new one... and the
difference? Nothing... hey.. then why is this worki... uhh... wait... in
my new code, I FORGOT to add port numbers!!!!! You mean...? Yup. The
problem was the port numbers I was adding. I SHOULD NOT have added them
AT ALL. Infact, if I hadn't added them at all, the very first time I
tested my app on the sonyericsson, it would have worked!!!!!

Well, needless to say, this was an anti-climax. I was just too relieved
and rushed to do the demo, and everything works well finally. I'm
currently awake putting finishing touches to the look, input
verification, etc.

The moral lesson of this long twisted story? Uhhh... I'm not too sure
what that should be, but I guess "when all fails, do something
illogical." is not a bad moral lesson :)

Seriously though, I still don't know why I hadn't tried without port
numbers loooooooooooooong ago. Now at least, I know better, and I'm
putting this here incase someone else has the same problem.

Anyways... njoi


At Sunday, June 25, 2006 6:21:00 PM, Anonymous Anonymous said...

Check out . Nigeria's own myspace.
We can create a blog profile that automatically aggregates your RSS feed of your blog on your profile and the blog posts link back to your blog to drive traffic to your blog.
you can email us @ to set up your profile or sign up at the site and send a message to ADMIN.
If you like the site we would appreciate a blog post. Thanks in advance.

At Wednesday, September 27, 2006 11:02:00 PM, Anonymous Alan Stewart said...

Thank you for posting this info. I've just run into the same darned thing. What's up with this stuff? SMS has been around for *YEARS* but they still can't get it right? Sheesh!

At Friday, June 08, 2007 11:35:00 PM, Blogger Mirko said...

hi Essien,
at the end of the story...
Not specify the port means port=1 or port =0
I have problems like yours:
I have GSM controlled by PIC and I dod not found the way to specify the application port for SMS.
could you kondly replay to:

At Friday, May 16, 2008 11:06:00 AM, Anonymous Anonymous said...

wow, that was great post.. thanx for sharing the same...
Mobile Social networking

At Saturday, November 29, 2008 12:00:00 AM, Anonymous Griffin said...

Thanks for sharing your experience. It was really helpful to me.



Post a Comment

<< Home