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

📄 cathandler.java

📁 gsm modem 发送短信 闪信 WAP PUSH开发包
💻 JAVA
字号:
//	jSMSEngine API.
//	An open-source API package for sending and receiving SMS via a GSM device.
//	Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
//		Web Site: http://www.jsmsengine.org
//
//	jSMSEngine is distributed under the GPL license.
//
//	This program is free software; you can redistribute it and/or
//	modify it under the terms of the GNU General Public License
//	version 2 as published by the Free Software Foundation
//
//	This program 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 General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//

package org.jsmsengine;

import java.util.logging.*;

/**
	This class contains all the AT commands which are used during communication
	of the API with the GSM device.<br><br>
	This is the generic handler. It should be compatible with all devices. However, "should" is
	a big word... Some GSM modems may need slightly different AT commands to work. In this
	case, subclassing of CATHandler() is required.<br><br>
	<strong>Note for those creating subclassed versions of CATHandler for their own modems:</strong><br>
	All methods that are marked as "<strong>***Critical method***</strong>" should be implemented, 
	otherwise the jSMSEngine API will not work. The Non-Critical may be left unimplemented - they should 
	not affect the normal jSMSEngine API operation.
*/
public class CATHandler
{
	protected CSerialDriver serialDriver;
	protected Logger log;

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

	/**
		Sends a couple of "AT" commands in order to sync with modem.<br>
		Useful with auto-baud-detecting GSM devices.<br>
		<strong>***Critical method***</strong>
	*/
	public synchronized void sync() throws Exception
	{
		serialDriver.send("AT\r");
		serialDriver.clearBuffer();
		serialDriver.send("AT\r");
		serialDriver.clearBuffer();
		serialDriver.send("AT\r");
		serialDriver.clearBuffer();
	}

	/**
		This command should soft-reset the modem.<br>
		Soft-reset is not available for all GSM devices, and the operation is not 
		critical.<br>
		<strong>***Non-Critical method***</strong>
	*/
	public synchronized void reset() throws Exception
	{
	}

	/**
		Sets Echo off.<br>
		<strong>***Critical method***</strong>
	*/
	public synchronized void echoOff() throws Exception
	{
		serialDriver.send("ATE0\r");
		serialDriver.clearBuffer();
	}

	/**
		Other initialization commands.<br>
		This is left empty, but you might need to add specific code for your specific modem.<br>
		<strong>***Non-Critical method***</strong>
	*/
	public synchronized void init() throws Exception
	{
	}

	/**
		Sends an "AT" command and waits for response.<br>
		<strong>***Critical method***</strong>

		@return  True if GSM device responded with "OK".
	*/
	public synchronized boolean isAlive() throws Exception
	{
		serialDriver.send("AT\r");
		return (serialDriver.getResponse().matches("\\s*[\\p{ASCII}]*\\s+OK\\s"));
	}

	/**
		Requests status regarding PIN.<br>
		<strong>***Critical method***</strong>

		@return  True if GSM device is waiting for PIN to be entered.
	*/
	public synchronized boolean waitingForPin() throws Exception
	{
		serialDriver.send("AT+CPIN?\r");
		return (serialDriver.getResponse().indexOf("SIM PIN") >= 0);
	}

