×

Discussion Board

Results 1 to 3 of 3

Thread: sms prob

  1. #1
    Regular Contributor
    Join Date
    Sep 2008
    Posts
    158

    sms prob

    hi everyone i have both the sending n receiving sms midlets in my nokia 3230 mob..when listener is not started in the receiving midlet.i can send msgs to my mob..but wen is start listening for receiving msg..i not able to send the msg..
    i enclose both the codes here..

  2. #2
    Regular Contributor
    Join Date
    Sep 2008
    Posts
    158

    Re: sms prob

    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.MessageConnection;
    import javax.wireless.messaging.TextMessage;
    public class SMSMIDlet extends MIDlet implements CommandListener {
    private Command sendCommand;
    private Command exitCommand;
    private Form mainForm;
    private TextField smsNumber;
    private TextField smsText;
    private MessageConnection connection;

    /**
    * Constructor. Constructs the object and initializes displayables.
    */
    public SMSMIDlet() {
    mainForm = new Form("SMS Example");

    smsNumber = new TextField("Phone number", null, 20,
    TextField.PHONENUMBER);
    mainForm.append(smsNumber);

    smsText = new TextField("Text", null, 160, TextField.PLAIN);
    mainForm.append(smsText);

    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
    }

    TextMessage message = prepareSMS();
    sendSMS(message);
    }

    /**
    * Sets the destination address and payload text for the text SMS.
    */
    private TextMessage prepareSMS() {
    // Prepare the text message
    TextMessage message = (TextMessage)connection.newMessage(
    MessageConnection.TEXT_MESSAGE);

    // Set the destination address
    String number = "sms://" + smsNumber.getString();
    message.setAddress(number);

    // Obtain the specified text and set it as the payload
    String text = smsText.getString();
    message.setPayloadText(text);

    return message;
    }

    /**
    * Sends a text SMS.
    */
    private void sendSMS(final TextMessage 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();
    }
    }

  3. #3
    Regular Contributor
    Join Date
    Sep 2008
    Posts
    158

    Re: sms prob

    import java.io.IOException;
    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.StringItem;
    import javax.microedition.midlet.MIDlet;
    import javax.wireless.messaging.BinaryMessage;
    import javax.wireless.messaging.Message;
    import javax.wireless.messaging.MessageConnection;
    import javax.wireless.messaging.MessageListener;
    import javax.wireless.messaging.MultipartMessage;
    import javax.wireless.messaging.TextMessage;
    public class SMSListenerMIDlet
    extends MIDlet
    implements CommandListener, MessageListener {

    // The port which is listened for incoming messages
    private final String PORT = "5000";

    private Form mainForm;
    private Command startCommand;
    private Command stopCommand;
    private Command exitCommand;
    private MessageConnection connection;
    private boolean listening;

    /**
    * Constructor. Constructs the object and initializes displayables.
    */
    public SMSListenerMIDlet() {
    mainForm = new Form("SMS Listener");

    startCommand = new Command("Start listening", Command.ITEM, 0);
    mainForm.addCommand(startCommand);

    stopCommand = new Command("Stop listening", Command.ITEM, 1);
    mainForm.addCommand(stopCommand);

    exitCommand = new Command("Exit", Command.EXIT, 1);
    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) {
    // Stop listening
    stopListening();
    }

    /**
    * 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 == startCommand) {
    startListening();
    } else if (command == stopCommand) {
    stopListening();
    }
    }

    /**
    * Starts listening for incoming messages.
    */
    private void startListening() {
    // If we are already listening, no need to start again
    if (listening) {
    return;
    }

    try {
    // Open the connection to the specified port
    connection = (MessageConnection)Connector.open("sms://:" + PORT);

    // Register this MIDlet as the listener that should be notified
    // whenever messages arrive
    connection.setMessageListener(this);
    } catch (IOException ex) {
    return;
    }

    listening = true;
    mainForm.append("Listener started.\n");
    }

    /**
    * Stops listening for incoming messages.
    */
    private void stopListening() {
    // If we are not listening, no need to do anything
    if (!listening) {
    return;
    }

    if (connection != null) {
    try {
    // Deregister the message listener and close the connection
    connection.setMessageListener(null);
    connection.close();
    connection = null;
    } catch (IOException ex) {
    // TODO: Exception handling
    }
    }

    listening = false;
    mainForm.append("Listener stopped.\n");
    }

    /**
    * Asynchronous callback method for receiving incoming messages.
    * Called by the WMA implementation when a new message is ready.
    * @param connection the message connection with incoming messages
    */
    public void notifyIncomingMessage(final MessageConnection conn) {
    // Because this method is called by the platform, it is good practice to
    // minimize the processing done here, on the system thread. Therefore,
    // let's create a new thread for reading the message.
    Thread smsThread = new Thread() {
    public void run() {
    try {
    // Receive all incoming messages to the specified
    // connection. The receive() method will block until there
    // is a message available.
    Message message = conn.receive();
    if (message != null) {
    mainForm.append("Message received.\n");
    processMessage(message);
    }
    } catch (IOException ex) {
    // Stop listening
    stopListening();
    }
    }
    };
    smsThread.start();
    }

    /**
    * Processes the received message according to its type.
    * @param message the received message
    */
    private void processMessage(Message message) {
    if (message instanceof TextMessage) {
    processTextMessage((TextMessage)message);
    } else if (message instanceof BinaryMessage) {
    processBinaryMessage((BinaryMessage)message);
    } else if (message instanceof MultipartMessage) {
    processMultipartMessage((MultipartMessage)message);
    }
    }

    /**
    * Processes a text message.
    */
    private void processTextMessage(TextMessage message) {
    String text = message.getPayloadText();
    StringItem textItem = new StringItem("Text", text);
    mainForm.append(textItem);
    }

    /**
    * Processes a binary message.
    */
    private void processBinaryMessage(BinaryMessage binaryMessage) {
    // Not implemented
    }

    /**
    * Processes a multipart message.
    */
    private void processMultipartMessage(MultipartMessage multipartMessage) {
    // Not implemented
    }
    }

Similar Threads

  1. Send SMS Problem. Cannot Get SMSC Address. Due to Strange SIM Card?
    By cksong in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2007-07-02, 16:13
  2. Replies: 1
    Last Post: 2005-12-01, 23:57
  3. sms prob
    By simonscsi in forum PC Suite API and PC Connectivity SDK
    Replies: 1
    Last Post: 2003-08-28, 13:38
  4. Replies: 2
    Last Post: 2002-10-02, 07:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×