Wireless messaging with the Java ME

Source: Internet
Author: User

December 31, 2006

Learn the Java™micro Edition (Java ME) wireless messaging API. This column will start with a few simple code examples and then discuss the various opportunities that it brings to enterprise developers.
One of the most common features of mobile phones is message delivery. Text messages or short messages (SMS) have become more and more popular since the beginning of the introduction. Multimedia Messaging (MMS), of course, adds multimedia content such as pictures, sounds, and video.

When MMS first appeared, some experts predicted that it would replace SMS, a prediction that could indeed become a reality if its price were not so high. Even so, the ability to transfer multimedia content between handsets has become increasingly popular. The recent success of mobile email means that the main form of future mobile messaging may be email, but there is a long way to go.

This month, I'll discuss the Java me wireless messaging API. I'll start with a few simple coding examples, and then discuss the various opportunities that it brings to enterprise developers.

Wireless Messaging API

The Wireless Message API (WMA) 2.0 defined in JSR-205 is a set of optional APIs for wireless messages. The WMA connection is based on the common connection framework, which is the basis for communication in the CLDC Java ME architecture. WMA 2.0 is backward compatible with WMA 1.1, adding support for sending and receiving multimedia messages. In other words, the wireless messaging API provides a high-level abstraction of wireless communications. It hides the transport layer completely, so all you have to do is create messages, send messages, and receive messages.

WMA supports text messages, community broadcasts, and multimedia messages. The text message is simple, and most people know what it is. SMS messages are extremely popular and have become part of everyday life. WMA supports sending and receiving text messages. Sending is very simple: just define the destination address and text, the text is called Net load (payload). Reception is slightly more complicated because it requires the application to be able to listen for incoming messages. In the following example, you will see how to read the incoming message.

Message content

Binary messages, as the name suggests, are messages with binary content. Content can be anything that you want to pass to an application or pass from an application to another client or server. Multi-part messages can contain multiple pieces of media, such as text, audio, and video.

Community broadcasts are probably the least known types of messages. The Community broadcast service is a data service in which messages are broadcast by the base station and received by each mobile station listening to the base station. This service is one-way, which means that WMA can only be used to receive such messages.

Send and receive

Sending a text message is simple. The steps are as follows:

Set up your mobile number (address).
Create the messageconnection with the Connector interface.
Creates a new message and converts its type to textmessage.
Set the net load.
Sends a message by calling the Send () method and passing the messages instance to the method.
The sent message contains the same sender information as when the message was normally sent. Listing 1 shows the code to send a message using Java ME WMA.

Listing 1. Send a message
try {
String addr =? ms://+358401234567?
Messageconnection conn = (messageconnection) connector.open (addr);
TextMessage msg =
(textmessage) Conn.newmessage (messageconnection.text_message);
Msg.setpayloadtext (? He is a test message!?;
Conn.send (msg);
catch (Exception e) {
...
}


Receiving text messages is slightly more complex, but not too complex. Open messageconnection on a port (5432 in this example) and read the incoming message from the messageconnection. You can use instanceof to test the type of message. To receive notification of a message entry, the application must implement the MessageListener interface, so write code that is more complex than listing 2 to make it work.

Listing 2. Receive 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 is not a-text message, but e.g. binary
...
}
}//End While
catch (Exception e) {
...
}

Sending a binary message is similar to sending a text message. First you need to put the data in a byte array, then create the messageconnection (shown in Listing 3), and create the binary_message.

Listing 3. Send 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 writing code using WMA, see Resources at the end of this article. Now, I'm focusing on the potential applications of WMA.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.