SMS Monitor in Java Midlet | Thales IoT Developer Community
March 28, 2017 - 7:34am, 4017 views
Hi,
I am trying to make a quite simple java midlet to monitor incoming SMS in EHS6 and I borrowed the code from the fotamidlet example which has the simlar functions. I used updater.java and removed those unwanted functions only left "startSmsMonitor()".
In the main.java. is like this:
protected void startApp() throws MIDletStateChangeException {
System.out.println("startApp");
try {
final ATCommand atc = new ATCommand(false);
System.out.println(atc.send("AT+CNMI=2,1\r")); // Enable SMS URC
try {
Updater.startSmsMonitor(); // Start Monitor
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final ATCommand atc2 = new ATCommand(false);
t = new Thread() {
public void run() {
int counter = 0;
try {
GsmUtils.switchToTextMode();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
try {
System.out.println(GsmUtils.sendSms(Main.this.getAppProperty("SmsNumber"), "QuarySms")); // Periodically sending an quary SMS to another modem
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep((Integer.parseInt(Main.this.getAppProperty("SmsPeriod")) * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
};
t.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
However the above code did not work. The Midlet didn't get ATEvent() trggered when an SMS was coming in. I could seen the received SMS by +CMGL. What did I miss in the code? Thanks.
Hello,
First advises (without analyzing the fotamidlet code) would be:
To be able to receive URC's in the Java MIDlet please make sure that the autostart is enabled and JRC MIDlet is running (AT^SCFG?, AT^SJAM=5). It is the factory MIDlet where some of the module's functionality is implemented.
The AT+CNMI=2,1 configuration works only for the AT command interpreter on which the command was sent. And if you configure it on another interpreter the previous one will no longer receive notifications. I works only on one interpreter at a time. So please make sure that you are using the same ATCommand instance for configuration of CNMI and listening for incoming messages.
Best regards,
Bartłomiej
Hi, Bartłomiej:
Thanks for you infomation. Yes. the autostart is enabled: ^SCFG: "Userware/Autostart","1", and the JRC Midelet is also running: ^SJAM: "a:/JRC-1.56.30.jad","Java Remote Control MIDlet Suite","Cinterion","1.56.30",1. The Module runs ARN14 f/w.
I understand that URC only works on the interface that +CNMI is sent and the command will reset the settings on other interfaces. In the code, the AT+CNMI=2,1 is sent via Java AT command API, which is defined in the GsmUtil.java (I didn't inlcude this file as it is the same as that in the sample code). The SMS URC listener is also via Java API. So I suppose the URC should come out of Java interface. But it is not happening by this code.
The code in the Updater.java is here:
public abstract class Updater {
protected static String apn = "internet";
protected static String apnUser = "apnUser";
protected static String apnPass = "apnPasw";
protected static String resultReceiver;
protected static String path = "";
protected static final String resultReceiverLabel = "RESULT_RECEIVER: ";
protected static final String historyFilePath = "fota_history.log";
protected static boolean bIsFotaUpdateProcessStarted = false;
/**
* Start the AT command listener to monitor the incoming URCs and parse each
* incoming SMS
*
* @throws IllegalStateException
* @throws ATCommandFailedException
*/
public static void startSmsMonitor() throws IllegalStateException, ATCommandFailedException {
GsmUtils.addUrcListener(new ATCommandListener() {
public void RINGChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void DSRChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void DCDChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void CONNChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void ATEvent(String Event) {
System.out.println("[AT EVENT] " + Event);
// if (Event.indexOf("+CMTI") > 0) {
// FotaSmsParser parser = new FotaSmsParser(Event);
// parser.start();
// }
}
});
System.out.println("SMS monitor started");
}
I guess something might be mssing and incorrect in the code preventing the ATEvent(Event) being triggerred. Maybe someone could shed some lights. Thanks.
Hi, Bartłomiej:
I figured out the problem. Yes. the ATCommand instance did the trick. The original code the listener worked on another instance so could not get URC. I changed the code to use the same instance and it worked. Thanks for your help.
Great that it's working!
Maybe I didn't make it clear enough but you have finally found out - each ATCommand instance and also each physical interface uses a separate AT commands interpreter.
Best regards,
Bartłomiej
Hi Luke,
Could you make available part of the code that you changed for the listener to use the same URC instance?
Hello,
The only trick is that +CMTI URC is only thrown for one AT commands interpreter - the same that was used for AT+CNMI configuration. So if you create an ATCommand instance in your code and using this instance you configure incoming SMS notifications with AT+CNMI command you need to also add the listener to this particular instance to be able to receive +CMTI URC's.
Example:
ATCommand mCmd = new new ATCommand(false);
mCmd.send("at+cnmi=2,1\r");
mCmd.addListener(mListener);
If you ever create another ATCommand instance in your code and configure new messages indications on this instance with AT+CNMI command, then the previous ATCommand instance will never receive more +CMTI notifications. The same if you configure this on any external interface.
Regards,
Bartłomiej