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

📄 smsservice.java

📁 应用SUN的串口通信包和AT命令
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	
		@return  One of ERR_* values.
		@see	SMSDeviceInfo
		@see	SMSService#refreshDeviceInfo()
		@see	SMSService#disconnect()
		@see	SMSService#initialize()
		@see	SMSService#setOperationMode(int)
	*/
	public int connect() {
		synchronized (_SYNC_) {
			if (getInitialized()) {
				if (getCacheDir() == null)
					return ERR_NO_CACHE;
				else {
					try {
						if (serialDriver.open()) {
							try {
								Thread.sleep(1000);
							} catch (Exception e) {
							}
							serialDriver.clearBuffer();
							serialDriver.send(SMSATCommands.AT_ECHO_OFF);
							serialDriver.getResponse();
							serialDriver.send(SMSATCommands.AT_AT);
							if (serialDriver
								.getResponse()
								.equalsIgnoreCase(SMSATCommands.AT_OK)) {
								if (getSimPin() != null) {
									serialDriver.send(
										SMSATCommands.AT_CHECK_LOGIN);
									if (serialDriver
										.getResponse()
										.indexOf(SMSATCommands.AT_READY)
										== -1) {
										if (getSimPin() == null) {
											serialDriver.close();
											setConnected(false);
											return ERR_SIM_PIN_ERROR;
										} else {
											serialDriver.send(
												SMSUtils.substituteSymbol(
													SMSATCommands.AT_LOGIN,
													"{1}",
													getSimPin()));
											if (serialDriver
												.getResponse()
												.indexOf(SMSATCommands.AT_OK)
												== -1) {
												serialDriver.close();
												setConnected(false);
												return ERR_SIM_PIN_ERROR;
											} else {
												// Pin OK - wait 20 seconds for the GSM device to boot up...
												try {
													Thread.sleep(10000);
												} catch (Exception e) {
												}
												serialDriver.send(
													SMSATCommands.AT_AT);
												serialDriver.getResponse();
												try {
													Thread.sleep(10000);
												} catch (Exception e) {
												}
												serialDriver.send(
													SMSATCommands.AT_AT);
												serialDriver.getResponse();
											}
										}
									}
								}

								serialDriver.send(SMSATCommands.AT_PDU_MODE);
								if (serialDriver
									.getResponse()
									.equalsIgnoreCase(SMSATCommands.AT_OK)) {
									supportedModes |= MODE_PDU;

									setOperationMode(MODE_PDU);

									serialDriver.send(
										SMSATCommands.AT_ENABLE_INDICATIONS);
									if (serialDriver
										.getResponse()
										.equalsIgnoreCase(
											SMSATCommands.AT_OK)) {

										setConnected(true);
										return ERR_OK;
									} else {
										serialDriver.close();
										setConnected(false);

									}

								} else {

									serialDriver.close();
									setConnected(false);

								}

							}
						}
						return (getConnected() ? ERR_OK : ERR_NOT_CONNECTED);
					} catch (Exception e) {
						serialDriver.close();
						return ERR_NOT_CONNECTED;
					}
				}
			} else
				return ERR_NOT_INITIALIZED;
		}
	}

	/**
		Disconnects to the GSM device. Closes the serial port. 
	
		@return  ERR_OK value.
		@see	SMSService#connect()
	*/
	public int disconnect() {
		synchronized (_SYNC_) {
			try {
				serialDriver.close();
			} catch (Exception e) {
			}
			setConnected(false);
			return ERR_OK;
		}
	}


	

	/**
		Refreshes the GSM device specific information. This method is called once during
		connection. Its up to the developer to call it periodically in order to get the latest
		information.
	
		@return  One of ERR_* values.
		@see	SMSDeviceInfo
		@see	SMSService#connect()
		@see	SMSService#getDeviceInfo()
	*/
	public int refreshDeviceInfo() {
		synchronized (_SYNC_) {
			if (getConnected())
				try {
					deviceInfo.manufacturer = getManufacturer();
					deviceInfo.model = getModel();
					deviceInfo.serialNo = getSerialNo();
					deviceInfo.imsi = getImsi();
					deviceInfo.swVersion = getSwVersion();
					deviceInfo.batteryLevel = getBatteryLevel();
					deviceInfo.signalLevel = getSignalLevel();
					return ERR_OK;
				} catch (Exception e) {
					e.printStackTrace();
					setConnected(false);
					return ERR_NOT_CONNECTED;
				} else
				return ERR_NOT_CONNECTED;
		}
	}

	/**
		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 IncomingMessage, 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>
		<strong>IMPORTANT NOTE:</strong> This version of jSMSEngine will read
		and process only received messages, and not stored messages - regardless
		of the class you requested.
	
		@param	messageList	a LinkedList object which will be loaded with the messages.
		@param	messageClass	one of the CLASS_* values defined in IncomingMessage class which
				define what type of messages are to be read.
		@return  One of ERR_* values.
		@see	IncomingMessage
		@see	SMSService#deleteMessage(IncomingMessage)
		@see	SMSService#deleteMessage(int)
	*/
	public int readMessages(LinkedList messageList, int messageClass) {
		int i, j, memIndex;
		String response, line, sms, temp, originator, text, pdu;
		String day, month, year, hour, min, sec;
		BufferedReader reader;
		Date sentDate;
		Calendar cal = Calendar.getInstance();

		synchronized (_SYNC_) {
			if (getConnected()) {
				switch (operationMode) {
					case MODE_ASCII :
						try {
							serialDriver.send(SMSATCommands.AT_CMD_MODE);
							messageList.clear();
							serialDriver.send(SMSATCommands.AT_ASCII_MODE);
							response = serialDriver.getResponse();
							serialDriver.send(SMSATCommands.AT_CHARSET_HEX);
							response = serialDriver.getResponse();
							switch (messageClass) {
								case IncomingMessage.CLASS_ALL :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"\"ALL\""));
									break;
								case IncomingMessage.CLASS_REC_UNREAD :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"\"REC UNREAD\""));
									break;
								case IncomingMessage.CLASS_REC_READ :
									return ERR_NOT_SUPPORTED;
								case IncomingMessage.CLASS_STO_UNSENT :
									return ERR_NOT_SUPPORTED;
								case IncomingMessage.CLASS_STO_SENT :
									return ERR_NOT_SUPPORTED;
							}
							response = serialDriver.getResponse();

							sms = "";
							reader =
								new BufferedReader(new StringReader(response));
							line = reader.readLine();
							while ((line != null)
								&& (!line.equalsIgnoreCase("OK"))) {
								if (line.indexOf("+CMGL") == 0)
									sms = line;
								else if (
									(line.indexOf("+CMGL") == -1)
										&& (line.length() > 0))
									sms = sms + "~" + line;
								else
									sms = sms + "~" + "00";

								if ((sms.indexOf("\"REC READ\"") >= 0)
									|| (sms.indexOf("\"REC UNREAD\"") >= 0)) {
									if (sms.indexOf("~") != -1) {
										i = 6;
										temp = "";
										while (sms.charAt(i) != ',') {
											temp += sms.charAt(i);
											i++;
										}
										memIndex =
											Integer.parseInt(temp.trim());
										i = sms.indexOf('"') + 1;
										i = sms.indexOf('"', i) + 1;
										i = sms.indexOf('"', i) + 1;
										temp = "";
										while (sms.charAt(i) != '"') {
											temp += sms.charAt(i);
											i++;
										}
										originator = temp;

										i++;
										i = sms.indexOf('"', i) + 1;
										day = sms.substring(i + 6, i + 6 + 2);
										month = sms.substring(i + 3, i + 3 + 2);
										year = sms.substring(i, i + 2);
										year = "20" + year;
										hour = sms.substring(i + 9, i + 9 + 2);
										min = sms.substring(i + 12, i + 12 + 2);
										sec = sms.substring(i + 15, i + 15 + 2);
										cal.set(
											Integer.parseInt(year),
											Integer.parseInt(month) - 1,
											Integer.parseInt(day),
											Integer.parseInt(hour),
											Integer.parseInt(min),
											Integer.parseInt(sec));
										sentDate = cal.getTime();

										i = sms.indexOf('~') + 1;
										text = sms.substring(i);
										if (text.equalsIgnoreCase("00"))
											messageList.add(
												new IncomingMessage(
													sentDate,
													originator,
													"",
													memIndex));
										else
											messageList.add(
												new IncomingMessage(
													sentDate,
													originator,
													SMSGSMAlphabets.hex2Text(
														text,
														SMSGSMAlphabets
															.GSM7BITDEFAULT),
													memIndex));
										deviceInfo.getStatistics().incTotalIn();
										sms = "";
									}
								} else if (sms.indexOf("\"STO SENT\"") >= 0);
								else if (sms.indexOf("\"STO UNSENT\"") >= 0);
								line = reader.readLine();
							}
							reader.close();
							return ERR_OK;
						} catch (Exception e) {
							e.printStackTrace();
							return ERR_GENERIC_ERROR;
						}
					case MODE_PDU :
						try {
							serialDriver.send(SMSATCommands.AT_CMD_MODE);
							messageList.clear();
							serialDriver.send(SMSATCommands.AT_PDU_MODE);
							response = serialDriver.getResponse();
							switch (messageClass) {
								case IncomingMessage.CLASS_ALL :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"4"));
									break;
								case IncomingMessage.CLASS_REC_UNREAD :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"0"));
									break;
								case IncomingMessage.CLASS_REC_READ :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"1"));
									break;
								case IncomingMessage.CLASS_STO_UNSENT :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"2"));
									break;
								case IncomingMessage.CLASS_STO_SENT :
									serialDriver.send(
										SMSUtils.substituteSymbol(
											SMSATCommands.AT_LIST,
											"{1}",
											"3"));
									break;
							}
							response = serialDriver.getResponse();
							reader =
								new BufferedReader(new StringReader(response));
							line = reader.readLine().trim();
							while ((line != null)
								&& (line.length() > 0)
								&& (!line.equalsIgnoreCase("OK"))
								&& (!line.equalsIgnoreCase("ERROR"))) {
								i = line.indexOf(':');
								j = line.indexOf(',');
								memIndex =
									Integer.parseInt(
										line.substring(i + 1, j).trim());
								pdu = reader.readLine();
								if (isIncomingMessage(pdu)) {
									messageList.add(
										new IncomingMessage(pdu, memIndex));
									deviceInfo.getStatistics().incTotalIn();
								} else if (isStatusReportMessage(pdu)) {
									messageList.add(
										new StatusReportMessage(pdu, memIndex));
									deviceInfo.getStatistics().incTotalIn();
								}
								line = reader.readLine().trim();
							}
							reader.close();
							return ERR_OK;
						} catch (Exception e) {
							e.printStackTrace();
							return ERR_GENERIC_ERROR;
						}
					default :
						return ERR_GENERIC_ERROR;
				}
			} else
				return ERR_NOT_CONNECTED;
		}
	}

	/**
		Send an SMS message from the GSM device. Once connected, you can create a
		OutgoingMessage 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 OutgoingMessage
				object with a nickname, instead of the actual phone number.</li>

⌨️ 快捷键说明

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