Thursday, April 7, 2011

Send SMS in Blackberry using J2ME

Hello,

When i had to implement this feature then i surfed a lot.I was new to Blackberry and J2ME.
I saw several posts but non was able to help me truely.

I gathered up the code and made it to work.
I had to send the message to the person whose call i missed up or to person whose incomming call is explicitly disconnected by me when i was busy.

The moment i disconnect the call, immediately a message is send to thta number.
That too should be done in a separate thread to avoid hang ups.

Hope it help someone. :)


Send SMS from Blackberry Application:

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import net.rim.device.api.system.RadioInfo;
public class SendSMS extends Thread {
    private String to;
    private String msg;

    public SendSMS(String to, String msg) {
        this.to = to;
        this.msg = msg;

    }
    public void run() {
        if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
            DatagramConnection dc = null;
            try {
                dc = (DatagramConnection) Connector.open("sms://" + to);
                byte[] data = msg.getBytes();
                Datagram dg = dc.newDatagram(dc.getMaximumLength());
                dg.setData(data, 0, data.length);
                dc.send(dg);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dc.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }

        } else {
            MessageConnection mc = null;
            try {
                mc = (MessageConnection) Connector
                        .open("sms://" + to);
                TextMessage m = (TextMessage) mc
                        .newMessage(MessageConnection.TEXT_MESSAGE);
                m.setPayloadText(msg);
                mc.send(m);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    mc.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());

                }
            }
        }
    }

}

Send msg when call gets disconnected:
public void callDisconnected(int callId)
{
        final PhoneCall call = Phone.getCall(callId);
        final String number = call.getDisplayPhoneNumber();
        SendSMS sendSMS = new SendSMS(number, "message");
        sendSMS.start();
        super.callDisconnected(callId);
    }
You can try other methods also depending upon the requirements.


Links:
http://stackoverflow.com/questions/3051301/send-sms-from-background-thread-in-blackberry-using-j2me

2 comments:

  1. Dear Swati,

    Me Pawan, i am new Black Berry Developer............

    can you help me......... something in Development.

    email: pankajace12@gmail.com

    ReplyDelete
  2. sure you can join developer forums of blackberry as well as stackoverflow. i am active on both.or u can post comments here and may be i can help u

    ReplyDelete