Getting error when trying to send SMS with AT commands in Java

523 Views Asked by At

I'm trying to send SMS using AT commands, through connected mobile cell phone with USB port. It can connect with the port but doesn't send any message.

This is a part of the Java code:

public void sendMessage(String phoneNumber, String message) {
    char quotes ='"';  
    send("AT+CMGS="+quotes + phoneNumber +quotes+ "\r\n");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    send(message + '\032');
    System.out.println("Message Sent");
}

public static void main(String args[]) {
    GSMConnect gsm = new GSMConnect(comPort);
    if (gsm.init()) {
        try {
            System.out.println("Initialization Success");
            gsm.connect();
            Thread.sleep(5000);
            gsm.checkStatus();
            Thread.sleep(5000);

            gsm.sendMessage("+xxxxxxxxxxxx", "Trial Success");

            Thread.sleep(1000);

            gsm.hangup();
            Thread.sleep(1000);
            gsm.closePort();
            gsm.outCommand();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("Can't init this card");
    }
}

And here is the output I get when I run the program:

Got PortName
Initialization Success
COM3: PORT_OWNED
6
OK
40
+CME Error:PACM(AP),UNREGISTED

OK
Message Sent
58AT+CMGS="+xxxxxxxxxxxx"

+CME ERROR: 614
Trial Success24ATH

+CME ERROR: 614
COM3: PORT_UNOWNED

C:\Users\Dell PC\AppData\Local\NetBeans\Cache\14\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Dell PC\AppData\Local\NetBeans\Cache\14\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 14 seconds)
0

There are 0 best solutions below