HanG321 Blog

J2ME SMS

http://www-128.ibm.com/developerworks/wireless/library/wi-p2pmsg/

Send SMS messages

Sending 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 1.

Listing 1. Sending an SMS message

We can also send out messages through a server connection, as shown in Listing 2.

Listing 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.

Receive SMS messages

To 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 3 illustrates a server loop that listens, receives, and replies to incoming SMS messages.

Listing 3. Receiving an SMS message and replying to it