CATHandler_SonyEricsson.java
上传用户:shunchung
上传日期:2013-04-07
资源大小:438k
文件大小:6k
源码类别:

手机短信编程

开发平台:

Java

  1. //      jSMSEngine API.
  2. //      An open-source API package for sending and receiving SMS via a GSM device.
  3. //      Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
  4. //              Web Site: http://www.jsmsengine.org
  5. //
  6. //      jSMSEngine is a package which can be used in order to add SMS processing
  7. //              capabilities in an application. jSMSEngine is written in Java. It allows you
  8. //              to communicate with a compatible mobile phone or GSM Modem, and
  9. //              send / receive SMS messages.
  10. //
  11. //      jSMSEngine is distributed under the LGPL license.
  12. //
  13. //      This library is free software; you can redistribute it and/or
  14. //              modify it under the terms of the GNU Lesser General Public
  15. //              License as published by the Free Software Foundation; either
  16. //              version 2.1 of the License, or (at your option) any later version.
  17. //      This library is distributed in the hope that it will be useful,
  18. //              but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. //              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. //              Lesser General Public License for more details.
  21. //      You should have received a copy of the GNU Lesser General Public
  22. //              License along with this library; if not, write to the Free Software
  23. //              Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24. //
  25. package org.jsmsengine;
  26. import java.util.logging.*;
  27. /**
  28.        AT handler for SonyEricsson 2003, 2004 and 2005 series devices.
  29. */
  30. public class CATHandler_SonyEricsson extends CATHandler
  31. {
  32.        public CATHandler_SonyEricsson(CSerialDriver serialDriver, Logger log)
  33.        {
  34.                super(serialDriver, log);
  35.        }
  36.        public boolean  disableIndications() throws Exception
  37.        {
  38.                /* work out what command to send to disable indications, the phone
  39.                 * itself will tell us what it supports */
  40.                String atDisableIndications = "ATr";
  41.                // check for valid AT+CNMI values
  42.                serialDriver.send("AT+CNMI=?r");
  43.                // what comes back?
  44.                String cnmiTestResponse = serialDriver.getResponse();
  45.                if (cnmiTestResponse.toUpperCase().indexOf("+CNMI: (2)") >= 0)
  46.                {
  47.                        atDisableIndications = "AT+CNMI=2,0,0,0r";
  48.                }
  49.                else if (cnmiTestResponse.toUpperCase().indexOf("+CNMI: (3)") >= 0)
  50.                {
  51.                        atDisableIndications = "AT+CNMI=3,0,0,0r";
  52.                }
  53.                else
  54.                {
  55.                        // we don't know what to send
  56.                        return false;
  57.                }
  58.                serialDriver.send(atDisableIndications);
  59.                return (serialDriver.getResponse().equalsIgnoreCase("OKr"));
  60.        }
  61.        /*
  62.         * SonyEricsson's documentation on which phones support which
  63.         * variant of AT+CFUN is inaccurate, so the safest thing is
  64.         * to ask the phone what it supports.
  65.         */
  66.        public void reset() throws Exception
  67.        {
  68.                String atCFUN = "AT+CFUN=1r";
  69.                // check what is supported
  70.                serialDriver.send("AT+CFUN=?r");
  71.                String cfunTestResponse = serialDriver.getResponse();
  72.                if (cfunTestResponse.matches("\+CFUN: \([^)]+\),\([^)]+\)"))
  73.                {
  74.                        atCFUN = "AT+CFUN=1,1r";
  75.                }
  76.                serialDriver.send(atCFUN);
  77.                try { Thread.sleep(10000); } catch (Exception e) {}
  78.                serialDriver.getResponse();
  79.        }
  80.        // SonyEricssons require an additional <CR> after the <26> that indicates the end of the PDU
  81.        public boolean sendMessage(int size, String pdu) throws Exception
  82.        {
  83.                int responseRetries, errorRetries;
  84.                String response;
  85.                boolean sent;
  86.                errorRetries = 0;
  87.                while (true)
  88.                {
  89.                        responseRetries = 0;
  90.                        serialDriver.send(CUtils.substituteSymbol("AT+CMGS="{1}"r", ""{1}"", "" + size));
  91.                        Thread.sleep(100);
  92.                        while (!serialDriver.dataAvailable())
  93.                        {
  94.                                responseRetries ++;
  95.                                if (responseRetries == 4) throw new NoResponseException();
  96.                                log.log(Level.SEVERE, "CATHandler_SonyEricsson().SendMessage(): Still waiting for response (I) (" + responseRetries + ")...");
  97.                                Thread.sleep(5000);
  98.                        }
  99.                        responseRetries = 0;
  100.                        serialDriver.clearBuffer();
  101.                        serialDriver.send(pdu);
  102.                        serialDriver.send((char) 26);
  103.                        serialDriver.send((char) 13);   // special for SonyEricsson
  104.                        response = serialDriver.getResponse();
  105.                        while (response.length() == 0)
  106.                        {
  107.                                responseRetries ++;
  108.                                if (responseRetries == 4)  throw new NoResponseException();
  109.                                log.log(Level.SEVERE, "CATHandler_SonyEricsson().SendMessage(): Still waiting for response (II) (" + responseRetries + ")...");
  110.                                response = serialDriver.getResponse();
  111.                        }
  112.                        if (response.indexOf("OKr") >= 0)
  113.                        {
  114.                                sent = true;
  115.                                break;
  116.                        }
  117.                        else if (response.indexOf("CMS ERROR:") >= 0)
  118.                        {
  119.                                errorRetries ++;
  120.                                if (errorRetries == 4)
  121.                                {
  122.                                        log.log(Level.SEVERE, "GSM CMS Errors: Quit retrying, message lost...");
  123.                                        sent = false;
  124.                                        break;
  125.                                }
  126.                                else log.log(Level.SEVERE, "GSM CMS Errors: Possible collision, retrying...");
  127.                        }
  128.                        else sent = false;
  129.                }
  130.                return sent;
  131.        }
  132. }