Skip to main content

Different approach to Receiving SMS

1 reply [Last post]
Anonymous

Hi,

I am developing a MIDlet that is a sms client. I have created test send and
receive MIDlets that you can specify the port number. Both MIDlets have
proven to work on numerous handsets by different manufacturers.

I am now working on the Samsung X450, I've had problems with other Samsungs
with regards to sms but trying these fixes have not proved to work.
The X450 can send sms's using the main application and the test send MIDlet.
But when testing for receiving an sms no matter which way I try, the sms
always goes to the phones inbox and never gets intercepted by the receive
MIDlet.

Here is the code for the Recevie MIDlet. If anyone could point the way in
getting this code to work on the Samsung it would be much appreciated.

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import javax.wireless.messaging.*;

public class SmsReceive extends MIDlet implements CommandListener, Runnable,
MessageListener {
MessageConnection connection;
String payload;
String url;
Form form;

private Display display;

private Form firstScreen;
private StringItem phoneNumber;
private StringItem messageText;
private StringItem portNum;
private TextField portNumber;

private Alert secondScreen;

private Command exitMIDlet;
Command back;

private static int PORTNUMBER = 50000;

public SmsReceive() {

// assorted commands

exitMIDlet = new Command("Exit", Command.EXIT, 1);
back = new Command("Back", Command.BACK, 1);

// displaying a received message

firstScreen = new Form("New message received:");
phoneNumber = new StringItem("number:",null);
messageText = new StringItem("message:",null);
portNum = new StringItem("port:",null);
portNumber = new TextField("port:",null,64,TextField.NUMERIC);
firstScreen.append(phoneNumber);
firstScreen.append(messageText);
firstScreen.append(portNum);
//firstScreen.append(portNumber);
firstScreen.addCommand(exitMIDlet);
firstScreen.addCommand(back);
firstScreen.setCommandListener(this);

// alerting the user

secondScreen = new Alert("Alert"," New message!",null,AlertType.INFO);
secondScreen.setTimeout(Alert.FOREVER);

//display = Display.getDisplay(this);
//display.setCurrent(firstScreen);

// create the thread that will keep receiving messages

}

protected void startApp() {
display = Display.getDisplay(this);
form = new Form("Receive");
form.append(portNumber);
Command cmd = new Command("Enter", Command.OK, 1);
form.addCommand(cmd);
form.setCommandListener(this);
display.setCurrent(form);
}

protected void destroyApp(boolean unconditional) {

}

protected void pauseApp() {
}

public void commandAction(Command c, Displayable s) {
if(c.getLabel().equals("Enter")) {
if(!portNumber.getString().equals(" ")) {
try {
PORTNUMBER = Integer.parseInt(portNumber.getString());
} catch (NumberFormatException nfe) {
System.out.println("Exception: " + nfe);
}
}
url = "sms://"+":"+ PORTNUMBER; // server mode connection
portNum.setText("" + PORTNUMBER);

Thread t = new Thread(this);
t.start();
display.setCurrent(firstScreen);
}
if (c == exitMIDlet) {
destroyApp(true);
notifyDestroyed();
return;
}

if(c == back) {
display.setCurrent(form);
}
}

public void run() {
connection = null;
Message message;
TextMessage textMessage;
BinaryMessage binaryMessage;
String number;
String text;

try {
connection = (MessageConnection) Connector.open(url);
connection.setMessageListener(this);

/*while(true) {
message = connection.receive();
number = message.getAddress();
phoneNumber.setText(number);
if(message instanceof TextMessage){
textMessage = (TextMessage)message;
text = textMessage.getPayloadText();
} else {
text = "<<>>";
}
messageText.setText(text);
display.setCurrent(secondScreen,firstScreen);
}*/

} catch (Exception e) {
e.printStackTrace();
try {
if(connection != null)
connection.close();
} catch(Exception e1) {
e1.printStackTrace();
}
return;
}
}

public void notifyIncomingMessage(MessageConnection msgConn) {
MessengerThread t = new MessengerThread();
t.start();
}

private class MessengerThread extends Thread {
private Message msg;

public MessengerThread(Message msg) {
this.msg = msg;
}

public MessengerThread() {
this.msg = null;
}

public void run() {
String number;
try {
if (msg == null) {
Message msg = connection.receive();

number = msg.getAddress();
phoneNumber.setText(number);

if (msg instanceof BinaryMessage) {
BinaryMessage binary = (BinaryMessage)msg;
byte[] data = binary.getPayloadData();
payload = new String(data, "UTF-8");
} else if (msg instanceof TextMessage) {
TextMessage text = (TextMessage)msg;
payload = text.getPayloadText();
}
messageText.setText(payload);
} else {
connection.send(msg);
}
} catch (IOException ioe) {
throw new RuntimeException(ioe.getMessage());
}
}
}
}

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
terrencebarr
Offline
Joined: 2004-03-04
Points: 0

Ian,

Sorry for the slow reply. I was hoping someone with Samsung expertise on the forum would reply.

Unfortunatly we at Sun can't provide help on an implementation if it is not based on Sun's commercial or open source bits. In the case of the Samsung X450 I am not sure where the platform implementation came from.

-- Terrence

Terrence Barr
Evangelist, Java Mobile & Embedded Community
Sun Microsystems, Germany
www.mobileandembedded.org