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

📄 atcommand.java

📁 使用java实现的计算机自主发短信到手机。 运行界面上
💻 JAVA
字号:
package keti2;

import java.io.*;
import javax.comm.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer;

/**
 * Dial a phone using the Java Communications Package.
 *
 */
public class ATcommand
    extends CommPortModem {
  protected static String number;
  protected String text;
  Boolean HasNewsms;
  public ATcommand() throws IOException, NoSuchPortException,
      PortInUseException,
      UnsupportedCommOperationException {
    super(null);
  }

  public void sendChineseSMS(String dialno, boolean notype, String smstext) throws
      java.rmi.RemoteException {

    //----- message will be restricted to 160 characters
    if (smstext.length() > 160) {
      smstext = smstext.substring(0, 160);
      //SMS.showText("\nWarning: SMS shorten to 160 characters\n");
    } // if

    //SMS.showText("Dialing Number: " + dialno + "\n");
    //SMS.showText("Message:        " + smstext + "\n\n");

    //----- build PDU

    String pdu2 = getPDU("", dialno, 3, smstext);

    //----- send
    try {
      WritePort("AT+CMGF=0");
      WritePort("\r\n"); // set message format to PDU mode
      //ReadPort();
      WritePort("AT+CMGS=" + pdu2.length() / 2); // set length of PDU
      //ReadPort();
      WritePort("\r\n");
      WritePort("00");
      WritePort("\r"); // prepare for the PDU
      WritePort(pdu2);
      WritePort("\r"); // set PDU
      WritePort("\u001A"); // set Ctrl-Z = indicates end of PDU
      //ReadPort();

    } // try
    catch (Exception e) {
      //SMS.showText("Error: send SMS failed: " + e);
    } // catch
    // os.close();
  } // sendSMS

  public String ReadSMS(int i) throws IOException {

    WritePort("AT+CMGF=1");
    WritePort("\r\n");
    WritePort("AT+CMGR=" + i);
    WritePort("\r\n");
    text = read();
    is.close();
    return text;
  } //end readsms

  public void DelSMS(int j) throws IOException {

    WritePort("AT+CMGD=" + j);
    WritePort("\r\n");
    read();
    is.close();

  } //end readsms

  protected void converse() throws IOException {
    String resp; // the modem response.
// Send the reset command
  }

  /*public Boolean HasNewSms()throws IOException
   {
     WritePort("AT+CMGF=1");
     WritePort("\r\n");
     WritePort("AT+CMGL=\"REC UNREAD\"");
     WritePort("\r\n");
     text=ReadPort();
     if(text.startsWith("+CMGL:"))
     {
       return HasNewsms.TRUE;

     }
     else
     {
       return HasNewsms.FALSE;

     }
   }*/
  public String ReadNew() throws IOException {
    WritePort("AT+CMGF=0");
    WritePort("\r\n");
    readshort();
    WritePort("AT+CMGL=0");
    WritePort("\r\n");
    text = ReadPort();
    WritePort("read success");
    is.close();
    return text;
  }

  public static String getPDU(String smscNumber, String dialNo,
                              int messageEncoding, String text) {
    String pdu;
    String str1, str2, str3;
    int i, high, low;
    char c;

    pdu = "";
    if ( (smscNumber != null) && (smscNumber.length() != 0)) {
      str1 = "91" + toBCDFormat(smscNumber.substring(1));
      str2 = Integer.toHexString(str1.length() / 2);
      if (str2.length() != 2) str2 = "0" + str2;
      pdu = pdu + str2 + str1;
    }
    else if ( (smscNumber != null) && (smscNumber.length() == 0))

//      pdu = pdu + "00";
      pdu = pdu;

    pdu = pdu + "11";
    pdu = pdu + "00";
    str1 = dialNo;
    if (str1.charAt(0) == '+') {
      str1 = toBCDFormat(str1.substring(1));
      str2 = Integer.toHexString(dialNo.length() - 1);
      str1 = "91" + str1;
    }
    else {
      str1 = toBCDFormat(str1);
      str2 = Integer.toHexString(dialNo.length());
      str1 = "81" + str1;
    }
    if (str2.length() != 2) str2 = "0" + str2;

    pdu = pdu + str2 + str1;
    pdu = pdu + "00";
    switch (messageEncoding) {
      case 1:
        pdu = pdu + "00";
        break;
      case 2:
        pdu = pdu + "04";
        break;
      case 3:
        pdu = pdu + "08";
        break;
    }
    pdu = pdu + "AA";
    switch (messageEncoding) {
      case 1:
        str1 = Integer.toHexString(text.length());
        if (str1.length() != 2) str1 = "0" + str1;
//        str2 = textToPDU(text);
        pdu = pdu + str1 + str2;
        break;
      case 2:
        str1 = text;
        str2 = "";
        for (i = 0; i < str1.length(); i++) {
          c = str1.charAt(i);
          str2 = str2 +
              ( (Integer.toHexString( (int) c).length() < 2) ?
               "0" + Integer.toHexString( (int) c) :
               Integer.toHexString( (int) c));
        }
        str1 = Integer.toHexString(text.length());
        if (str1.length() != 2) str1 = "0" + str1;
        pdu = pdu + str1 + str2;
        break;
      case 3:
        str1 = text;
        str2 = "";
        for (i = 0; i < str1.length(); i++) {
          c = str1.charAt(i);
          high = (int) (c / 256);
          low = c % 256;
          str2 = str2 +
              ( (Integer.toHexString(high).length() < 2) ?
               "0" + Integer.toHexString(high) : Integer.toHexString(high));
          str2 = str2 +
              ( (Integer.toHexString(low).length() < 2) ?
               "0" + Integer.toHexString(low) : Integer.toHexString(low));
        }
        str1 = Integer.toHexString(text.length() * 2);
        if (str1.length() != 2) str1 = "0" + str1;
        pdu = pdu + str1 + str2;
        break;
    }
    return pdu.toUpperCase();
  }

  public static String toBCDFormat(String s) {
    String bcd;
    int i;

    if ( (s.length() % 2) != 0) s = s + "F";
    bcd = "";
    for (i = 0; i < s.length(); i += 2) bcd = bcd + s.charAt(i + 1) +
        s.charAt(i);
    return bcd;
  }

}

⌨️ 快捷键说明

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