9.2 WMA in ActionThe WMATester sample application, available from this book's Web site (see "Resources"), demonstrates the basic use of the WMA and the Sun Microsystems Reference Implementation (RI). You can use the provided ANT script to build and run the project. Commands ant run1 and ant run2 bring up two emulator windows that can send emulated SMS messages to each other (see Section 9.3.1). Peer 1 listens at SMS port 3333 and peer 2 listens at port 3334. So, if you want to send a message from peer 1 to peer 2, you can use an address like 18005555555:3334. Upon receipt of the message, peer 2 will display it. If the message comes from a server connection (using the Send2 button), peer 2 will send an acknowledgment back to peer 1. The process is illustrated in Figure 9.2. Figure 9.2. WMATester work flow.![]() There are two MIDlets in the WMATester suite. They have identical functionalities from the user point of view. The difference is that WMAsync uses a synchronous messaging model and WMAasync uses an asynchronous model. In the next two sections, I walk you through basic features of WMA and its RI using code snippets from the WMATester. 9.2.1 Send SMS MessagesSending messages with the WMA is very simple. You can send a message to an arbitrary phone number and/or an SMS port through a MessageConnection constructed for that destination, as shown in Listing 9.1. Listing 9.1. Sending an SMS message
You can also send out messages through a server connection, as shown in Listing 9.2. Listing 9.2. Sending an SMS message via a server connection
If you choose to use a server connection to send your message, you gain a number of benefits:The connection can be reused again and again.The message contains the sender's port number and phone number. Hence, it gives the recipient peer the opportunity to respond. 9.2.2 Synchronously Receive SMS MessagesTo receive SMS messages in a Java application, we need to have a server MessageConnection listening at the message's target port. The MessageConnection.receive() method blocks until a message is received or the connection is closed. We can loop the receive() method to automatically handle incoming messages when they arrive. Listing 9.3 illustrates a server loop that listens, receives, and replies to incoming SMS messages. Listing 9.3. Receive and reply to all incoming SMS messages
NoteIn the real world, the server loop in Listing 9.3 must run in a separate background thread to avoid blocking the user interface. To see how that works, please refer to our sample application, WMATester.9.2.3 Asynchronously Receive SMS MessagesThe polling loop using the receive() method is often tedious to program with. WMA provides an event-based model for incoming message processing. The following code snippet (Listing 9.4) is adopted from the WMATester example to illustrate the asynchronous messaging technique. Listing 9.4. Asynchronously receive messages
Please note how to put message handling in a separate thread so that we can quickly return from the notifyIncomingMessage() method, as required by the specification. The handler thread is similar to Listing 9.4 except that we do not need the loop anymore. 9.2.4 Receive SMS Message via MIDP PUSHThrough the MIDP v2.0 PUSH architecture, we can push the concept of asynchronous messaging one step further. The MIDP 2.0 AMS can listen for incoming connections. When a registered connection comes in, the AMS invokes a corresponding MIDlet to handle it. This way, the user does not even need to manually start up the server MIDlet. When the AMS picks up an SMS incoming message, it first buffers the message and then triggers the handling MIDlet. The MIDlet can read and delete the message. For more information on the PUSH architecture, please refer to the MIDP 2.0 specification and WMA documentation (see "Resources"). |