Mobile Computing (Kelas Kamis) – Simulasi REG(spasi)XXX


SMS atau Short Message Service merupakan salah satu fitur dalam dunia Mobile yang sangat primer, artinya sebagian besar menggunakan fitur ini dalam melaksanakan proses komunikasi.

 

 

Berikut adalah Source code “di balik layar” SMS:

 

UNTUK SERVER:


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.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class terimaSMS extends MIDlet implements Runnable, CommandListener {

    private TextBox tb;
    private String text = "Menunggu pesan...";
    private Command okComand;

    public terimaSMS() {
        tb = new TextBox("Hello MIDlet", text, 150, 0);
    }

    protected void destroyApp(boolean arg0) {
        notifyDestroyed();
    }

    protected void pauseApp() {
    }

    protected void startApp() {
        try {
            Display.getDisplay(this).setCurrent(tb);
            new Thread(this).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addConfirmation(String msg) {
        tb = new TextBox("SMS MIDlet", text, 150, 0);
        tb.setString(msg);
        okComand = new Command("OK", Command.OK, 1);
        tb.addCommand(okComand);
        tb.setCommandListener(this);
        Display.getDisplay(this).setCurrent(tb);
    }

    public void run() {
        try {
            MessageConnection rconn = (MessageConnection) Connector.open("sms://:1234");
            while (true) {
                Message msg = rconn.receive();
                if (msg instanceof TextMessage) {
                    TextMessage tmsg = (TextMessage) msg;
                    String msgOut = tmsg.getPayloadText();
                    addConfirmation(msgOut);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void commandAction(Command arg0, Displayable arg1) {
    }
}

UNTUK CLIENT


import javax.microedition.io.Connector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class kirimSMS extends MIDlet implements CommandListener, Runnable {

    private Form sendMessageForm;
    private TextField number, msg;
    private Command cmdOK, cmdExit;
    private String telpNum, isiPesan = "";

    public kirimSMS() {
        cmdExit = new Command("Back", Command.EXIT, 2);
        cmdOK = new Command("Send", Command.SCREEN, 2);
        sendMessageForm = new Form("Send Message");
        msg = new TextField("Message:", null, 50, TextField.ANY);
        number = new TextField("Telepon:", "+5550000", 15, TextField.PHONENUMBER);

        sendMessageForm.append(number);
        sendMessageForm.append(msg);
        sendMessageForm.addCommand(cmdOK);
        sendMessageForm.addCommand(cmdExit);
        sendMessageForm.setCommandListener(this);
    }

    protected void destroyApp(boolean arg0) {
        notifyDestroyed();
    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        Display.getDisplay(this).setCurrent(sendMessageForm);
    }

    public void commandAction(Command c, Displayable arg1) {
        if (c == cmdOK) {
            telpNum = number.getString();
            isiPesan = msg.getString();
            new Thread(this).start();
            number.setString("");
            msg.setString("");
            Display.getDisplay(this).setCurrent(sendMessageForm);
        } else if (c == cmdExit) {
            destroyApp(true);
        }
    }

    public void run() {
        try {
            String nomorTelp = "sms://" + telpNum + ":1234";
            MessageConnection conn = (MessageConnection) Connector.open(nomorTelp);
            TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
            msg.setPayloadText(isiPesan);
            conn.send(msg);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Tugasnya adalah membuat simulasi SMS Premium  (Ketik REG<spasi> bla-bla-bla) Terserah teman2, Jadi di sini nati akan ada 2 device satu bertindak sebagai client (yang mengirim SMS) satu sebagai Server (Yang menerima SMS).

Setelah Server menerima SMS maka SECARA OTOMATIS akan mereply (membalas SMS) tanpa mengetik terlebih dahulu.

Contoh:

Client (Ketik) : REG PRIMBON
Server (Reply) : Selamat Datang di dunia Primbon, anda akan mendapatkan SMS tentang hal-hal gaib di sekitar Anda
Client (Ketik): rEg PriMBon
Server (Reply) : Selamat Datang di dunia Primbon, anda akan mendapatkan SMS tentang hal-hal gaib di sekitar Anda
Client (Ketik) : r39 pR1mBoOoNnN
Server (Reply) : Maaf SMS anda Terlalu Alay Sehingga Server tidak dapat membaca

Untuk Interfacenya tidak penting, yang penting teknik programmingnya:

Kemudian Setelah Dikirim:

Terakhir Server akan membaca String yang diterima kemudian Akan Automaticaly Reply

KUMPULKAN:

  • Seperti biasa agar lebih romantis, paling lambat (Malem Minggu besok)
  • Jam Malam (Biar sekalian Ronda Malam)
  • kepada yth: yere.getlife@yahoo.com
  • Subject: BANG_ADASMS_NIM

 

Salam Sayang!!!

Semangat, aku dan kamu bisa

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: