One of the most common features in mobile phones is messaging. Text messages or short messages (SMS), as they are sometimes called, have been getting more and more popular since the technology was first introduced. And of course, multimedia messaging (MMS) has added the flavors of images, sound, and video to the soup.
When it first surfaced, some experts predicted that MMS would displace SMS, which probably could have happened if the price tag hadn't been so high. Even so, the ability to transfer multimedia content between mobile phones is gaining in popularity. The recent success of mobile e-mail suggests that in the future the main form of mobile messaging could be e-mail; but that's still a long way ahead.
This month I'll talk about Java Micro Edition (Java ME) Wireless Messaging API. I'll get you started with a couple of simple coding examples, then discuss the opportunities it opens up for enterprising developers.
The Wireless Messaging API (WMA) 2.0, defined in JSR-205, is a set of optional APIs for wireless messaging. WMA connectivity is based on the Generic Connection Framework, which is the basis of all communication in CLDC-based Java ME architectures. WMA 2.0 is backward compatible with WMA 1.1, and WMA 2.0 adds support for sending and receiving multimedia messages. In other words, the Wireless Messaging API provides a high-level abstraction of wireless communication. It hides the transport layer completely, so all you have to do is create messages, send them, and receive them.
The WMA enables text messaging, cell broadcasting, and multimedia messaging. Text messaging is fairly simple and most people these days know what it is. SMS messaging is incredibly popular and is, at the same time, simply part of everyday life. WMA enables both sending and receiving text messages. Sending is quite trivial: you only need to define the destination address and the text, which is called a payload. Receiving is a bit more complicated, because it requires that the application be able to listen for incoming messages. You'll see in the upcoming examples how to read incoming messages.
Binary messages are messages with binary content. The content could be basically anything that you want to transfer to your application or from your application to other clients or to a server. Multipart messages contain several pieces of media, such as text, audio, and video.
Cell broadcasting is probably the most unknown of the message types. The Cell Broadcast Service is a data service where messages are broadcast by a base station and received by every mobile station listening to that base station. The service is unidirectional, which means that WMA can only be used for receiving such messages.
Sending a text message is quite simple. The steps are:
- Set the mobile phone number (the address).
- Create a
MessageConnectionwith theConnectorinterface. - Create a new message and typecast it to a
TextMessage. - Set the payload.
- Send the message by calling
send()and passing the instance ofMessageto the method.
The sent message will contain sender information identical to what you would expect when sending a message normally. Listing 1 shows the code to send a message using the Java ME WMA.
Listing 1. Sending a message
try {
String addr = ѳms://+358401234567ѻ
MessageConnection conn = (MessageConnection) Connector.open(addr);
TextMessage msg =
(TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(єhis is a test message!ѩ;
conn.send(msg);
} catch (Exception e) {
...
}
|
Receiving a text message is a bit more complicated, but not overwhelming. You open the MessageConnection to a port (in this case 5432) and read the incoming messages from the MessageConnection. The type of the message can be tested using instanceof. The application must implement the MessageListener interface to receive notification of an incoming message, so you need more code than what I show in Listing 2 to make it actually work.
Listing 2. Receiving a message
try {
String addr = ѳms://:5432ѻ
MessageConnection conn = (MessageConnection) Connector.open(addr);
Message msg = null;
while (someExitCondition) {
msg = conn.receive();
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage)msg;
String receivedText = tmsg.getPayloadText();
// respond with the same text with єhanks.Ѡ
tmsg.setPayloadText(єhanksѩ;
conn.send(tmsg);
} else {
// Received message was not a text message, but e.g. binary
...
}
} // end while
} catch (Exception e) {
...
}
|
Sending a binary message is quite similar to sending a text message. First you need to have the data as a byte array. You then create a MessageConnection as shown in Listing 3, and create a BINARY_MESSAGE.
Listing 3. Sending a binary message
try {
String str = "Hello!";
byte[] msg = str.getBytes();
String addr = "sms://+358401234567";
MessageConnection conn = (MessageConnection) Connector.open(addr);
BinaryMessage bm = (BinaryMessage)mc.newMessage
(MessageConnection.BINARY_MESSAGE);
if (url!= null)
bm.setAddress(url);
bm.setPayloadData(msg);
conn.send(bm);
}
catch(Exception e) {
}
|
If you want to learn more about coding with the WMA, see Resources at the end of this article. For now, I'll focus on the potential applications of the WMA.
So what can you do with messaging? Sending text messages from a Java ME application is cool -- you can customize the user interface and the usability as you want -- but that's hardly a killer app. Nokia and Sony-Ericsson have already invested quite a lot of time and money to make that sort of messaging as easy and convenient as possible.
The real advantage comes with business applications and predefined text messages. For example, in business applications you can use text messaging as your transfer protocol if GPRS, WLAN, or other IP-based protocols are not available or are unreliable; if you have legacy interfaces on SMS; or if you're commanding a device with an SMS interface. For one scenario, you could have the server (for example a CRM) send a text message with the latest sales data, then your mobile application could confirm that the message came from your service and represent the data as a graph, or any other way you wish. (With larger amounts of data it would of course be better to use HTTP or datagrams.)
You could also use both text and binary messages in games programming. For example, one player could send game-specific items, like earned levels, tools, or points, to a friend who plays the same mobile game. The friend's application would receive the message and open the sent items. Of course, binary messages can be used with business applications as well.
Multimedia messages have not yet gained much popularity in applications or services, but they do offer some interesting opportunities in games. For example, you could send a snapshot of your game screen to your friend and your friend could receive the snapshots as multimedia messages, unattached to any specific application. Sending weather reports as images is another handy service that uses the convenience of MMS.
The types of messaging handled by the WMA can be categorized as server-to-mobile, mobile-to-server, and mobile-to-mobile.
In server-to-mobile messaging, a CRM application could send data to a mobile application that the application could then receive and represent in a user friendly way. In mobile-to-server messaging, the mobile application could send data to the server. (An HTTP connection would also work for this, but it isn't always possible.) In mobile-to-mobile messaging, the user could send application or game data to a friend's phone, enabling game item changes, and so on.
In this month's column I've introduced wireless messaging with the Java ME WMA. I've offered some basic sending and receiving examples and discussed some of the ways you could leverage wireless messaging. The WMA enables sending and receiving text messages, binary messages, and multimedia messages, and receiving cell broadcast messages. All of these message types offer great opportunities for both game developers and business application developers.
Messaging (e-mail, IM) has proved to be an important part of the daily use of the internet. Text messaging has also become a vital part of the mobile phone user experience. The straightforwardness of the WMA makes it a lucky addition to a wireless application developer's toolkit.
Finally, as all things have a certain lifespan, so has the Architectural manifesto column come to its end. It has been truly interesting to write about mobile development and other issues related to software architectures. I've greatly enjoyed the feedback from my readers and the resulting e-mail conversations. Thank you, everyone!
Learn
- "The Wireless Messaging API" (Sun Developer Network, December 2002): Learn more about JSR 120.
- "Mobile P2P messaging, Part 1" (developerWorks, December 2002): This article introduces point-to-point messaging with WMA.
- "Extend J2ME to wireless messaging" (developerWorks, February 2003): This article provides a particularly a good introduction to cell broadcasting.
- Architectural manifesto: Read every article in the series.
- developerWorks Web development zone: Specializing in Web technologies.
Discuss
- developerWorks blogs: Get involved in the developerWorks community!
Mikko Kontio works as a Head of Production for the leading-edge Finnish software company, Softera. He holds a Masters degree in computer science and is the author and co-author of several books, the latest being Professional Mobile Java with J2ME, published by IT Press. Mikko can be reached at mikko.kontio@softera.fi.
Comments (Undergoing maintenance)





