0

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)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LaRb
  • 3
  • 1
  • fyi I send texts through my phone using termux, having connected to it over ssh. Using termux-sms-send is tons easier than what you're doing – g00se Aug 06 '22 at 12:47
  • I can't use Termux because it's an Android terminal emulator and Linux environment app, and, in my case, i'm building a desktop application and my laptop is running windows 11. Do you have any other suggestion ? – LaRb Aug 08 '22 at 09:03
  • I don't really understand your comments. The only way in which they would be valid is if in some way the Android wasn't real and you couldn't install anything on it. I do it in the way I mentioned precisely *so* I can send 'free' text messages from my desktop. But the answer to your comment question is no. AT commands are legacy stuff and not the way forward – g00se Aug 08 '22 at 11:52
  • Do you want your code to work reliably? If so you **MUST** immediately stop using `Thread.sleep` like that. Like the very first code change you do after reading this comment and [this answer](https://stackoverflow.com/a/46064206/23118). – hlovdal Aug 08 '22 at 21:43

0 Answers0