Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Shout from the Hilltops with the J2ME Wireless Messaging API : Page 2

Text messaging rapidly surfaced as the killer app of the wireless space. Find out how to integrate text messaging into your J2ME applications using the Wireless Messaging APIs.


advertisement
A Look at the API
Support for WMA is found in the javax.microedition.messaging package, which includes the interfaces shown in Table 2. The interface hierarchy is shown in Figure 3.

Table 2. Interfaces included in javax.microedition.messaging.

Message The basic interface for a message. Commonly needed methods are defined such as the ability to get or set the address associated with the message and to retrieve the timestamp of when the message was sent.
BinaryMessage Extends the Message interface, allowing an application to extract the binary payload of the message.
TextMessage Extends the Message interface, allowing an application to extract the ASCII payload of the message.
MessageConnection Supports the functionary for sending and receiving messages based on the underlying protocol.
MessageListener Allows classes that implement this interface to register with an instance of MessageConnection to be notified when a new message is received.

Figure 3. Selecting Format: TextMessage and BinaryMessage extend the Message interface to provide support for ASCII and binary formatted message payloads.
Show Me the Code
Receiving text messages is pretty much the same, regardless of the protocol being used. Within the WMA, the only difference is that an SMS message listens on a port number where a CBS message listens on a channel ID. However, the way connections are created using the generic connection framework makes both connection protocols look nearly identical. The following illustrates how to create connections for each.

MessageConnection smsListener = (MessageConnection) Connector.open("sms://:4444"); MessageConnection cbsListener = (MessageConnection) Connector.open("cbs://:3333");

In this example, the CBS connection is created with a channel ID of 3333.



Once an instance of MessageConnection is obtained, all an application needs to do to receive its message is to call the receive() method and wait for another device to send a message. When a message is received, the payload can be extracted and handled as desired. The following example displays the received message to the user in a list.

Figure 4. The WMA Console: The WMA Console selects the default phone number of 5550000 on its first instance.

Message msg = listener.receive(); if (msg instanceof TextMessage){ String data = ((TextMessage)msg).getPayloadText(); list.append(data, null); } else { byte[] data = ((BinaryMessage)msg).getPayloadData(); //for simplicity, assume ascii sent in binary format StringBuffer sb = new StringBuffer(); for (int ccnt = 0; ccnt < data.length; ccnt++) { sb.append((char)data[ccnt]); } list.append(sb.toString(), null); }

Testing WMA using the Wireless Toolkit
The Wireless Toolkit provides a WMA Console utility for testing WMA-enabled applications. This utility is opened from the KToolbar by selected "File | Utilities" and clicking the "WMA Console" button. The WMA Console is an application that can be used to test sending and receiving SMS and CBS messages.

Figure 5. Sent and Received: A test MIDlet running in the phone emulator receives the SMS message from the Wireless Toolkit WMA Console.
By default, the address (i.e., phone number) for the WMA console is 5550000. Note, however, multiple instances of the console can be opened. Each time the console is opened, the address is incremented by 1. Figure 4 shows the WMA Console when it is first opened.

The code from the message-receiving example can be placed into a MIDlet and tested using the WMA Console. Figure 5 shows the WMA Console sending an SMS message to the MIDlet.

Note that the addresses for the phone emulator and the WMA console are one digit apart. This is due to the Wireless Toolkit automatically assigning addresses to these tools when they are invoked. The address obtained depends on the order the tool is opened.

Sending Messages
Sending messages using WMA applies only to SMS, because the CBS protocol is used by devices only for receiving messages. The following code illustrates sending an SMS message.

Figure 6. Back At Ya: The MIDlet running in the phone emulator sends an SMS message to the WMA Console.

String addr = "sms://5550000:50000"; MessageConnection sender = (MessageConnection)Connector.open(addr); try { (TextMessage)sender.newMessage (MessageConnection.TEXT_MESSAGE); msg.setPayloadText(messageToSend); sender.send(msg); } finally { sender.close(); }

This example first opens a MessageConnection to address 5550000 on port 50000. The port number identifies the port that the target device listens to for the message. Once a connection instance is obtained, a message container can be created by making a request to the newMessage() method of the MessageConnection. Finally, the payload (the content of the message) is set and send() is called to transmit the message. Figure 6 shows a MIDlet sending an SMS message to the WMA Console.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date