?? sms.java
字號:
package GEOSMS;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
/*
* Created on 2005-9-10
*
*/
/**
* @author Mick Chen
* wuhan university
* TODO
* Get the information of the sms,include the source tel number ,
* the time of receive sms and the charset of the sms,
* then you can send a sms to another number that you want it
*/
public class SMS {
private InputStream input;
private OutputStream out;
private String[] ts=new String[5];
public SMS(InputStream sinput,OutputStream soutput)
{
input=sinput;
out=soutput;
}
/**
* 發(fā)送短消息函數(shù)
* @param Number 目的電話號碼
* @param Sms 發(fā)送短消息的內(nèi)容
* @return
*/
public boolean SendSms(String Number,String Sms)
{
boolean ptr=false;
try{
String TelNum=Number.trim()+" "; //"13437287921"+5個空格;
//String SMS=dm.lookingfor(RecSms);
String SMS=Sms;
//String SMS=RecSms;
//String Msg="AT+CDV13437287921\r";//撥號
// String Msg="AT+SMSG=1\r"; //串口發(fā)送
// String Msg="AT+SMSP=285\r"; //設(shè)置編碼格式為unicode
// out.write(Msg.getBytes()); //發(fā)送命令
// Msg="AT*SkT*MOREQ=0,"+TelNum+",,4098,";
// out.write(Msg.getBytes()); //發(fā)送命令
// byte[] b=SMS.getBytes("utf-16BE");
// out.write(b);
// out.write("\r".getBytes());//發(fā)送標(biāo)志
String Msg="AT+SMSG=1\r"; //設(shè)置為串口發(fā)送模式
out.write(Msg.getBytes());
Thread.sleep(200); //等待0.2秒,緩沖串口發(fā)送模式設(shè)置完畢操作
byte chdot=0x2c; //逗號
byte[] head={0x07,0x03}; //短信數(shù)據(jù)區(qū)起始標(biāo)志:0x07,數(shù)據(jù)區(qū)分為3段:0x03
byte[] dest_len={0x00,0x01,0x0B}; //目標(biāo)電話號碼段起始標(biāo)志:0x00 0x01,電話號碼長度0x0B
byte[] dest=TelNum.getBytes(); //目標(biāo)電話號碼
byte[] callback_len={0x00,0x02,0x10};//回叫電話號碼起始標(biāo)志:0x00 0x01,電話號碼長度0x10
byte[] callback=TelNum.getBytes(); //回叫電話號碼
byte[] data_head={0x00,0x08,(byte)(SMS.length()*2)}; //短信內(nèi)容起始標(biāo)志0x00 0x08,短信內(nèi)容長度為字節(jié)數(shù)組長度
byte[] data=SMS.getBytes("utf-16BE"); //短信內(nèi)容,采用utf-16be字符集
/**按照anydata公司的擴(kuò)展短消息發(fā)送協(xié)議
* 逐字節(jié)按協(xié)議格式寫入串口
*/
out.write(head);out.write(chdot); //短信數(shù)據(jù)區(qū)起始段
out.write(dest_len);out.write(dest); out.write(chdot); //目標(biāo)電話號碼段
out.write(callback_len);out.write(callback); out.write(chdot); //回叫電話號碼段
out.write(data_head);out.write(data); //短信內(nèi)容段
Thread.sleep(20); //發(fā)送完畢暫停20毫秒,等待dtss800數(shù)據(jù)緩沖
ptr=true; //置發(fā)送成功標(biāo)志
}catch(Exception e)
{System.out.println("send sms err"+e);
ptr=false; //置發(fā)送失敗標(biāo)志
}
return ptr;
}
/**
* 讀取短消息數(shù)據(jù)處理函數(shù),
*
* @param sms_byte 從串口取出的字節(jié)數(shù)組,內(nèi)包含按短消息擴(kuò)展協(xié)議組織的數(shù)據(jù)格式
* @return 返回1個長度為4的字符串?dāng)?shù)組
*/
public void ReadSmsByte(byte[] sms_byte)
{
String str=new String(sms_byte);
StringTokenizer st=new StringTokenizer(str,",");
int ptr_ts=0;
while(st.hasMoreTokens())
{
String s=st.nextToken();
ts[ptr_ts]=s;
ptr_ts++;
}
}
/**
* 讀取源電話號碼函數(shù)
* 必須在ReadSmsByte()運(yùn)行后調(diào)用
* @return 返回電話號碼字符串
*/
public String getTelNumber()
{
String s="";
if(ts[1]!=null)
{
char[] number=ts[1].toCharArray();
for(int j=3;j<(int)number[2]+3;j++)
{
s=s+number[j];
}
}
return s;
}
/**
* 讀取短消息發(fā)送時間函數(shù)
* 必須在ReadSmsByte()運(yùn)行后調(diào)用
* @return 返回時間字符串
*/
public String getSmsTime()
{
String s="";
if(ts[2]!=null)
{
char[] number=ts[2].toCharArray();
for(int j=3;j<(int)number[2]+3;j++)
{
s=s+number[j];
}
}
return s;
}
/**
* 讀取短消息采用的字符集類型函數(shù)
* 必須在ReadSmsByte()運(yùn)行后調(diào)用
* @return 返回字符集類型
*/
public String getCharSet()
{
String s="";
char temp=0xff;
if(ts[3]!=null)
{
char[] charset=ts[3].toCharArray();
temp=charset[4];
}
switch(temp)
{
case '0':{s="OCTEC";break;}
case '1':{s="Extended Protocol Message";break;}
case '2':{s="ASCII";break;}
case '3':{s="IA5";break;}
case '4':{s="UNICODE";break;}
default:s="UNKONW CHARSET";
}
return s;
}
/**
* 讀取短消息內(nèi)容函數(shù)
*
* @param buffer 從串口取出的字節(jié)數(shù)組,內(nèi)包含按短消息擴(kuò)展協(xié)議組織的數(shù)據(jù)格式
* @param len 從串口取出的字節(jié)數(shù)組的長度
* @return 包含短消息內(nèi)容的字節(jié)數(shù)組
*/
public byte[] getSmsData(byte[] buffer,int len)
{
int k=0;
byte[] sc=ts[4].getBytes(); //將第3數(shù)據(jù)段轉(zhuǎn)換為字節(jié)數(shù)組
byte[] temp=new byte[(int)sc[2]]; //根據(jù)短信數(shù)據(jù)長度創(chuàng)建temp數(shù)組,存放短信數(shù)據(jù),sc[2]為數(shù)據(jù)有幾個字節(jié)
String result="";
int flag=0;
for(int j=0;j<len;j++)
{
if(buffer[j]==0x00) //尋找短信內(nèi)容段,段前表示為0x00,0x08
if(buffer[j+1]==0x08)
{flag=j;break;}
}
System.arraycopy(buffer,flag+3,temp,0,sc[2]);
return temp;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -