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

📄 smsmsgincoming.java

📁 Java写的通过手机收发短信息的程序。
💻 JAVA
字号:
//// Copyright (c) 2001 Kvanttisofta oy.  All rights reserved.////// SMS PDU message class for incoming messages.// (aspa@users.sourceforge.net).//// $Id: SmsMsgIncoming.java,v 1.1.1.1 2001/04/18 04:19:00 aspa Exp $.//// TODO:// - support other data encoding schemes than GSM 7 bit//package fi.kvanttisofta.sms;public class SmsMsgIncoming {    public final int SMS_MSG_ENCODING_7BIT = 0;    private int    smscAddressLength;    private int    smscAddressType;    private String smscAddress;    private int    smsDeliverCode;    private int    senderAddressLength;    private int    senderAddressType;    private String senderAddress;    private int    tpPid;      // protocol identifier.    private int    tpDcs;      // data coding scheme.    private String tpScts;     // time stamp.    private int    tpUdl;      // message length.    private int    encMsgLen;    private String tpUd;       // user message (as sent).    private String msg;        // user message (decoded).    public String getSenderNumber() {	return senderAddress;    }    public String getMessage() {	return msg;    }    public SmsMsgIncoming(String smspdu) throws PduParseException {	int pdulen = smspdu.length();	String tmpstr;	int i = 0;	try {	    boolean hasSmscInfo = false;	    String octet1str = smspdu.substring(i, 2);	    int octet1 = Integer.parseInt(octet1str, 16);	    i=i+2;	    String octet2str = smspdu.substring(i, i+2);	    int octet2 = Integer.parseInt(octet2str, 16);	    i=i+2;	    // simple heuristic for deducing whether the PDU contains	    // SMSC info (if second octet is a SMSC type-of-address	    // field it always has bit 7 set).	    if (octet2 >= 127)		hasSmscInfo = true;	    if( hasSmscInfo ) {		// SMSC info included in the PDU.				// get SMSC address length.		smscAddressLength = octet1;				// get SMSC address type.		smscAddressType = octet2;		// get SMSC address string (minus type len).		int smsAddrLastIndex = i + (smscAddressLength*2)-2;		tmpstr = smspdu.substring(i, smsAddrLastIndex);		smscAddress = SmsPduCodec.swapDigits(tmpstr);		if( smscAddress.indexOf('F') != -1 ) // strip trailing F.		    smscAddress = smscAddress.substring(0,						smscAddress.length()-1);		if((smscAddressType & 0xf0) == 0x90) // international format.		    smscAddress = '+' + smscAddress;		i=i+(smscAddressLength*2)-2;		String smsDeliverStr = smspdu.substring(i, i+2);		smsDeliverCode = Integer.parseInt(smsDeliverStr, 16);		i=i+2;	    		// get sender address length.		String addressLenStr = smspdu.substring(i, i+2);		senderAddressLength = Integer.parseInt(addressLenStr, 16);		i=i+2;	    } else {		// no SMSC info in the PDU.		smsDeliverCode = octet1;		senderAddressLength = octet2;			    }	    // decode GSM SMS-DELIVER PDU message.	    if(smsDeliverCode != 0x04)		throw new PduParseException("unknown SMS-DELIVER code " +					    smsDeliverCode);	    // get sender address type.	    String addressTypeStr = smspdu.substring(i, i+2);	    senderAddressType = Integer.parseInt(addressTypeStr, 16);	    i=i+2;	    // get sender address.	    int senderLastIndex = i+senderAddressLength+senderAddressLength%2;	    tmpstr = smspdu.substring(i, senderLastIndex);	    senderAddress = SmsPduCodec.swapDigits(tmpstr);	    senderAddress = senderAddress.substring(0, senderAddressLength);	    if( (senderAddressType & 0xf0) == 0x90 ) // 1001xxxx?		senderAddress = '+' + senderAddress;	    i=i+senderAddressLength + senderAddressLength%2;	    	    // get protocol id.	    String protocolStr = smspdu.substring(i, i+2);	    tpPid = Integer.parseInt(protocolStr, 16);	    if(tpPid != 0x00)		throw new PduParseException("unknown protocol ID " + tpPid);	    i=i+2;	    // get data encoding scheme.	    String dataEncStr = smspdu.substring(i, i+2);	    tpDcs = Integer.parseInt(dataEncStr, 16);	    if(tpDcs != 0x00)		throw new PduParseException("unknown data encoding scheme "+					    tpDcs);	    i=i+2;	    // get timestamp.	    tmpstr = smspdu.substring(i, i+7*2);	    tpScts = SmsPduCodec.swapDigits(tmpstr);	    i=i+7*2;	    // get message length.	    String msgLenStr = smspdu.substring(i, i+2);	    tpUdl = Integer.parseInt(msgLenStr, 16);	    i=i+2;	    // calculate encoded message length.	    if(tpDcs == 0x00) {		encMsgLen = (tpUdl*7) / 8;		if( ((tpUdl*7) % 8) != 0 )		    encMsgLen++;	    } else		encMsgLen = tpUdl;	    // get message string.	    tpUd = smspdu.substring(i, i + encMsgLen*2);	    	    msg = SmsPduCodec.sevenBitDecode(tpUd, tpUdl);	} catch (IndexOutOfBoundsException e) {	    throw new PduParseException("SMS PDU too short: " + e);	} catch (NumberFormatException e) {	    throw new PduParseException("Hexadecimal number expected: " + e);	}	if( (i + tpUd.length()) < pdulen )	    throw new PduParseException("PDU too long");    }    public String toString() {	StringBuffer sb = new StringBuffer(200);	sb.append("\n");	sb.append("\t smscAddressLength   = 0x" +		  SmsPduCodec.toHexString(smscAddressLength));	sb.append("\n\t smscAddressType     = 0x" +		  SmsPduCodec.toHexString(smscAddressType));	sb.append("\n\t smscAddress         = " + smscAddress);	sb.append("\n\t smsDeliverCode      = 0x" +		  SmsPduCodec.toHexString(smsDeliverCode));	sb.append("\n\t senderAddressLength = 0x" +		  SmsPduCodec.toHexString(senderAddressLength));	sb.append("\n\t senderAddressType   = 0x" +		  SmsPduCodec.toHexString(senderAddressType));	sb.append("\n\t senderAddress       = " + senderAddress);	sb.append("\n\t tpPid               = 0x" +		  SmsPduCodec.toHexString(tpPid));	sb.append("\n\t tpDcs               = 0x" +		  SmsPduCodec.toHexString(tpDcs));	sb.append("\n\t tpScts              = " + tpScts);	sb.append("\n\t tpUdl               = 0x" +		  SmsPduCodec.toHexString(tpUdl));	sb.append("\n\t tpUd                = '" + tpUd + "'");	sb.append("\n\t msg                 = '" + msg + "'");	sb.append("\n\t msg hex             = " + SmsPduCodec.hexDump(msg));	return new String(sb);    }    public static void main(String[] args) {	String smspdu1 =	    "07917238010010F5040BC87238880900F100009930925161958003C16010";	//String smspdu2 = "040C91534850251863000000214061738580665079991EC603DC65BA0E344DBBEB6C7619F47683EAF5791A240FC3DF723A3D0D3A2DD37275D80D4F93DF6E103D3C2F9F5C206A9A1D0E83CC613C28F60AC7626B761A9E77C1C96690BBDE2ECBDF6FB70E04CBE16AB0582E579B01";	String smspdu2 = "040C91534850251863000000214061738580665079991EC603DC65BA0E344DBBEB6C7619F47683EAF5791A240FC3DF723A3D0D3A2DD37275D80D4F93DF6E103D3C2F9F5C206A9A1D0E83CC613C28F60AC7626B761A9E77C1C96690BBDE2ECBDF6FB70E04CBE16AB0582E579B01";	String smspdu3 = "040C915348502518630000002140618383800FC6B49B0C82D160B59A0C179BD900";	//String smspdu4 = "040C91534850251863000000212161515480665079991EC603DC65BA0E344DBBEB6C7619F47683EAF5791A240FC3DF723A3D0D3A2DD37275D80D4F93DF6E103D3C2F9F5C206A9A1D0E83CC613C28F60AC7626B761A9E77C1C96690BBDE2ECBDF6FB70E04CBE16AB0582E579B01";	String smspdu4 = "040C915348700689440000002121717570801FC6301E44AFB3DFF37918E4AEB7CBF2F7DB0D82E57035582C97ABCD00";	SmsMsgIncoming sms1 = null;	try {	    sms1 = new SmsMsgIncoming(smspdu1);	} catch (PduParseException e) {	    System.err.println("error in PDU parsing: " + e);	}	System.err.println("pdu1: " + smspdu1);	System.err.println("message1: " + sms1 + "\n\n");	SmsMsgIncoming sms2 = null;        try {            sms2 = new SmsMsgIncoming(smspdu2);        } catch (PduParseException e) {            System.err.println("error in PDU parsing: " + e);        }        System.err.println("pdu2: " + smspdu2);        System.err.println("message2: " + sms2 + "\n\n");	SmsMsgIncoming sms3 = null;        try {            sms3 = new SmsMsgIncoming(smspdu3);        } catch (PduParseException e) {            System.err.println("error in PDU parsing: " + e);        }        System.err.println("pdu3: " + smspdu3);        System.err.println("message3: " + sms3 + "\n\n");	SmsMsgIncoming sms4 = null;        try {            sms4 = new SmsMsgIncoming(smspdu4);        } catch (PduParseException e) {            System.err.println("error in PDU parsing: " + e);        }        System.err.println("pdu4: " + smspdu4);        System.err.println("message4: " + sms4 + "\n\n");	System.err.println("number: '" + sms4.getSenderNumber() + "'");	System.err.println("msg: '" + sms4.getMessage() + "'");	// sms4 hexdump.	String str4 = sms4.getMessage();	System.err.println("dump: " + SmsPduCodec.hexDump(str4));    }}

⌨️ 快捷键说明

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