如何使用java comm从gsm读取短信?

发布于 2024-12-26 18:19:29 字数 5488 浏览 0 评论 0原文

我找到了使用 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 和其他库。但要使用该库,我必须添加许多其他库(用于日志记录)。我想要一个简单的应用程序来发送和接收短信。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2025-01-02 18:19:29

如果您正在从 SIM 卡读取短信,则必须首先执行 AT+CMGL 来查找存储的任何短信 (SMS-DELIVER) 的索引。然后你需要使用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 命令配置要启用的未经请求的消息来控制的。然后,每当收到 SMS 时,您都会异步收到 +CMT 消息。如果您想了解更多信息,请告诉我。

使用主动提供的方法有一些好处。主要的一点是您无需管理您的 SIM 卡内存(没有内存已满的风险)。此外,如果短信数量过多,您的 SIM 卡实际上可能会变得无法使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文