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

📄 cservice.java

📁 gsm modem 发送短信 闪信 WAP PUSH开发包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	public void setSimPin(String simPin) { this.simPin = simPin; }

	public void setMessageHandler(CSmsMessageListener messageHandler) { this.messageHandler = messageHandler; }

	/**
		Returns the SIM pin number.

		@return  the SIM pin number.
	*/
	public String getSimPin() { return simPin; }

	/**
		Sets the reception mode.
		There are two reception modes, the synchronous and the asynchronous.
		In synchronous mode, you should call readMessages() function on demand,
		where you want to check for new messages. In asynchronous mode, the engine
		automatically calls the received() method (which you <strong>should</strong>
		override) for every received message.
		<br>By default, the reception mode is the synchronous one.

		@param	receiveMode	the reception mode (one of values RECEIVE_MODE_ASYNC, RECEIVE_MODE_SYNC).
		@see	CService#getReceiveMode()
	*/
	public void setReceiveMode(int receiveMode) { this.receiveMode = receiveMode; }

	/**
		Returns the reception mode.

		@return	the reception mode (one of values RECEIVE_MODE_ASYNC, RECEIVE_MODE_SYNC).
		@see	CService#setReceiveMode(int)
	*/
	public int getReceiveMode() { return receiveMode; }

	/**
		Loads the phonebook. The phonebook is an XML file containing associations of name
		and phone numbers.
		<br><br>
		<strong>The phonebook is optional.</strong> 

		@param	phoneBookFile	The XML full-path name which keeps the phonebook.
		@see	CPhoneBook
		@see	CService#sendMessage(COutgoingMessage)
		@see	CService#sendMessage(LinkedList)
	*/
	public void setPhoneBook(String phoneBookFile) throws Exception
	{
		phoneBook.load(phoneBookFile);
	}

	protected CSmsMessageListener getMessageHandler() { return messageHandler; }

	/**
		Connects to the GSM device. Opens the serial port, and sends the appropriate
		AT commands to initialize the operation mode of the GSM device. Retrieves
		information about the GSM device.
		Notes:
		<br>
		<ul>
			<li>The GSM device specific information (read by the call to refreshDeviceInfo()
					function is called once from this method. Since some information changes
					with time (such as battery or signal level), its your responsibility to
					call refreshDeviceInfo() periodically in order to have the latest information.
					Otherwise, you will get the information snapshot taken at the time
					of the initial connection.
			</li>
		</ul>

		@throws  AlreadyConnectedException
		@throws NoPinException
		@throws InvalidPinException
		@throws NoPduSupportException
		@throws CannotDisableIndicationsException
		@see	CDeviceInfo
		@see	CService#refreshDeviceInfo()
		@see	CService#disconnect()
	*/
	public void connect() throws Exception
	{
		if (getConnected()) throw new AlreadyConnectedException();
		else
			try
			{
				serialDriver.open();
				atHandler.reset();
				atHandler.sync();
				atHandler.echoOff();
				if (atHandler.isAlive())
				{
					if (atHandler.waitingForPin())
					{
						if (getSimPin() == null) throw new NoPinException();
						else if (!atHandler.enterPin(getSimPin())) throw new InvalidPinException();
					}
					atHandler.init();
					atHandler.echoOff();
					atHandler.setVerboseErrors();
					if (!atHandler.setPduMode()) throw new NoPduSupportException();
					if (!atHandler.disableIndications()) throw new CannotDisableIndicationsException();
					connected = true;
					refreshDeviceInfo();

					receiveThread = new CReceiveThread();
					receiveThread.start();
					keepAliveThread = new CKeepAliveThread();
					keepAliveThread.start();
				}
				else throw new NotConnectedException("GSM device is not responding.");
			}
			catch (Exception e)
			{
				disconnect();
				throw e;
			}
	}

	/**
		Disconnects to the GSM device. Closes the serial port. 

		@see	CService#connect()
	*/
	public void disconnect()
	{
		if (receiveThread != null)
		{
			receiveThread.killMe();
			receiveThread.interrupt();
			while (!receiveThread.killed()) try { receiveThread.join(); } catch (Exception e) {}
			receiveThread = null;
		}
		if (keepAliveThread != null)
		{
			keepAliveThread.killMe();
			keepAliveThread.interrupt();
			while (!keepAliveThread.killed()) try { keepAliveThread.join(); } catch (Exception e) {}
			keepAliveThread = null;
		}
		try { serialDriver.close(); } catch (Exception e) {}
		connected = false;
	}

	/**
		Reads SMS from the GSM device's memory. You should call this method when
		you want to read messages from the device. In the MessageList object you
		pass, the method will add objects of type CIncomingMessage, as many of them
		as the messages pending to be read. The class defines which types of messages
		should be read.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>The method <strong>does not delete</strong> the messages it reads
				from the GSM device. It's your responsibility to delete them, if you
				don't want them. Otherwise, on the next call of this function you
				will read the same messages.</li>
		</ul>

		@param	messageList	a LinkedList object which will be loaded with the messages.
		@param	messageClass	one of the CLASS_* values defined in CIncomingMessage class which
				define what type of messages are to be read.
		@throws NotConnectedException
		@see	CIncomingMessage
		@see	CService#deleteMessage(CIncomingMessage)
		@see	CService#deleteMessage(int)
	*/
	public void readMessages(LinkedList messageList, int messageClass) throws Exception
	{
		int i, j, memIndex;
		String response, line, sms, temp, originator, text, pdu;
		BufferedReader reader;
		CIncomingMessage mpMsg = null;

		if (getConnected())
		{
			atHandler.switchToCmdMode();
			response = atHandler.listMessages(messageClass);
			response = response.replaceAll("\\s+OK\\s+", "\nOK");
			reader = new BufferedReader(new StringReader(response));
			line = reader.readLine().trim();
			line = reader.readLine().trim();
			while ((line != null) && (line.length() > 0) && (!line.equalsIgnoreCase("OK")))
			{
				i = line.indexOf(':');
				j = line.indexOf(',');
				memIndex = Integer.parseInt(line.substring(i + 1, j).trim());
				pdu = reader.readLine();
				if (isIncomingMessage(pdu))
				{
					CIncomingMessage msg;

					msg = new CIncomingMessage(pdu, memIndex);
					if (msg.getMpRefNo() == 0)
					{
						messageList.add(msg);
						deviceInfo.getStatistics().incTotalIn();
					}
					else
					{
						if (msg.getMpSeqNo() == 1)
						{
							if (mpMsg == null)
							{
								mpMsg = new CIncomingMessage(pdu, memIndex);
								mpMsg.setMpMemIndex(memIndex);
							}
						}
						else
							if ((msg.getMpRefNo() == mpMsg.getMpRefNo()) && (msg.getMpSeqNo() == mpMsg.getMpSeqNo() + 1))
							{
								mpMsg.addText(msg.getText());
								mpMsg.setMpSeqNo(msg.getMpSeqNo());
								mpMsg.setMpMemIndex(memIndex);
								if (mpMsg.getMpSeqNo() == mpMsg.getMpMaxNo())
								{
									mpMsg.setMemIndex(-1);
									messageList.add(mpMsg);
								}
							}
					}
				}
				else if (isStatusReportMessage(pdu))
				{
					messageList.add(new CStatusReportMessage(pdu, memIndex));
					deviceInfo.getStatistics().incTotalIn();
				}
				line = reader.readLine().trim();
			}
			reader.close();
		}
		else throw new NotConnectedException();
	}

	/**
		Send an SMS message from the GSM device. Once connected, you can create a
		COutgoingMessage object with the message you want to send, and pass it
		to this function.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>If you have set a phonebook, you can create the COutgoingMessage
				object with a nickname, instead of the actual phone number.</li>
		</ul>

		@param	message	a COutgoingMessage containing the message you wish to send.
		@return  true if sending succeded.
		@see	COutgoingMessage
		@see	CPhoneBook
		@see	CService#sendMessage(LinkedList)
		@see	CService#setPhoneBook(String)
	*/
	public boolean sendMessage(COutgoingMessage message) throws Exception
	{
		LinkedList messageList;
		COutgoingMessage msg;
		boolean result;

		messageList = new LinkedList();
		messageList.add(message);
		sendMessage(messageList);
		return (message.getDispatchDate() != null);
	}


	/**
		Send an series of SMS messages from the GSM device. This method is used
		when you want to send more than one message as a batch. If your GSM device
		support the feature of keeping the GSM link open during message dispatch,
		this method should work faster than calling the sendMessage(COutgoingMessage)
		method many times.
		<br>
		Just create a LinkedList object, add as many COutgoingMessage objects you wish
		and call the method.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>If you have set a phonebook, you can create the COutgoingMessage
				object with a nickname, instead of the actual phone number.</li>
		</ul>

		@param	messageList	a LinkedList filled with COutgoingMessage objects.
		@throws NotConnectedException
		@see	COutgoingMessage
		@see	CPhoneBook
		@see	CService#sendMessage(COutgoingMessage)
		@see	CService#setPhoneBook(String)
	*/
	public void sendMessage(LinkedList messageList) throws Exception
	{
		LinkedList outList;
		COutgoingMessage message;
		int i, j, refNo;
		String pdu;

		if (getConnected())
		{
			if (phoneBook.isLoaded()) outList = phoneBook.expandPhoneBookEntries(messageList);
			else outList = messageList;
			atHandler.keepGsmLinkOpen();
			for (i = 0; i < outList.size(); i ++)
			{
				message = (COutgoingMessage) outList.get(i);
				pdu = message.getPDU(smscNumber);
				j = pdu.length();
				j /= 2;
				if (smscNumber == null) ;
				else if (smscNumber.length() == 0) j --;
				else
				{
					j -= ((smscNumber.length() - 1) / 2);
					j -= 2;
				}
				refNo = atHandler.sendMessage(j, pdu);
				if (refNo != -1)
				{
					message.setRefNo(refNo);
					message.setDispatchDate(new Date());
					deviceInfo.getStatistics().incTotalOut();
				}
				else message.setDispatchDate(null);
			}
		}
		else throw new NotConnectedException();
	}

	/**
		Deletes an SMS message from the GSM device memory.
		<br><br>
		<strong>Notes:</strong>
		<ul>

⌨️ 快捷键说明

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