	/**
		Enters PIN.<br>
		<strong>***Critical method***</strong>

		@return  True if GSM device unlocked. False if PIN is wrong.
	*/
	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)
		{
			try { Thread.sleep(10000); } catch (Exception e) {} 
			return true;
		}
		else return false;
	}

	/**
		Sets verbose mode for error reporting.<br>
		<strong>***Non-Critical method***</strong>

		@return  True if GSM device accepted the option.
	*/
	public synchronized boolean setVerboseErrors() throws Exception
	{
		serialDriver.send("AT+CMEE=1\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	/**
		Sets PDU operation.<br>
		<strong>***Critical method***</strong>

		@return  True if GSM device supports PDU operation.
	*/
	public synchronized boolean setPduMode() throws Exception
	{
		serialDriver.send("AT+CMGF=0\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	/**
		Disable GSM device originated delivery notifications.<br>
		<strong>***Critical method***</strong>

		@return  True if GSM device disabled notifications.
	*/
	public synchronized boolean  disableIndications() throws Exception
	{
		serialDriver.send("AT+CNMI=0,0,0,0\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	/**
		Returns the Manufacturer string.<br>
		<strong>***Non-Critical method***</strong>

		@return  The Manufacturer string.
	*/
	public synchronized String getManufacturer() throws Exception
	{
		serialDriver.send("AT+CGMI\r");
		return serialDriver.getResponse();
	}

	/**
		Returns the Model string.<br>
		<strong>***Non-Critical method***</strong>

		@return  The Model string.
	*/
	public synchronized String getModel() throws Exception
	{
		serialDriver.send("AT+CGMM\r");
		return serialDriver.getResponse();
	}

	/**
		Returns the GSM Device serial number.<br>
		<strong>***Non-Critical method***</strong>

		@return  The GSM Device serial number.
	*/
	public synchronized String getSerialNo() throws Exception
	{
		serialDriver.send("AT+CGSN\r");
		return serialDriver.getResponse();
	}

	/**
		Returns the IMSI (International Mobile Subscriber Identity).<br>
		<strong>***Non-Critical method***</strong>

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

	/**
		Returns the GSM device software version.<br>
		<strong>***Non-Critical method***</strong>

		@return  The software version.
	*/
	public synchronized String getSwVersion() throws Exception
	{
		serialDriver.send("AT+CGMR\r");
		return serialDriver.getResponse();
	}

	/**
		Returns the battery level.<br>
		<strong>***Non-Critical method***</strong>

		@return  The battery level.
	*/
	public synchronized String getBatteryLevel() throws Exception
	{
		serialDriver.send("AT+CBC\r");
		return serialDriver.getResponse();
	}

	/**
		Returns the signal level.<br>
		<strong>***Non-Critical method***</strong>

		@return  The signal level.
	*/
	public synchronized String getSignalLevel() throws Exception
	{
		serialDriver.send("AT+CSQ\r");
		return serialDriver.getResponse();
	}

	/**
		Sets the preferred message storage to Memory.<br>
		<strong>***Non-Critical method***</strong>

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

	/**
		Sets the preferred message storage to SIM.<br>
		<strong>***Non-Critical method***</strong>

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

	/**
		Switches GSM device to command mode.<br>
		<strong>***Critical method***</strong>
	*/
	public synchronized void switchToCmdMode() throws Exception
	{
		serialDriver.send("+++");
	}

	/**
		Keeps the GSM link open for sending multiple messages in less time.<br>
		Not critical. If this option is supported, message dispatch may be a little 
		faster.<br>
		<strong>***Non-Critical method***</strong>

		@return  True if operated succeded.
	*/
	public synchronized boolean keepGsmLinkOpen() throws Exception
	{
		serialDriver.send("AT+CMMS=1\r");
		return (serialDriver.getResponse().matches("\\s+OK\\s+"));
	}

	/**
		Sends a message.<br>
		In case of errors, it rerties up to three times to complete the operation 
		before it returns failure.<br>
		<strong>***Critical method***</strong>

		@return  The SMSC Reference Number on success, -1 on error.
	*/
	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 == 4) throw new NoResponseException();
				log.log(Level.INFO, "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 == 4)  throw new NoResponseException();
				log.log(Level.INFO, "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("CMS ERROR:") >= 0)
			{
				errorRetries ++;
				if (errorRetries == 4)
				{
					log.log(Level.SEVERE, "GSM CMS Errors: Quit retrying, message lost...");
					refNo = -1;
					break;
				}
				else log.log(Level.SEVERE, "GSM CMS Errors: Possible collision, retrying...");
			}
			else refNo = -1;
		}
		return refNo;
	}

	/**
		Returns the list of messages.<br>
		<strong>***Critical method***</strong>

		@return  True if operated succeded.
	*/
	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();
	}

	/**
		Deletes a message from GSM device memory.<br>
		<strong>***Critical method***</strong>

		@return  True if operated succeded.
	*/
	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 + -