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

📄 msg.java

📁 手机短信编程的源代码
💻 JAVA
字号:
// Useful tools for the communication with the mobile phone//// file:    Msg.java// used by: SMS.java//// For comments see header of SMS.java.//---------------------------------------------------------------------------------------package smspack;import java.io.*;import java.rmi.*;import java.util.*;import javax.comm.*;class Msg {  private static String portName;  private static SerialPort port;  private static OutputStreamWriter out;  private static InputStreamReader in;  private static String number;  private static String message;  private static boolean numbertype;       // national or international dialing number  /** sends a SMS over the mobile phone to the GSM network   * @param dialno dialing number of the destionation   * @param notype national/international dialing number   * @param smstext SMS to send   * @throws java.rmi.RemoteException   */  public static void sendSMS(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    SMSTools smstools = new SMSTools();    byte[] pdu = SMSTools.getPDUPart(dialno, notype, smstext);    //----- send    try {      Port.writeln("AT+CMGF=0");              // set message format to PDU mode      Port.read();      Port.writeln("AT+CMGS=" + pdu.length);  // set length of PDU      Port.read();      Port.write("00");                       // prepare for the PDU      Port.write(SMSTools.toHexString(pdu));  // set PDU      Port.write("\u001A");                   // set Ctrl-Z = indicates end of PDU      Port.read();    } // try    catch (Exception e) {      SMS.showText("Error: send SMS failed: " + e);    } // catch  }  // sendSMS  /** get SMS from the ME   *  @param  index of SMS   *  @return  SMS string of the ME   *  @throws Exception   */  public static String getSMS(int index) throws Exception {    int p;    String s="";    try {      s = Port.sendAT ("AT+CMGR=" + index);    } // try    catch (Exception e) {      SMS.showText("Error: getSMS failed: " + e);    } // catch    //----- destilate the PDU from the received string of raw data    p = s.indexOf("+CMGR:");    // delete the AT command information at the beginning of the PDU    s = s.substring(p+6, s.length());    p = s.indexOf("\n");    s = s.substring(p+1, s.length());    p = s.indexOf("\r");        // delete LF / CR at the end of the PDU    s = s.substring(0, p);    return s;  } // getSMS  /** get number of stored SMS from the ME   *  @return  number of stored SMS in the ME   *  @throws Exception   */  public static int getNoOfSMS() throws Exception {    int n=-1;    int[] index;    try {      index = getIndexOfSMS();          // get a index list of stored SMS      do {                              // search all guilty index of SMS        n++;      } while (index[n] != 0);    }  // try    catch (Exception e) {      SMS.showText("Error: getNoOfSMS failed: " + e);    }  // catch    return n;  } // getNoOfSMS  /** get an index list of guilty SMS from the ME   *  @return  list of indexes of stored SMS in the ME   *  @throws Exception   */  public static int[] getIndexOfSMS() throws Exception {    int n, p;    int[] index;    index = new int[SMS.MAXNOSIMSMS];    String s = "", atrsp = "";    try {      atrsp = Port.sendAT ("AT+CMGL");    } // try    catch (Exception e) {      SMS.showText("Error: getIndexOfSMS failed: " + e);    } // catch    // get a index list from the stored SMS    // example of answer: "+CMGL: 1,0,,51", 1 is the index of the SMS    n = 0;    do {      p = atrsp.indexOf("+CMGL: ");      if (p != -1) {                             // found a new PDU        atrsp = atrsp.substring(p+7, atrsp.length());        p = atrsp.indexOf(",");        s = atrsp.substring(0, p);        index[n] = Integer.parseInt(s.trim());      } // if      else break;      n++;    } while (p != -1);    return index;  } // getIndexOfSMS  /** delete a SMS from the ME   *  @param  index of SMS   *  @throws Exception   */  public static void deleteSMS(int index) throws Exception {    String s="";    try {      s = Port.sendAT ("AT+CMGD=" + index);    } // try    catch (Exception e) {      SMS.showText("Error: deleteSMS failed: " + e);    } // catch  } // deleteSMS  /** deletes all SMS from the ME   *  @throws Exception   */  public static void deleteAllSMS() throws Exception {    int n;    int[] index;    try {      index = getIndexOfSMS();          // get a index list of stored SMS      n = -1;      do {        n++;        if (index[n] != 0) {          // found a guilty index for a SMS          SMS.showText("Delete SMS with Index: " + index[n] + "\n");          deleteSMS(index[n]);        } // if      } while (index[n] != 0);    }  // try    catch (Exception e) {      SMS.showText("Error: deleteAllSMS failed: " + e);    }  // catch  } // deleteAllSMS  /** get the signal quality from the ME   *  @return  received signal strength indication (rssi = 0 ... 31 (best), 99 not known)   *  @throws Exception   */  public static int getSignalQuality() throws Exception {    int n, p;    String s = "";    try {      s = Port.sendAT ("AT+CSQ");    } // try    catch (Exception e) {      SMS.showText("Error: getSignalQuality failed: " + e);    } // catch    // destilate the signal quality from the answer string    // example of answer: "+CSQ: 31,99"    if (s.length() > 0) {      p = s.indexOf(":");      s = s.substring(p+1, s.length());      p = s.indexOf(",");      s = s.substring(0, p);      n = Integer.parseInt(s.trim());    } // if    else n = 99;    return n;  } // getSignalQuality  /** get the battery status from the ME   *  @return  battery connection status (bcs), battery charge level (bcl)   *           bcs: 0	     powered by the battery   *                1	     battery connected, but not powered by it   *                2	     does not have a battery connected   *           bcl: 0	     battery is exhausted, or MT does not have a battery connected   *                1...100  battery has 1 ... 100 percent of capacity remaining   *  @throws Exception   */  public static String getBatteryStatus() throws Exception {    int p;    String s = "";    try {      s = Port.sendAT ("AT+CBC");    } // try    catch (Exception e) {      SMS.showText("Error: getBatteryStatus failed: " + e);    } // catch    // destilate the battery status from the answer string    // example of answer: "+CBC: 0,90"    if (s.length() > 0) {      p = s.indexOf(":");      s = s.substring(p+1, s.length());      p = s.indexOf("\r");      s = s.substring(0, p);      s = s.trim();    } // if    else s = "";    return s;  } // getBatteryStatus  /** test the connection between PC and ME   * @return  answer string of the ME   * @throws java.rmi.RemoteException   */  public static boolean test() throws java.rmi.RemoteException {    String s="";    try {      Port.writeln("AT");   // test if there is a working communication      s = Port.read();      // get response from ME    } // try    catch (Exception e) {      SMS.showText("Error: testConnection failed:" + e);    } // catch    if (s.indexOf("AT") == 0) return true;  // working connection to the mobile phone    else return false;                      // no connection to the mobile phone  }  // test} // Msg

⌨️ 快捷键说明

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