4

How to Implement a MIDlet that gets invoked when a SMS is sent to port 50000?

The code is not working. SMS can't be received on the phone, SMS is sent through the emulator (JAVA Me SDK).

What settings should be done to receive the SMS ?

my code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import javax.microedition.io.PushRegistry;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 * @author bonni
 */
public class Midletsms extends MIDlet implements CommandListener{

    protected Display display;                                  
     //boolean started=false;
      Form form = new Form("Welcome");
      Command mCommandQuit;

    public void startApp() {

        String url = "sms://:50000";
        try {

            PushRegistry.registerConnection(url,this.getClass().getName(), "*");
           // PushRegistry.registerConnection(url,"Midletsms.class", "*");
        } catch (IOException ex) {

        } catch (ClassNotFoundException ex) {

        }
         form.append("This midlet gets invoked when message is sent to port:50000");



             display = Display.getDisplay(this);
             display.setCurrent(form);

                   mCommandQuit = new Command("Quit", Command.EXIT, 0);
             form.addCommand(mCommandQuit);
             form.setCommandListener(this);


    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
     public void commandAction(Command c, Displayable d) {
       // throw new UnsupportedOperationException("Not supported yet.");
         String label = c.getLabel();
        if(label.equals("Quit"))
                {
                        destroyApp(false);
            notifyDestroyed();
                }
     }
}
gnat
  • 6,213
  • 108
  • 53
  • 73
matko
  • 41
  • 1
  • 1
    are you aware that JSR 120 and JSR 205 API ([WMA](http://stackoverflow.com/questions/tagged/wma)) are used for this? code snippet you posted does not use these, why? – gnat Sep 12 '12 at 20:13
  • 1
    @gnat All he's doing is registering the push connection dynamically. IIRC this doesn't require use of WMA APIs... – funkybro Sep 13 '12 at 09:10
  • @funkybro I see. that makes a question interesting. I can't recall midlet that got push notification that way without WMA, can't figure whether this is possible or not – gnat Sep 13 '12 at 09:19

2 Answers2

2

Not sure I fully understand the problem. But you need to read about PushRegistry.

So there are two types of push registration, static and dynamic.

The code example you have given uses dynamic registration. You will need to manually invoke this MIDlet at least once in order for the push registration to happen. (Aside: In your example you are doing this in the startApp method, this is a very bad idea! Push registration is a potentially blocking operation, and therefore should not be done in a lifecycle method such as startApp. You should do this in a new thread).

The alternative is static registration, where you include the push information in the jad. The push port will be registered when the MIDlet is installed, without the need to run it.

Finally, you say

sms is sent through the emulator

what does this mean? In order for the app to start you need to send an SMS on the relevant port number from another MIDlet (this could be on the same handset if you want).

funkybro
  • 8,432
  • 6
  • 39
  • 52
0

I found this code on net from Jimmy's blog and it is perfectly working. You can try it your self,

SMSSender.java

public class SMSSender extends MIDlet implements CommandListener {

    private Form formSender = new Form("SMS Sender");
    private TextField tfDestination = new TextField("Destination", "", 20, TextField.PHONENUMBER);
    private TextField tfPort = new TextField("Port", "50000", 6, TextField.NUMERIC);
    private TextField tfMessage = new TextField("Message", "message", 150, TextField.ANY);
    private Command cmdSend = new Command("Send", Command.OK, 1);
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);
    private Display display;

    public SMSSender() {
        formSender.append(tfDestination);
        formSender.append(tfPort);
        formSender.append(tfMessage);
        formSender.addCommand(cmdSend);
        formSender.addCommand(cmdExit);
        formSender.setCommandListener(this);

        display = Display.getDisplay(this);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        display.setCurrent(formSender);
    }

    public void commandAction(Command c, Displayable d) {
        if (c==cmdSend) {
            SendMessage.execute(tfDestination.getString(), tfPort.getString(), tfMessage.getString());
        } else if (c==cmdExit) {
            notifyDestroyed();
        }
    }

}



class SendMessage {

    public static void execute(final String destination, final String port, final String message) {

        Thread thread = new Thread(new Runnable() {

            public void run() {
                MessageConnection msgConnection;
                try {
                    msgConnection = (MessageConnection)Connector.open("sms://"+destination+":" + port);
                    TextMessage textMessage = (TextMessage)msgConnection.newMessage(
                            MessageConnection.TEXT_MESSAGE);
                    textMessage.setPayloadText(message);
                    msgConnection.send(textMessage);
                    msgConnection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
    }

}

SMSReceiver.java

public class SMSReceiver extends MIDlet implements CommandListener, MessageListener {

    private Form formReceiver = new Form("SMS Receiver");
    private TextField tfPort = new TextField("Port", "50000", 6, TextField.NUMERIC);
    private Command cmdListen = new Command("Listen", Command.OK, 1);
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);
    private Display display;

    public SMSReceiver() {
        formReceiver.append(tfPort);
        formReceiver.addCommand(cmdListen);
        formReceiver.addCommand(cmdExit);
        formReceiver.setCommandListener(this);

        display = Display.getDisplay(this);
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        display.setCurrent(formReceiver);
    }

    public void commandAction(Command c, Displayable d) {
        if (c==cmdListen) {
            ListenSMS sms = new ListenSMS(tfPort.getString(), this);
            sms.start();
            formReceiver.removeCommand(cmdListen);
        } else if (c==cmdExit) {
            notifyDestroyed();
        }
    }

    public void notifyIncomingMessage(MessageConnection conn) {
        Message message;
        try {
            message = conn.receive();
            if (message instanceof TextMessage) {
                TextMessage tMessage = (TextMessage)message;
                formReceiver.append("Message received : "+tMessage.getPayloadText()+"\n");
            } else {
                formReceiver.append("Unknown Message received\n");
            }
        } catch (InterruptedIOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class ListenSMS extends Thread {
    private MessageConnection msgConnection;
    private MessageListener listener;
    private String port;

    public ListenSMS(String port, MessageListener listener) {
        this.port = port;
        this.listener = listener;
    }

    public void run() {
        try {
            msgConnection = (MessageConnection)Connector.open("sms://:" + port);
            msgConnection.setMessageListener(listener);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Lucifer
  • 29,392
  • 25
  • 90
  • 143