J2ME: sending human readable SMS
A short entry ’cause Java bothered me today.
If you want to send a human readable SMS with J2ME do NOT supply a port - else the SMS would not get delivered (or even sent). Somehow this is not really well documented and solutions are difficult to find…
And a sample for that (copy paste to your favourite editor):
import javax.wireless.messaging.*;
import javax.microedition.io.Connector;public MessageConnection newMessageConnection(String addr)
throws Exception {
return((MessageConnection)Connector.open(addr));
}public void sendTextMessage(MessageConnection mc, String
msg, String url) {
try {
TextMessage tmsg =
(TextMessage)mc.newMessage
(MessageConnection.TEXT_MESSAGE);
if (url!= null)
tmsg.setAddress(url);
tmsg.setPayloadText(msg);
mc.send(tmsg);
}
catch(Exception e) {
error.setText (”send failed ” + e);
}
}public void sendSMS ( String msg )
{
MessageConnection mc;try
{
mc = newMessageConnection(”sms://” + phoneNo.getString());
} catch ( Exception e )
{
error.setText(”failed to create message connection!”);
return;
}info.setText(”sending message…”);
sendTextMessage(mc, msg, null);
info.setText(”sent message!”);try
{
mc.close();
} catch ( Exception e )
{
error.setText(”error closing connecting!”);
}
}
StringItems error and info for output used, TextField phoneNo for the phone number.