⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cathandler.java

📁 SMSLib一个很有用的程序,有服务平台和收发平台
💻 JAVA
字号:
//	SMSLib for Java
//	An open-source API Library for sending and receiving SMS via a GSM modem.
//	Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
//		Web Site: http://www.smslib.org
//
//	SMSLib is distributed under the LGPL license.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

package org.smslib;

public class CATHandler
{
	protected final int RETRIES_NO_RESPONSE = 4;
	protected final int RETRIES_CMS_ERRORS = 4;
	protected final int DELAY_CMS_ERRORS = 10000;

	protected CSerialDriver serialDriver;
	protected CLogger log;

	public CATHandler(CSerialDriver serialDriver, CLogger log)
	{
		this.serialDriver = serialDriver;
		this.log = log;
	}

	public synchronized boolean dataAvailable() throws Exception
	{
		return serialDriver.dataAvailable();
	}

	public synchronized void sync() throws Exception
	{
		serialDriver.send("AT\r");
		serialDriver.clearBuffer();
		serialDriver.send("AT\r");
		serialDriver.clearBuffer();
		serialDriver.send("AT\r");
		serialDriver.clearBuffer();
	}

	public synchronized void reset() throws Exception
	{
	}

	public synchronized void echoOff() throws Exception
	{
		serialDriver.send("ATE0\r");
		serialDriver.clearBuffer();
	}

	public synchronized void init() throws Exception
	{
	}

	public synchronized boolean isAlive() throws Exception
	{
		serialDriver.send("AT\r");
		return (serialDriver.getResponse().matches("\\s*[\\p{ASCII}]*\\s+OK\\s"));
	}

	public synchronized boolean waitingForPin() throws Exception
	{
		serialDriver.send("AT+CPIN?\r");
		return (serialDriver.getResponse().indexOf("SIM PIN") >= 0);
	}

	public synchronized boolean enterPin(String pin) throws Exception
	{
		serialDriver.send(CUtils.substituteSymbol("AT+CPIN=\"{1}\"\r", "{1}", pin));
		Thread.sleep(10000);
		if (serialDriver.getResponse().indexOf("OK") > 0)
		{
			Thread.sleep(10000);
			return true;
		}
		else return false;
	}

	public synchronized boolean setVerboseErrors() throws Exception
	{
		serialDriver.send("AT+CMEE=1\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	public synchronized boolean setPduMode() throws Exception
	{
		serialDriver.send("AT+CMGF=0\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	public synchronized boolean enableIndications() throws Exception
	{
		serialDriver.send("AT+CNMI=1,1,0,0,0\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	public synchronized boolean disableIndications() throws Exception
	{
		serialDriver.send("AT+CNMI=0,0,0,0,0\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	public synchronized String getManufacturer() throws Exception
	{
		serialDriver.send("AT+CGMI\r");
		return serialDriver.getResponse();
	}

	public synchronized String getModel() throws Exception
	{
		serialDriver.send("AT+CGMM\r");
		return serialDriver.getResponse();
	}

	public synchronized String getSerialNo() throws Exception
	{
		serialDriver.send("AT+CGSN\r");
		return serialDriver.getResponse();
	}

	public synchronized String getImsi() throws Exception
	{
		serialDriver.send("AT+CIMI\r");
		return serialDriver.getResponse();
	}

	public synchronized String getSwVersion() throws Exception
	{
		serialDriver.send("AT+CGMR\r");
		return serialDriver.getResponse();
	}

	public synchronized String getBatteryLevel() throws Exception
	{
		serialDriver.send("AT+CBC\r");
		return serialDriver.getResponse();
	}

	public synchronized String getSignalLevel() throws Exception
	{
		serialDriver.send("AT+CSQ\r");
		return serialDriver.getResponse();
	}

	public synchronized boolean setStorageMEM() throws Exception
	{
		serialDriver.send("AT+CPMS=\"ME\"\r");
		return (serialDriver.getResponse().matches("\\s*[\\p{ASCII}]*\\s+OK\\s"));
	}

	public synchronized boolean setStorageSIM() throws Exception
	{
		serialDriver.send("AT+CPMS=\"SM\"\r");
		return (serialDriver.getResponse().matches("\\s*[\\p{ASCII}]*\\s+OK\\s"));
	}

	public synchronized void switchToCmdMode() throws Exception
	{
		serialDriver.send("+++");
		Thread.sleep(1000);
	}

	public synchronized boolean keepGsmLinkOpen() throws Exception
	{
		serialDriver.send("AT+CMMS=1\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	public synchronized int sendMessage(int size, String pdu) throws Exception
	{
		int responseRetries, errorRetries;
		String response;
		int refNo;

		errorRetries = 0;
		while (true)
		{
			responseRetries = 0;
			serialDriver.send(CUtils.substituteSymbol("AT+CMGS=\"{1}\"\r", "\"{1}\"", "" + size));
			Thread.sleep(300);
			while (!serialDriver.dataAvailable())
			{
				responseRetries ++;
				if (responseRetries == RETRIES_NO_RESPONSE) throw new NoResponseException();
				log.log(CLogger.LOG_WARNING, "ATHandler().SendMessage(): Still waiting for response (I) (" + responseRetries + ")...");
				Thread.sleep(5000);
			}
			responseRetries = 0;
			serialDriver.clearBuffer();
			serialDriver.send(pdu);
			serialDriver.send((char) 26);
			response = serialDriver.getResponse();
			while (response.length() == 0)
			{
				responseRetries ++;
				if (responseRetries == RETRIES_NO_RESPONSE)  throw new NoResponseException();
				log.log(CLogger.LOG_WARNING, "ATHandler().SendMessage(): Still waiting for response (II) (" + responseRetries + ")...");
				response = serialDriver.getResponse();
			}
			if (response.indexOf("OK\r") >= 0)
			{
				int i;
				String tmp = "";

				i = response.indexOf(":");
				while (!Character.isDigit(response.charAt(i))) i ++;
				while (Character.isDigit(response.charAt(i)))
				{
					tmp += response.charAt(i);
					i ++;
				}
				refNo = Integer.parseInt(tmp);
				break;
			}
			else if (response.indexOf("ERROR") >= 0)
			{
				errorRetries ++;
				if (errorRetries == RETRIES_CMS_ERRORS)
				{
					log.log(CLogger.LOG_ERROR, "GSM CMS Errors: Quit retrying, message lost...");
					refNo = -1;
					break;
				}
				else
				{
					log.log(CLogger.LOG_WARNING, "GSM CMS Errors: Possible collision, retrying...");
					Thread.sleep(DELAY_CMS_ERRORS);
				}
			}
			else refNo = -1;
		}
		return refNo;
	}

	public synchronized String listMessages(int messageClass) throws Exception
	{
		switch (messageClass)
		{
			case CIncomingMessage.CLASS_ALL:
				serialDriver.send("AT+CMGL=4\r");
				break;
			case CIncomingMessage.CLASS_REC_UNREAD:
				serialDriver.send("AT+CMGL=0\r");
				break;
			case CIncomingMessage.CLASS_REC_READ:
				serialDriver.send("AT+CMGL=1\r");
				break;
			case CIncomingMessage.CLASS_STO_UNSENT:
				serialDriver.send("AT+CMGL=2\r");
				break;
			case CIncomingMessage.CLASS_STO_SENT:
				serialDriver.send("AT+CMGL=3\r");
				break;
		}
		return serialDriver.getResponse();
	}

	public synchronized boolean deleteMessage(int memIndex) throws Exception
	{
		serialDriver.send(CUtils.substituteSymbol("AT+CMGD={1}\r", "{1}", "" + memIndex));
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -