I am using Java to send SMS from my 3G dongle (GSM modem) using AT commands. It is mostly working, but at times the SMS text contains part of the AT commands fired prior to that. This is intermittent, but needs to be fixed.
The relevant code is as follows:
public void sendMessage(String phoneNumber, String message) throws InterruptedException {
char qu=34;
char cz=26;
send("AT+CMGF=1\r\n");
Thread.sleep(2000);
send("AT+CMGS=" + qu + phoneNumber + qu + ",145\r\n");
send(message + cz + "\r");
}
public static void main(String args[]) {
GSMConnect gsm = new GSMConnect("COM22");
if (gsm.init()) {
try {
gsm.connect();
Thread.sleep(2000);
gsm.sendMessage("+9172xxxxxxxx", "Test Message sent from GSM Modem using AT Commands.");
System.out.println("Sleeping for 20 secs");
Thread.sleep(20000);
gsm.hangup();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Can't init this card");
}
}
The message I receive at times is as follows:
AT+CMGS="+9172xxxxxxxx", 145 Test Message sent from GSM Modem using AT Commands.
============
Thanks in advance for your help!
Regards, Kumarjit
\r
and not\r\n
. stackoverflow.com/a/21503919/23118 – hlovdal