Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Problem: Can't send Text SMS from Device.

Hello everybody.

I was trying the following code to send SMS but I am not able to:

 

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() {
int nwType=RadioInfo.getNetworkType();
synchronized(Application.getEventLock()) {
TrackerScreen.showMsg(""+nwType);
}
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
DatagramConnection dc = null;
try {
dc = (DatagramConnection) Connector.open("sms://" + to);
synchronized(Application.getEventLock()) {
TrackerScreen.showMsg("Connection open..");
}
byte[] data = msg.getBytes();
Datagram dg = dc.newDatagram(dc.getMaximumLength());
dg.setData(data, 0, data.length);
synchronized(Application.getEventLock()) {
TrackerScreen.showMsg("Data Set.");
}
dc.send(dg);
synchronized(Application.getEventLock()) {
TrackerScreen.showMsg("Message Sent Successfully : Datagram");
}
System.out.println("Message Sent Successfully : Datagram");
//Dialog.alert("Message Sent Successfully : Datagram");
} catch (Exception e) {
System.out.println("Exception 1 : " + e.toString());
e.printStackTrace();
} finally {
try {
dc.close();
dc = null;
} catch (IOException e) {
System.out.println("Exception 2 : " + e.toString());
e.printStackTrace();
}
}
} else {
MessageConnection conn = null;
try {
conn = (MessageConnection) Connector.open("sms://" + to);
//generate a new text message
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set the message text and the address
tmsg.setAddress("sms://" + to);
tmsg.setPayloadText(msg);
//finally send our message
conn.send(tmsg);
System.out.println("Message Sent Successfully : TextMessage");
//Dialog.alert("Message Sent Successfully : TextMessage");
} catch (IOException e) {
System.out.println("IOException : " + 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("IOException 4 : " + e.toString());
e.printStackTrace();
}
}
}
}
}

 

My device executes the "else" part and establishes a MessageConnection. It shows IOException when it executes the line highlighted above. Also, the nwType variable shows the value 3 ie. NETWORK_GPRS. I'm using the following line to call:

 

new SendSMS("+<some 12 digit no. with country code>","Some Text").start();

 

But the code above doesn't send any SMS. Could anybody correct me in this. Thanks in advance.