Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 06:23, 27 May 2008.

CS000977 - Sending a binary SMS

From Forum Nokia Wiki


ID CS000977 Creation date May 27, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95 8GB
Category Java ME Subcategory WMA 2.0 (JSR-205)


Keywords (APIs, classes, methods, functions): javax.wireless.messaging.MessageConnection, javax.wireless.messaging.BinaryMessage, javax.microedition.io.Connector, java.lang.Thread, java.io.IOException, java.io.InterruptedIOException, java.lang.IllegalArgumentException, java.lang.SecurityException, javax.microedition.io.Connector.open(), javax.wireless.messaging.MessageConnection.newMessage(), javax.wireless.messaging.Message.setAddress(), javax.wireless.messaging.BinaryMessage.setPayloadData(), javax.wireless.messaging.MessageConnection.send(), javax.wireless.messaging.MessageConnection.close()

Overview

This code snippet demonstrates how to send a binary SMS. Binary messages are used, for example, to deliver application-specific settings to the user. In practice, in order for binary messages to be usable, their format must be defined - otherwise the device will not understand them.

In the snippet, the user enters a phone number to a text field. He or she may also fetch the number from the address book. Then, by selecting Options > Send, the SMS is sent to that number.

This is a complete example MIDlet, but the most interesting methods (methods regarding SMS handling) are handleSendCommand(), prepareSMS(), and sendSMS().

Source file

import java.io.IOException;
import java.io.InterruptedIOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.MessageConnection;
public class SMSMIDlet extends MIDlet implements CommandListener {
    private Command sendCommand;
    private Command exitCommand;
    private Form mainForm;
    private TextField smsAddress;
    private MessageConnection connection;
    
    /**
     * Constructor. Constructs the object and initializes displayables.
     */
    public SMSMIDlet() {
        mainForm = new Form("SMS Example");
        
        smsAddress = new TextField("Phone number", null, 20,
            TextField.PHONENUMBER);
        mainForm.append(smsAddress);
        
        sendCommand = new Command("Send", Command.ITEM, 0);
        mainForm.addCommand(sendCommand);
 
        exitCommand = new Command("Exit", Command.EXIT, 0);
        mainForm.addCommand(exitCommand);
        
        mainForm.setCommandListener(this);
    }
    
    /**
     * From MIDlet.
     * Called when the MIDlet is started.
     */
    public void startApp() {
        // The initial display is the main form
        Display.getDisplay(this).setCurrent(mainForm);
    }
 
    /**
     * From MIDlet.
     * Called to signal the MIDlet to enter the Paused state.
     */
    public void pauseApp() {
        // No implementation required
    }
 
    /**
     * From MIDlet.
     * Called to signal the MIDlet to terminate.
     * @param unconditional whether the MIDlet has to be unconditionally
     * terminated
     */
    public void destroyApp(boolean unconditional) {
        if (connection != null) {
            try {
                // Close the message connection
                connection.close();
            } catch (IOException ex) {
                // TODO: Exception handling
            }
        }
    }
 
    /**
     * From CommandListener.
     * Called by the system to indicate that a command has been invoked on a
     * particular displayable.
     * @param command the command that was invoked
     * @param displayable the displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == exitCommand) {
            // Exit the MIDlet
            destroyApp(true);
            notifyDestroyed();
        } else if (command == sendCommand) {
            handleSendCommand();
        }
    }
 
    private void handleSendCommand() {
        try {
            // Open the connection
            connection = (MessageConnection)Connector.open("sms://:5000");
        } catch (IOException ex) {
            // TODO: Exception handling
        }
            
        BinaryMessage message = prepareSMS();
        sendSMS(message);
    }
 
    /**
     * Sets the address and payload data for the binary SMS.
     */
    private BinaryMessage prepareSMS() {
        // Prepare the binary message
        BinaryMessage message = (BinaryMessage)connection.newMessage(
            MessageConnection.BINARY_MESSAGE);
        
        // Set the destination address
        String address = "sms://" + smsAddress.getString();
        message.setAddress(address);
        
        // Set the data
        byte[] data = obtainByteData();
        message.setPayloadData(data);
        
        return message;
    }
 
    /**
     * Constructs a byte array and fills it with data that is to be sent as an
     * SMS message.
     */
    private byte[] obtainByteData() {
        // Implementation not provided
        // ...
    }
 
    /**
     * Sends a binary SMS.
     */
    private void sendSMS(final BinaryMessage message) {
        // Send the message on its own thread of execution
        Thread smsThread = new Thread() {
            public void run() {
                try {
                    connection.send(message);
                    mainForm.append("Message sent.");
                } catch (InterruptedIOException ex) {
                    // TODO: Exception (e.g. timeout) handling
                } catch (IOException ex) {
                    // TODO: Exception (e.g. network failure) handling
                } catch (IllegalArgumentException ex) {
                    // TODO: Exception (e.g. too big or otherwise invalid
                    // message) handling
                } catch (SecurityException ex) {
                    // TODO: Exception (e.g. insufficient permissions) handling
                }
            }
        };
        smsThread.start();
    }
}

Postconditions

The MIDlet sends a binary SMS to the number specified by the user.

See also

Related Discussions
Thread Thread Starter Forum Replies Last Post
Ctrl+Z, at+cmgs kfchong1 Nokia M2M 3 2003-08-22 05:45
Calling Native SMS API from JAva App paulbutler Mobile Java General 12 2003-10-29 12:38
ending SMS with Simplewire SDK to a specific port, not working lekkie Mobile Java Networking & Messaging & Security 1 2007-02-22 17:27
Listening to SMS sent without any port number - in J2ME ullas.kris General Messaging 2 2007-08-14 21:45
Detect new email and send it out as SMS smileyyhl General Symbian C++ 2 2008-06-05 12:45
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX