package mobile;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class Sender {
private String msgCenterPhone;
private String serialPort;
private int baud;
private int parity;
private int stop;
private int data;
private int gsmType;
/** AT+CMGS= */
public final static byte[] SMS_STRUCTOR = {
0x41, 0x54, 0x2B, 0x43, 0x4D, 0x47, 0x53, 0x3D};
/** AT+CMGF=0 */
public final static byte[] SMS_PDU_HEADER = {
0x41, 0x54, 0x2B, 0x43, 0x4D, 0x47, 0x46, 0x3D, 0x30};
public final static byte[] SMS_HEADER_START = {
0x41, 0x54, 0x2B, 0x43, 0x4D, 0x47, 0x53, 0x3D};
public final static byte[] SMS_HEADER_END = {
0x0D};
public final static byte[] SMS_CONTENT_END = {
0x1A};
public Sender() {
msgCenterPhone = "8613800100500";
msgCenterPhone = msgCenterPhone.trim();
serialPort = "COM1";
baud = 9600;
parity = 0;
stop = 1;
data = 8;
gsmType = 2;
}
/**
* 确认发送格式并发送短信,将长短信分为N条正常短信
* @param mobileSendAlarmInfo
* @return 1:发送成功;-1:发送失败
*/
public int sendSMS(String destPhone, String content){
/**
* 发送成功标志
*/
int succeedFlag;
CommPortIdentifier cpi = null;
SerialPort sp = null;
DataOutputStream dos = null;
DataInputStream dis = null;
try {
cpi = CommPortIdentifier.getPortIdentifier("COM1");
sp = (SerialPort) cpi.open("iMessenger", 1000);
sp.setSerialPortParams(baud, data, stop, parity);
dos = new DataOutputStream(sp.getOutputStream());
dis = new DataInputStream(sp.getInputStream());
int[] tpduLength = new int[1];
String pack = packStrMessage(destPhone, content, msgCenterPhone,
tpduLength);
dos.write(SMS_PDU_HEADER);
//System.out.println( new String( SMS_PDU_HEADER ) );
dos.write(SMS_HEADER_END);
dos.flush();
int timeout = 0;
while (true) {
Thread.sleep(100);
byte[] b = new byte[21];
int length = dis.read(b);
if (timeout > 50) {
System.out.println("Time Out");
return succeedFlag = -1;
}
if (length == -1) {
timeout++;
continue;
}
String s = new String(b);
if (s.indexOf("OK") != -1) { //'OK'
System.out.println("Set PDU Successful!");
break;
}
}
//write the header
dos.write(SMS_HEADER_START);
String strLength = (tpduLength[0] < 100 ?
("0" + Integer.toString(tpduLength[0])) :
Integer.toString(tpduLength[0]));
dos.write(strLength.getBytes());
dos.write(SMS_HEADER_END);
dos.flush();
Thread.sleep(100);
timeout = 0;
while (true) {
Thread.sleep(100);
byte[] b = new byte[21];
int length = dis.read(b);
if (timeout > 50) {
System.out.println("Time Out");
return succeedFlag = -1;
}
if (length == -1) {
timeout++;
continue;
}
String s = new String(b);
if (s.indexOf('>') != -1) { //'>'
System.out.println("Title Successful!");
break;
}
if (s.indexOf("ERROR") != -1) { //'R'
System.out.println("Error");
return succeedFlag = -1;
}
}
dos.write(pack.getBytes());
dos.write(SMS_CONTENT_END);
dos.write(SMS_HEADER_END);
dos.flush();
timeout = 0;
while (true) {
Thread.sleep(100);
byte[] b = new byte[21];
int length = dis.read(b);
if (timeout > 50) {
System.out.println("Time Out");
return succeedFlag = -1;
}
if (length == -1) {
timeout++;
continue;
}
String s = new String(b);
System.out.println(s);
//System.exit(0);
if (s.indexOf("OK") != -1) { //'>'
System.out.println("Content Successful!");
return succeedFlag = 1;
}
if (s.indexOf("ERROR") != -1) { //'R'
System.out.println("Error");
return succeedFlag = -1;
}
}
//Thread.sleep(5000);
//return 1;
} catch (IOException ioe) {
System.out.println("IO Error!");
System.out.println(ioe.getMessage());
return succeedFlag = -1;
// throw new SendException("IO Error!");
} catch (NoSuchPortException nspe) {
System.out.println("No Such Port!");
System.out.println(nspe.getMessage());
return succeedFlag = -1;
// throw new SendException("No Such Port!");
} catch (PortInUseException piue) {
sp.close();
System.out.println("Port Now In Use!");
System.out.println(piue.getMessage());
return succeedFlag = -1;
// throw new SendException("Port Now In Use!");
} catch (UnsupportedCommOperationException ucoe) {
System.out.println("Unsupport Operation!");
System.out.println(ucoe.getMessage());
return succeedFlag = -1;
// throw new SendException("Unsupport Operation!");
} catch (InterruptedException ie) {
System.out.println("Thread Interrupted Error!");
System.out.println(ie.getMessage());
return succeedFlag = -1;
// throw new SendException("Thread Interrupted Error!");
} finally {
try {
if (sp != null)
sp.close();
if (dos != null)
dos.close();
if (dis != null)
dis.close();
} catch (IOException ex) {
System.out.println("Close Error!");
System.out.println(ex.getMessage());
return succeedFlag = -1;
// throw new SendException("Close Error!");
}
}
// return succeedFlag;
}
private String packStrMessage(String destPhone, String content,
String centerPhone, int[] tpduLength) {
if (destPhone == null || content == null || centerPhone == null)
return "";
//encode the SCA
byte[] sca = new byte[3];
sca[0] = (byte) (0x00);
sca[1] = (byte) 0x11;
sca[2] = (byte) 0x00;
//encode TPDU
if (!destPhone.startsWith("86")) //add china code 86 to dest phone. 81 为日本的代码
destPhone = "86" + destPhone;
- 1
- 2
前往页