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

📄 coutgoingmessage.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.*;

/**
	This class represents an outgoing SMS message, i.e. message created for dispatch
	from the GSM device.

	@see	CMessage
	@see	CIncomingMessage
	@see	CPhoneBook
	@see	CService#sendMessage(COutgoingMessage)
	@see	CService#sendMessage(LinkedList)
*/
public class COutgoingMessage extends CMessage implements java.io.Serializable
{
	/**
		Default constructor of the class.
	*/
	public COutgoingMessage()
	{
		super(TYPE_OUTGOING, null, null, null, null, -1);
		setDate(new Date());
	}

	/**
		Constructor of the class.

		@param	recipient	the recipients's number.
		@param	text	the actual text of the message.

		<br><br>Notes:<br>
		<ul>
			<li>Phone numbers are represented in their international or national format.</li>
			<li>If you use a phonebook, the phone number may be a string starting with the '~' character,
					representing an entry in the phonebook.</li>
			<li>By default, a created message is set to be encoded in 7bit. If you want to change that, be sure
					to operate in PDU mode, and change the encoding with setMessageEncoding() method.</li>
		</ul>
	*/
	public COutgoingMessage(String recipient, String text)
	{
		super(TYPE_OUTGOING, new Date(), null, recipient, text, -1);
		setDate(new Date());
	}

	public String getPDU(String smscNumber)
	{
		String pdu, udh;
		String str1, str2;
		int i, high, low;
		char c;

		pdu = "";
		udh = "";
		if ((smscNumber != null) && (smscNumber.length() != 0))
		{
			str1 = "91" + toBCDFormat(smscNumber.substring(1));
			str2 = Integer.toHexString(str1.length() / 2);
			if (str2.length() != 2) str2 = "0" + str2;
			pdu = pdu + str2 + str1;
		}
		else if ((smscNumber != null) && (smscNumber.length() == 0)) pdu = pdu + "00";
		if ((srcPort != -1) && (dstPort != -1))
		{
			if (statusReport) pdu = pdu + "71";
			else pdu = pdu + "51";
		}
		else
		{
			if (statusReport) pdu = pdu + "31";
			else pdu = pdu + "11";
		}
		pdu = pdu + "00";
		str1 = getRecipient();
		if( str1.charAt(0) == '+' )
		{
			str1 = toBCDFormat(str1.substring(1));
			str2 = Integer.toHexString(getRecipient().length() - 1);
			str1 = "91" + str1;
		}
		else
		{
			str1 = toBCDFormat(str1);
			str2 = Integer.toHexString(getRecipient().length());
			str1 = "81" + str1;
		}
		if (str2.length() != 2) str2 = "0" + str2;

		pdu = pdu + str2 + str1;
		pdu = pdu + "00";
		switch (getMessageEncoding())
		{
			case MESSAGE_ENCODING_7BIT:
				if (flashSms) pdu = pdu + "10";
				else pdu = pdu + "00";
				break;
			case MESSAGE_ENCODING_8BIT:
				if (flashSms) pdu = pdu + "14";
				else pdu = pdu + "04";
				break;
			case MESSAGE_ENCODING_UNICODE:
				if (flashSms) pdu = pdu + "18";
				else pdu = pdu + "08";
				break;
		}
		pdu = pdu + getValidityPeriodBits();
		if ((srcPort != -1) && (dstPort != -1))
		{
			String s;

			udh = "060504";
			s = Integer.toHexString(dstPort);
			while (s.length() < 4) s = "0" + s;
			udh += s;
			s = Integer.toHexString(srcPort);
			while (s.length() < 4) s = "0" + s;
			udh += s;
		}

		switch (getMessageEncoding())
		{
			case MESSAGE_ENCODING_7BIT:
				str2 = textToPDU(getText());
				i = CGSMAlphabet.stringToBytes(getText(), new byte[400]);
				if ((srcPort != -1) && (dstPort != -1)) str1 = Integer.toHexString(i + 8);
				else str1 = Integer.toHexString(i);
				if (str1.length() != 2) str1 = "0" + str1;
				if ((srcPort != -1) && (dstPort != -1)) pdu = pdu + str1 + udh + str2;
				else pdu = pdu + str1 + str2;
				break;
			case MESSAGE_ENCODING_8BIT:
				str1 = getText();
				str2 = "";
				for (i = 0; i < str1.length(); i ++)
				{
					c = str1.charAt(i);
					str2 = str2 + ((Integer.toHexString((int) c).length() < 2) ? "0" + Integer.toHexString((int) c) : Integer.toHexString((int) c));  
				}
				if ((srcPort != -1) && (dstPort != -1)) str1 = Integer.toHexString(getText().length() + 7);
				else str1 = Integer.toHexString(getText().length());
				if (str1.length() != 2) str1 = "0" + str1;
				if ((srcPort != -1) && (dstPort != -1)) pdu = pdu + str1 + udh + str2;
				else pdu = pdu + str1 + str2;
				break;
			case MESSAGE_ENCODING_UNICODE:
				str1 = getText();
				str2 = "";
				for (i = 0; i < str1.length(); i ++)
				{
					c = str1.charAt(i);
					high = (int) (c / 256);
					low = c % 256;
					str2 = str2 + ((Integer.toHexString(high).length() < 2) ? "0" + Integer.toHexString(high) : Integer.toHexString(high));
					str2 = str2 + ((Integer.toHexString(low).length() < 2) ? "0" + Integer.toHexString(low) : Integer.toHexString(low));
				}
				if ((srcPort != -1) && (dstPort != -1)) str1 = Integer.toHexString((getText().length() * 2) + 7);
				else str1 = Integer.toHexString(getText().length() * 2);
				if (str1.length() != 2) str1 = "0" + str1;
				if ((srcPort != -1) && (dstPort != -1)) pdu = pdu + str1 + udh + str2;
				else pdu = pdu + str1 + str2;
				break;
		}
		return pdu.toUpperCase();
	}

	private String getValidityPeriodBits()
	{
		String bits;
		int value;

		if (validityPeriod == -1) bits = "FF";
		else
		{
			if (validityPeriod <= 12) value = (validityPeriod * 12) - 1;
			else if (validityPeriod <= 24) value = (((validityPeriod - 12) * 2) + 143);
			else if (validityPeriod <= 720) value = (validityPeriod / 24) + 166;
			else value = (validityPeriod / 168) + 192;
			bits = Integer.toHexString(value);
			if (bits.length() != 2) bits = "0" + bits;
			if (bits.length() > 2) bits = "FF";
		}
		return bits;
	}

	private String textToPDU(String text)
	{
		String pdu, str1;
		byte[] bytes, oldBytes, newBytes;
		BitSet bitSet;
		int i, j, k, value1, value2;

		bytes = new byte[400];
		i = CGSMAlphabet.stringToBytes(text, bytes);
		oldBytes = new byte[i];
		for (j = 0; j < i; j ++) oldBytes[j] = bytes[j];
		bitSet = new BitSet(oldBytes.length * 8);

		value1 = 0;
		for (i = 0; i < oldBytes.length; i ++)
			for (j = 0; j < 7; j ++)
			{
				value1 = (i * 7) + j;
				if ((oldBytes[i] & (1 << j)) != 0) bitSet.set(value1);
			}
		value1 ++;

		if (((value1 / 56) * 56) != value1) value2 = (value1 / 8) + 1;
		else value2 = (value1 / 8);
		if (value2 == 0) value2 = 1;

		newBytes = new byte[value2];
		for (i = 0; i < value2; i ++)
			for (j = 0; j < 8; j ++)
				if ((value1 + 1) > ((i * 8) + j))
					if (bitSet.get(i * 8 + j)) newBytes[i] |= (byte) (1 << j);

		pdu = "";
		for (i = 0; i < value2; i ++)
		{
			str1 = Integer.toHexString((int) newBytes[i]);
			if (str1.length() != 2) str1 = "0" + str1;
			str1 = str1.substring(str1.length() - 2, str1.length());
			pdu += str1;
		}
		return pdu;
	}

	private String toBCDFormat(String s)
	{
		String bcd;
		int i;

		if ((s.length() % 2) != 0) s = s + "F";
		bcd = "";
		for (i = 0; i < s.length(); i += 2) bcd = bcd + s.charAt(i + 1) + s.charAt(i);
		return bcd; 
	}
}

⌨️ 快捷键说明

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