Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By bharani

Thread: J2ME sms listener

  1. #1
    bharani is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Thumbs down J2ME sms listener

    Hi
    I am doing J2ME application to send and receive sms, but there is a problem while receiving the sms....the message listener doesn't listen to the incoming sms...incoming sms directly goes to the mobile inbox instead of coming to applications, I have set my port number as 5000.....Please give any solutions to receive the sms without port number[From any sms should come to the application]....!

    Regards
    ~Bharani~
    Suranga likes this.

  2. #2
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Hi Bharani,

    As you are developing application for sending/receiving sms, then you have to know how it works. J2ME application will listen only those incoming message that comes to specific port (that you defined as 5000). The messages that comes in your OEM inbox folder have default '0' port, you can't take this, it is define only for your native OEM inbox folder. Also you can't read OEM inbox folder messages. So if you want to receive message through application then you have to send message with specific port number like (phone no/port no) 9916292826/5000.

    try this and feel free if you have any doubt.

    Thanks
    Abhijeet Namdeo

  3. #3
    ashah is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    hi
    I am new to j2me. I tried the following example.

    For sending:-
    public class SMSClient2t extends MIDlet
    {
    protected Display display;
    protected Displayable displayable;

    public SMSClient2t()
    {
    display = Display.getDisplay(this);
    displayable = new Form("SMS Client");
    }


    public void startApp() {display.setCurrent(displayable);

    try {
    String myAddress = "sms://:4";
    String hisAddress = "sms://9426729397:4";
    String msg = "Hello";
    MessageConnection smsconn =
    (MessageConnection) Connector.open(myAddress);
    TextMessage txtmessage =
    (TextMessage)smsconn.newMessage(
    MessageConnection.TEXT_MESSAGE);
    txtmessage.setPayloadText(msg);
    txtmessage.setAddress(hisAddress);
    smsconn.send(txtmessage);

    // get a reply back. Note, there could be a race
    // condition here: while this server is waiting for
    // a reply, someone else could send us a message
    // which might slip in first... So we should really
    // check it is from who we expect

    Message replyMsg = smsconn.receive();
    if (replyMsg instanceof TextMessage) {
    TextMessage tmsg = (TextMessage) replyMsg;
    String replyText = tmsg.getPayloadText();
    System.out.println("Reply: " + replyText);
    }
    } catch (Exception e) {
    System.out.println(e.toString());
    e.printStackTrace();
    }

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    display = null;
    displayable = null;

    }
    }


    for receiveing:-
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import javax.wireless.messaging.*;



    /**
    * @author vvachhani
    */
    public class SMSServer2 extends MIDlet {
    protected Display display;
    protected Displayable displayable;
    protected Form form;

    public SMSServer2()
    {
    display = Display.getDisplay(this);
    displayable = form = new Form("SMS Server");
    }


    public void startApp()
    {
    display.setCurrent(displayable);
    try {
    String addr = "sms://:4";
    MessageConnection conn = (MessageConnection) Connector.open(addr);
    Message msg = null;
    while (true) {
    // wait for incoming messages
    msg = conn.receive(); // received a message
    if (msg instanceof TextMessage) {
    TextMessage tmsg = (TextMessage) msg;
    String receivedText = tmsg.getPayloadText();
    form.append(new TextField("Message received", receivedText,
    10, TextField.ANY));

    // now send the message back
    String replyText = "You sent: " + receivedText;
    tmsg.setPayloadText(replyText);
    conn.send(tmsg);
    } else {
    // Received message was not a text message, but e.g. binary ...
    }
    }
    } catch (Exception e) {
    System.out.println(e.toString());
    }
    }




    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional)
    {
    display = null;
    displayable = null;

    }
    }

    I can send the sms with port no but not able to recive it on same port no.
    I use nokia 6233 mobile to run this application.
    Please tel me how can get i get the port no of mobile?

    Thanks
    Alpesh shah

  4. #4
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    HI,

    The code you are using for receving incoming message with specific port is not correct. You have to implement message listner for that.

    Try below mentioned example, Hope it will help you.

    Thanks & Regards
    Abhijeet Namdeo


    Java Code:
    // Sample message listener program.
    import java.io.IOException;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import javax.wireless.messaging.*;
    public class Example extends MIDlet implements MessageListener
    {
    MessageConnection messconn;
    boolean done;
    Reader reader;
    // Initial tests setup and execution.
    public void startApp()
    {
    try
    {
    // Get our receiving port connection.
    messconn = (MessageConnection)
    Connector.open(“sms://:6222”);
    // Register a listener for inbound messages.
    messconn.setMessageListener(this);
    // Start a message-reading thread.
    done = false;
    reader = new Reader();
    new Thread(reader).start();
    } catch (IOException e)
    {
    // Handle startup errors
    }
    }
    // Asynchronous callback for inbound message.
    public void notifyIncomingMessage(MessageConnection conn)
    {
    if (conn == messconn)
    {
    reader.handleMessage();
    }
    }
    // Required MIDlet method - release the connection and
    // signal the reader thread to terminate.
    public void pauseApp()
    {
    done = true;
    try
    {
    messconn.close();
    } catch (IOException e)
    {
    // Handle errors
    }
    }
    // Required MIDlet method - shutdown.
    // @param unconditional forced shutdown flag
    public void destroyApp(boolean unconditional)
    {
    done = true;
    try
    {
    messconn.setMessageListener(null);
    messconn.close();
    } catch (IOException e)
    {
    // Handle shutdown errors.
    }
    }
    }
    
    // Isolate blocking I/O on a separate thread, so callback
    // can return immediately.
    class Reader implements Runnable
    {
    private int pendingMessages = 0;
    // The run method performs the actual message reading.
    public void run()
    {
    while (!done)
    {
    synchronized(this)
    {
    if (pendingMessages == 0)
    {
    try
    {
    wait();
    } catch (Exception e)
    {
    // Handle interruption
    }
    }
    pendingMessages--;
    }
    // The benefit of the MessageListener is here.
    // This thread could via similar triggers be
    // handling other kind of events as well in
    // addition to just receiving the messages.
    try
    {
    Message mess = messconn.receive();
    } catch (IOException ioe)
    {
    // Handle reading errors
    }
    }
    }
    public synchronized void handleMessage()
    {
    pendingMessages++;
    notify();
    }
    }
    Last edited by abhijeetrajenamdeo; 10-28-2009 at 09:41 AM.

  5. #5
    ashah is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    hi

    Abhijeet thanks for reply. I tried your sugested code but i am not able to recive sms. When i open an applcation it will goes in to catch portion.

Similar Threads

  1. Replies: 0
    Last Post: 04-22-2009, 01:30 PM
  2. #key listener problem
    By mij1_7 in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 09:02 PM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Tags for this Thread

Posting Permissions

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