2

我找到了使用 java comm 发送短信的免费源项目: http ://code.google.com/p/find-ur-pal/source/browse/src/?r=21

发送短信的函数如下所示:

  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 命令正在根据规范发送。它工作得很好,但现在我已经从收件箱中阅读了短信。我写过类似的函数:

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() ;

      }

在第 1 步中,我发送 atz 命令并得到响应 OK,然后是命令 ath0 和响应 OK,然后是命令 at+cmgl=\"ALL\" 并再次响应 OK,但是我的消息在哪里?我当时在想最后一个响应(getIncommingString)应该包含从收件箱中读取的消息。

我知道那是 SMSLib 和其他库。但是要使用这些库,我必须添加很多其他库(用于日志记录)。我想要一个简单的应用程序来发送和接收短信。

谢谢

4

1 回答 1

1

如果您正在从 SIM 卡中读取 SMS,则必须首先执行 AT+CMGL 以查找存储的任何 SMS(SMS-DELIVERs)的索引。然后你需要使用 AT+CMGR 来读取特定的短信。您是在 PDU 模式还是文本模式下工作?

就像旁注一样。为什么要发送 ATZ 和 ATH0 命令?这些是配置文件和调用相关的命令。

要查看调制解调器允许的所有消息状态:

AT+CGML=?

一个典型的反应是:

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

因此,要查看 SIM 卡上的所有消息:

AT+CGML="ALL"

要查看 SIM 卡上的所有未读(新)信息:

AT+CGML="REC UNREAD"

还有另一个选项可以防止将 SMS 消息存储在 SIM 卡上。这是通过使用 AT+CNMI 命令来配置要启用的未经请求的消息来控制的。然后,无论何时收到短信,您都会异步收到一条+CMT 消息。如果您想了解更多关于那个的信息,请告诉我。

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.

于 2012-01-12T13:00:19.863 回答