📄 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;
}
/**
* 发送短消息函数
* @param Number 目的电话号码
* @param Sms 发送短消息的内容
* @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"; //串口发送
// String Msg="AT+SMSP=285\r"; //设置编码格式为unicode
// out.write(Msg.getBytes()); //发送命令
// Msg="AT*SkT*MOREQ=0,"+TelNum+",,4098,";
// out.write(Msg.getBytes()); //发送命令
// byte[] b=SMS.getBytes("utf-16BE");
// out.write(b);
// out.write("\r".getBytes());//发送标志
String Msg="AT+SMSG=1\r"; //设置为串口发送模式
out.write(Msg.getBytes());
Thread.sleep(200); //等待0.2秒,缓冲串口发送模式设置完毕操作
byte chdot=0x2c; //逗号
byte[] head={0x07,0x03}; //短信数据区起始标志:0x07,数据区分为3段:0x03
byte[] dest_len={0x00,0x01,0x0B}; //目标电话号码段起始标志:0x00 0x01,电话号码长度0x0B
byte[] dest=TelNum.getBytes(); //目标电话号码
byte[] callback_len={0x00,0x02,0x10};//回叫电话号码起始标志:0x00 0x01,电话号码长度0x10
byte[] callback=TelNum.getBytes(); //回叫电话号码
byte[] data_head={0x00,0x08,(byte)(SMS.length()*2)}; //短信内容起始标志0x00 0x08,短信内容长度为字节数组长度
byte[] data=SMS.getBytes("utf-16BE"); //短信内容,采用utf-16be字符集
/**按照anydata公司的扩展短消息发送协议
* 逐字节按协议格式写入串口
*/
out.write(head);out.write(chdot); //短信数据区起始段
out.write(dest_len);out.write(dest); out.write(chdot); //目标电话号码段
out.write(callback_len);out.write(callback); out.write(chdot); //回叫电话号码段
out.write(data_head);out.write(data); //短信内容段
Thread.sleep(20); //发送完毕暂停20毫秒,等待dtss800数据缓冲
ptr=true; //置发送成功标志
}catch(Exception e)
{System.out.println("send sms err"+e);
ptr=false; //置发送失败标志
}
return ptr;
}
/**
* 读取短消息数据处理函数,
*
* @param sms_byte 从串口取出的字节数组,内包含按短消息扩展协议组织的数据格式
* @return 返回1个长度为4的字符串数组
*/
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++;
}
}
/**
* 读取源电话号码函数
* 必须在ReadSmsByte()运行后调用
* @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;
}
/**
* 读取短消息发送时间函数
* 必须在ReadSmsByte()运行后调用
* @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;
}
/**
* 读取短消息采用的字符集类型函数
* 必须在ReadSmsByte()运行后调用
* @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;
}
/**
* 读取短消息内容函数
*
* @param buffer 从串口取出的字节数组,内包含按短消息扩展协议组织的数据格式
* @param len 从串口取出的字节数组的长度
* @return 包含短消息内容的字节数组
*/
public byte[] getSmsData(byte[] buffer,int len)
{
int k=0;
byte[] sc=ts[4].getBytes(); //将第3数据段转换为字节数组
byte[] temp=new byte[(int)sc[2]]; //根据短信数据长度创建temp数组,存放短信数据,sc[2]为数据有几个字节
String result="";
int flag=0;
for(int j=0;j<len;j++)
{
if(buffer[j]==0x00) //寻找短信内容段,段前表示为0x00,0x08
if(buffer[j+1]==0x08)
{flag=j;break;}
}
System.arraycopy(buffer,flag+3,temp,0,sc[2]);
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -