3

How to send SMS programmatically in BlackBerry?

And I read somewhere, I need server side as well as client side code for sending SMS. Is it true? For sending message from 1 device to another or from Emulator to device, Do I really need server side as well as client side code?

I found this code somewhere for client side, but I am not getting output.

private void sendSMS(String phone, String message) throws RuntimeException, ServicesManagerException, IOException 
{
    // TODO Auto-generated method stub

    System.out.println("in send sms function");
    MessageConnection conn =
        (MessageConnection)Connector.open("sms://+919099087960");
    BinaryMessage msgOut = (BinaryMessage) conn.newMessage(MessageConnection.BINARY_MESSAGE);
    msgOut.setPayloadData("my binary payload".getBytes("UTF-8"));
    conn.send(msgOut);
}
Cœur
  • 32,421
  • 21
  • 173
  • 232
Riddhi Barbhaya
  • 1,165
  • 1
  • 10
  • 19

1 Answers1

5

You don't need any server side code. Check following code.

static String msg="hai";
try {
    new Thread() {
        public void run() {
            if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
                DatagramConnection dc = null;
                try {
                    dc = (DatagramConnection) Connector.open("sms://+919099087960");
                    byte[] data = msg.getBytes();
                    Datagram dg = dc.newDatagram(dc.getMaximumLength());
                    dg.setData(data, 0, data.length);
                    dc.send(dg);
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            try {
                                System.out.println("Message Sent Successfully : Datagram");
                                Dialog.alert("Message Sent Successfully");
                            } catch (Exception e) {
                                System.out.println("Exception  : " + e.toString());
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    System.out.println("Exception  : " + e.toString());
                    e.printStackTrace();
                } finally {
                    try {
                        dc.close();
                        dc = null;
                    } catch (IOException e) {
                        System.out.println("Exception  : " + e.toString());
                        e.printStackTrace();
                    }
                }
            } else {
                MessageConnection conn = null;
                try {
                    conn = (MessageConnection) Connector.open("sms://+919099087960");
                    //generate a new text message
                    TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                    //set the message text and the address
                    tmsg.setAddress("sms://+919099087960");
                    tmsg.setPayloadText(msg);
                    //finally send our message
                    conn.send(tmsg);
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            try {
                                System.out.println("Message Sent Successfully : TextMessage");
                                Dialog.alert("Message Sent Successfully : TextMessage");
                            } catch (Exception e) {
                                System.out.println("Exception  : " + e.toString());
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    System.out.println("Exception  : " + e.toString());
                    e.printStackTrace();
                } finally {
                    try {
                        conn.close();
                        conn = null;
                    } catch (IOException e) {
                        System.out.println("Exception  : " + e.toString());
                        e.printStackTrace();
                    }
                }
            }
        }
    }.start();
} catch (Exception e) {
    System.out.println("Exception  : " + e.toString());
    e.printStackTrace();
}
Rupak
  • 3,674
  • 12
  • 23
Signare
  • 4,175
  • 5
  • 22
  • 44
  • @Signare-I have a same kind of problem...I can run app to completion (I get Dialog box for "Message Sent Successfully : TextMessage") but I am not seeing my message in 'Messages' folder of BlackBerry Simulator! Can u help me out in that ?? – Name is Nilay Dec 01 '12 at 05:56