📄 smssender.java
字号:
String dialText = jTextPane1.getText().trim();
String dialNum;
int dialTextLen = getDialTextHex(dialText).length() / 2;
String dialTextLenHex = Integer.toHexString(dialTextLen);
if (dialTextLen < 15) {
dialTextLenHex = "0" + dialTextLenHex;
}
String dialNumAr[] = dialNums.split(",");
for (int i = 0; i < dialNumAr.length; i++) {
dialNum = dialNumAr[i];
if (isDialNumValid(dialNum)) {
try {
//smsout.write("AT+CMGF=0\r".getBytes());
String cmd0 = "AT+CMGS=" + (dialTextLen + 15) + "\r";
smsout.write(cmd0.getBytes());
String cmd1 = "0011000D9168" + getDialNumHex(dialNum) + "0008A7" +
dialTextLenHex + getDialTextHex(dialText) + (char) 26;
smsout.write(cmd1.getBytes());
System.out.println("正在向 " + dialNum + " 发短信...");
}
catch (Exception e1) {
JOptionPane.showMessageDialog(this, e1.getMessage(), "error", 1);
}
try {
Thread.sleep(5000);
}
catch (Exception e1) {
JOptionPane.showMessageDialog(this, e1.getMessage(), "error", 1);
}
}
else {
System.out.println("手机号 " + dialNum + " 不合法...");
}
}
System.out.println("发送完毕,谢谢使用...");
}
public String getDialNumHex(String dialNum) {
String dialNumHex = "";
int b = dialNum.length();
char c1, c2;
for (int ib = 2; ib < b; ib++) {
if (ib % 2 == 0) {
String c = dialNum.substring(ib - 2, ib);
c1 = c.charAt(0);
c2 = c.charAt(1);
dialNumHex = dialNumHex + c2 + c1;
}
}
dialNumHex = dialNumHex + 'F' + dialNum.substring(b - 1, b);
return dialNumHex;
}
public String getDialTextHex(String dialText) {
String dialTextHex = "";
int b = dialText.length();
char ch;
for (int i = 0; i < b; i++) {
ch = dialText.charAt(i);
int chUc = new Character(ch).hashCode();
String chUcHex = Integer.toHexString(chUc);
if (chUcHex.length() < 2) {
chUcHex = "000" + chUcHex;
}
if (chUcHex.length() < 3) {
chUcHex = "00" + chUcHex;
}
if (chUcHex.length() < 4) {
chUcHex = "0" + chUcHex;
}
dialTextHex = dialTextHex + chUcHex;
}
return dialTextHex;
}
public boolean isDialNumValid(String dialNum) {
if (dialNum == null || dialNum.length() != 11) {
return false;
}
String dialNumL = "13000000000";
String dialNumR = "13999999999";
int c1 = dialNum.compareTo(dialNumL);
int c2 = dialNumR.compareTo(dialNum);
if (c1 * c2 > 0) {
return true;
}
else {
return false;
}
}
public void serialEvent(SerialPortEvent e) {
StringBuffer inputBuffer = new StringBuffer();
int newData = 0;
switch (e.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
while (newData != -1) {
try {
newData = smsin.read();
if (newData == -1) {
break;
}
if ('\r' == (char) newData) {
inputBuffer.append('\n');
}
else {
inputBuffer.append( (char) newData);
}
}
catch (IOException ex) {
System.err.println(ex);
return;
}
}
String receivedStr = new String(inputBuffer);
//System.out.println(receivedStr);
String x = receivedStr.replace('\n', ' ');
receivedStr = x.trim();
System.out.println(receivedStr);
if (receivedStr.startsWith("+CMTI:")) {
curSmsIndex = receivedStr.substring(receivedStr.lastIndexOf(",") +
1, receivedStr.length());
try {
smsout.write( ("AT+CMGR=" + curSmsIndex + "\r").getBytes());
}
catch (Exception e1) {}
}
if (receivedStr.startsWith("+CMGR:")) {
int indexOfSms = receivedStr.indexOf(" ");
String str = receivedStr.substring(indexOfSms + 2, receivedStr.length());
String sTelNum = "";
String sTelTime = "";
String sTelContent = "";
String sTmp = "";
int sContentLen = 0;
int nTelNumBegin = 24;
int nTelNumEnd = 36 +
str.substring(36, str.length() - 1).indexOf("0008");
sTelNum = str.substring(nTelNumBegin, nTelNumEnd);
sTelTime = str.substring(nTelNumEnd + 4, nTelNumEnd + 16);
sContentLen = Integer.parseInt(str.substring(nTelNumEnd + 18,
nTelNumEnd + 20), 16);
sTelContent = str.substring(nTelNumEnd + 20,
nTelNumEnd + 20 + sContentLen * 2);
for (int i = 0; i < sTelNum.length(); i = i + 2) { //取得电话号码
char ch1 = sTelNum.charAt(i);
char ch2 = sTelNum.charAt(i + 1);
sTmp = sTmp + ch2 + ch1;
}
sTelNum = sTmp;
if (sTmp.charAt(sTmp.length() - 1) == 'F' ||
sTmp.charAt(sTmp.length() - 1) == 'f') {
sTelNum = sTmp.substring(0, sTmp.length() - 1);
}
sTmp = "";
for (int i = 0; i < sTelTime.length(); i = i + 2) { //取得日期时间
char ch1 = sTelTime.charAt(i);
char ch2 = sTelTime.charAt(i + 1);
sTmp = sTmp + ch2 + ch1;
switch (i) {
case 0:
case 2:
sTmp += '-';
break;
case 4:
sTmp += ' ';
break;
case 6:
case 8:
sTmp += ':';
break;
}
}
sTelTime = sTmp;
sTmp = "";
for (int i = 0; i < sTelContent.length(); i = i + 4) { //取得内容
sTmp = sTmp +
(char) Integer.parseInt(sTelContent.substring(i, i + 4), 16);
}
System.out.println("主叫号码: " + sTelNum + "\t" + "呼叫时间: 20" + sTelTime);
System.out.println("呼叫内容\n" + sTmp);
}
break;
case SerialPortEvent.BI:
System.out.println("\n--- BREAK RECEIVED ---\n");
}
}
public void ownershipChange(int type) {
if (type == CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED) {
JOptionPane.showMessageDialog(this, "Do you want to give up your port?",
"error", 1);
}
}
void jTextPane1_keyReleased(KeyEvent e) {
String txt = jTextPane1.getText();
if (txt.length() > 70) {
JOptionPane.showMessageDialog(this, "不能超过70字", "对不起", 1);
jTextPane1.setText(txt.substring(0, 70));
}
jLabel10.setText(jTextPane1.getText().length() + " 字");
}
void jTextField1_keyPressed(KeyEvent e) {
String txt = jTextField1.getText();
if (txt.length() > 0) {
char ch = txt.charAt(txt.length() - 1);
if (! (ch >= '0' && ch < '9' || ch == ',')) {
jTextField1.setText(txt.substring(0, txt.length() - 1));
}
}
}
void jTextField1_keyReleased(KeyEvent e) {
String txt = jTextField1.getText();
if (txt.length() > 0) {
char ch = txt.charAt(txt.length() - 1);
if (! (ch >= '0' && ch < '9' || ch == ',')) {
jTextField1.setText(txt.substring(0, txt.length() - 1));
}
}
}
void jButton4_actionPerformed(ActionEvent e) {
jButton4.setEnabled(false);
jButton5.setEnabled(true);
jMenuItem6.setEnabled(false);
jMenuItem7.setEnabled(true);
st = new smsThread(this);
st.start();
}
void jButton5_actionPerformed(ActionEvent e) {
st.interrupt();
st = null;
jButton4.setEnabled(true);
jButton5.setEnabled(false);
jMenuItem6.setEnabled(true);
jMenuItem7.setEnabled(false);
}
void jMenuItem1_actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
void jMenuItem2_actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
void jMenuItem3_actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
void jMenuItem4_actionPerformed(ActionEvent e) {
int smsTotal = Integer.parseInt(curSmsIndex, 10);
if (smsTotal == 0) {
smsTotal = 20;
}
for (int i = 1; i < smsTotal + 1; i++) {
String listAll = "AT+CMGR=" + i + "\r";
try {
smsout.write(listAll.getBytes());
Thread.sleep(1000);
}
catch (Exception e1) {
JOptionPane.showMessageDialog(this, e1.getMessage(), "error", 1);
}
}
}
void jMenuItem5_actionPerformed(ActionEvent e) {
int smsTotal = Integer.parseInt(curSmsIndex, 10);
if (smsTotal == 0) {
smsTotal = 20;
}
for (int i = 1; i < smsTotal + 1; i++) {
String delAll = "AT+CMGD=" + i + "\r";
try {
smsout.write(delAll.getBytes());
Thread.sleep(1000);
}
catch (Exception e1) {
JOptionPane.showMessageDialog(this, e1.getMessage(), "error", 1);
}
}
}
void jMenuItem6_actionPerformed(ActionEvent e) {
jButton4_actionPerformed(e);
}
void jMenuItem7_actionPerformed(ActionEvent e) {
jButton5_actionPerformed(e);
}
}
class SmsSender_jButton1_actionAdapter
implements java.awt.event.ActionListener {
SmsSender adaptee;
SmsSender_jButton1_actionAdapter(SmsSender adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class SmsSender_jButton2_actionAdapter
implements java.awt.event.ActionListener {
SmsSender adaptee;
SmsSender_jButton2_actionAdapter(SmsSender adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -