Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import com.cinterion.io.ATCommand;
  2. import com.cinterion.io.ATCommandFailedException;
  3. import com.cinterion.io.ATCommandListener;
  4. import com.cinterion.io.ATCommandResponseListener;
  5.  
  6. /**
  7.  * MIDlet demonstrating the usage of the ATCommand class
  8.  */
  9.  
  10. public class Main extends MIDlet {
  11.  
  12.     private ATCommand m_Cmd;
  13.     private Listener m_Listener = new Listener();
  14.  
  15.     /**
  16.      * Implementation of ATCommandListener to receive asynchronous events
  17.      */
  18.     private class Listener implements ATCommandListener {
  19.         public void ATEvent(String Event) {
  20.             System.out.println("ATEvent " + Event);
  21.         }
  22.  
  23.         public void RINGChanged(boolean SignalState) {
  24.             System.out.println("RINGChanged " + SignalState);
  25.         }
  26.  
  27.         public void DCDChanged(boolean SignalState) {
  28.             System.out.println("DCDChanged " + SignalState);
  29.         }
  30.  
  31.         public void DSRChanged(boolean SignalState) {
  32.             System.out.println("DSRChanged " + SignalState);
  33.         }
  34.  
  35.         public void CONNChanged(boolean SignalState) {
  36.             System.out.println("CONNChanged " + SignalState);
  37.         }
  38.     }
  39.  
  40.     /**
  41.      * Implementation of ATCommandResponseListener to receive asynchronous
  42.      * responses
  43.      */
  44.     private class RspListener implements ATCommandResponseListener {
  45.         public void ATResponse(String Response) {
  46.             System.out.println("Received asynchronous response:");
  47.             System.out.println(Response);
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Default constructor
  53.      */
  54.     public Main() {
  55.         System.out.println("Constructor");
  56.         try {
  57.             m_Cmd = new ATCommand(false);
  58.             m_Cmd.addListener(m_Listener);
  59.         } catch (Exception e) {
  60.             System.out.println(e);
  61.         }
  62.     }
  63.  
  64.     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  65.         System.out.println("SmsReceiver stopped");
  66.         notifyDestroyed();
  67.     }
  68.  
  69.     protected void pauseApp() {
  70.         // TODO Auto-generated method stub
  71.  
  72.     }
  73.  
  74.     protected void startApp() throws MIDletStateChangeException {
  75.         System.out.println("Main started");
  76.  
  77.         try {
  78.  
  79.             // check the registration
  80.             boolean registered = false;
  81.  
  82.             while (registered == false) {
  83.  
  84.                 String registration_response = m_Cmd.send("AT+CREG?\r");
  85.  
  86.                 int locally_registered = registration_response.indexOf(",1");
  87.                 int roaming_registered = registration_response.indexOf(",5");
  88.  
  89.                 if ((locally_registered > -1) || (roaming_registered > -1)) {
  90.                     registered = true;
  91.                     System.out.println("Module registered to the network");
  92.                 } else {
  93.                     System.out.println("Module not registered to the network");
  94.                 }
  95.  
  96.                 Thread.sleep(2000);
  97.             }
  98.  
  99.             sendSMS();
  100.  
  101.         } catch (Exception e) {
  102.             e.printStackTrace();
  103.         }
  104.  
  105.         destroyApp(true);
  106.     }
  107.  
  108.     protected void sendSMS() {
  109.         try {
  110.             String Response = m_Cmd.send("AT+CMGF = 1\r");
  111.             System.out.println(Response);
  112.             Response = m_Cmd.send("AT+CMGS=\"+614********\"\r"); // here you put
  113.                                                                 // your nummber
  114.             System.out.println(Response);
  115.             Response = m_Cmd.send("Message \r this is my message from Developer."
  116.                     + (char) 26); // here you put text that is inside the SMS
  117.             System.out.println(Response);
  118.             String ctrlZ = (char) 26 + "";
  119.             m_Cmd.send(ctrlZ, new RspListener()); // escapse character, that
  120.                                                     // marks end of SMS content
  121.         } catch (ATCommandFailedException ex) {
  122.             ex.printStackTrace();
  123.         } catch (IllegalStateException ex) {
  124.             ex.printStackTrace();
  125.         } catch (IllegalArgumentException ex) {
  126.             ex.printStackTrace();
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement