Question

I found free source project to send sms using java comm: http://code.google.com/p/find-ur-pal/source/browse/src/?r=21

Function sending sms looks like this:

  public void run(){

        boolean timeOut=false;
        long startTime=(new Date()).getTime();



        while ((step <7) && (!timeOut)){
//        log(""+((new Date()).getTime() - startTime);
          //check where we are in specified delay
          timeOut=((new Date()).getTime() - startTime)>delay;

          //if atz does not work, type to send cntrlZ and retry, in case a message was stuck
          if (timeOut && (step==1)) {
              step=-1;
              mySerial.send(        ""+cntrlZ);
          }

          //read incoming string
          String result=  mySerial.getIncommingString() ;

//      log ("<- "+result+"\n--------");
          int expectedResult=-1;

          try{
            //log ("Step:"+step);

            switch (step){
              case 0:

                mySerial.send("atz");
                delay=LONG;
                startTime=(new Date()).getTime();
                break;

              case 1:
                delay=STANDARD;
                mySerial.send("ath0");
                startTime=(new Date()).getTime();
                break;
              case 2:
                expectedResult=result.indexOf("OK");

                //log ("received ok ="+expectedResult);
                if (expectedResult>-1){
                  mySerial.send("at+cmgf=1");
                  startTime=(new Date()).getTime();
                }else{
                    step=step-1;
                }
                break;
              case 3:
                expectedResult=result.indexOf("OK");

               // log ("received ok ="+expectedResult);
                if (expectedResult>-1){
                  mySerial.send("at+csca=\""+csca+"\"");
                  startTime=(new Date()).getTime();
                }else{
                  step=step-1;
                }

                break;
              case 4:
                expectedResult=result.indexOf("OK");

               // log ("received ok ="+expectedResult);
                if (expectedResult>-1){
                  mySerial.send("at+cmgs=\""+recipient+"\"");
                  startTime=(new Date()).getTime();
                }else{
                  step=step-1;
                }

                break;
              case 5:
                expectedResult=result.indexOf(">");

               // log ("received ok ="+expectedResult);
                if (expectedResult>-1){
                  mySerial.send(message+cntrlZ);
                  startTime=(new Date()).getTime();
                }else{
                  step=step-1;
                }
                delay=VERYLONG;//waitning for message ack

                break;

              case 6:
                expectedResult=result.indexOf("OK");
                //read message number
                if (expectedResult>-1){
                  int n=result.indexOf("CMGS:");
                  result=result.substring(n+5);
                  n=result.indexOf("\n");
                  status=0;
                  messageNo=Long.parseLong(result.substring(0,n).trim() );

                  log ("sent message no:"+messageNo);


                }else{
                  step=step-1;
                }

              break;
            }
            step=step+1;

            aThread.sleep(100);

          }catch (Exception e){
              e.printStackTrace();
          }
        }

        mySerial.closeConnection() ;

        //if timed out set status

        if (timeOut ) {
            status=-2;
            log("*** time out at step "+step+"***");
        }
      }

AT commands are sending according to specifications. And it works perfectly, but now I have read sms from Inbox. I have write similar function like this:

public void receiveMessage() throws Exception
      {
          int expectedResult = 0;

          SerialParameters params = defaultParameters;

            mySerial =new SerialConnection (params);

            mySerial.openConnection();

            // step 1
            mySerial.send("atz");
            delay=LONG;

            Thread.sleep(100);
            //aThread.sleep(100);

            String result=  mySerial.getIncommingString() ;

            // step 2
            delay=STANDARD;
            mySerial.send("ath0");
            Thread.sleep(100);

            // step 3
            result=  mySerial.getIncommingString() ;
            expectedResult=result.indexOf("OK");

            //log ("received ok ="+expectedResult);
            if (expectedResult>-1){
              mySerial.send("at+cmgf=1");
              //startTime=(new Date()).getTime();
            }

            // step 4
            result=  mySerial.getIncommingString() ;
            expectedResult=result.indexOf("OK");

            //log ("received ok ="+expectedResult);
            if (expectedResult>-1){
              //mySerial.send("at+cmgl=\"ALL\"");
                mySerial.send("at+cmgr=1");
              //startTime=(new Date()).getTime();
            }

            Thread.sleep(100);
            result=  mySerial.getIncommingString() ;

      }

In step 1 I send atz command and I got response OK then command ath0 and response OK and then command at+cmgl=\"ALL\" and again response OK, but where are my messages ? I was thinking then the last response (getIncommingString) should contain messages read from inbox.

I know that are SMSLib and other libraries. But to use that libraries I have to added a lot of other libraries (for logging). I want to have simple application to send and receive sms.

Thanks

Was it helpful?

Solution

If you are reading SMS's from the SIM Card then you must first execute AT+CMGL to find out the indexes of any SMS's (SMS-DELIVERs) stored. Then you need to use AT+CMGR to read a specific SMS. Are you working in PDU mode or Text mode?

Just as a side note. Why are you sending ATZ and ATH0 commands? These are profile and call related commands.

To see all message statuses allowed from your modem:

AT+CGML=?

A typical response would be:

+CMGL: ("REC UNREAD","REC READ","STO UNSENT","STO SENT","ALL")

So to look at all messages on your SIM card:

AT+CGML="ALL"

To see all unread (new) messages on your SIM card:

AT+CGML="REC UNREAD"

There is another option where you can prevent SMS messages being stored on your SIM card. This is controlled by using the AT+CNMI command to configure unsolicited messages to be enabled. Then whenever a SMS is received then you will receive a +CMT message asynchronously. If you want to know more about that one just let me know.

There are a few benefits making use of the unsolicited approach. The main one being you don't have to manage your SIM cards memory (no risk of it getting full). Also with large quantities of SMS's your SIM card can actually become unusable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top