Send SMS to an android Phone with ADB

edited November 2013 in Share Your Work

Hi, a pretty simple solution to send SMS messages from a processing application via adb.

1 - Place adb server application in /data

2 - to send a message, follow this code (before import java.io.*):

 try {
        Runtime.getRuntime().exec(dataPath("")+"/adb shell input keyevent 3"); //send home button event (you can use keyevent 26 to emulate power button)
      }
      catch(Exception e) {
        println(e);
      }

      try {
        String command = dataPath("")+"/adb shell am start -a android.intent.action.SENDTO -d sms:"+num+" --es sms_body \""+texte+"\" --ez exit_on_sent true";
        Runtime.getRuntime().exec(command);
      }
      catch(Exception e) {
        println(e);
      }
      delay(1000);
      try {
        Runtime.getRuntime().exec(dataPath("")+"/adb shell input keyevent 22");
      }
      catch(Exception e) {
        println(e);
      }
      try {
        Runtime.getRuntime().exec(dataPath("")+"/adb shell input keyevent 66");
      }
      catch(Exception e) {
        println(e);
      } 
Tagged:

Comments

  • edited November 2013

    For example a simple sms application. Edit : you need adb root access to read database in your phone

    main.pde

        // Need G4P library
    import g4p_controls.*;
    import java.io.*;
    import java.io.IOException;
    
    
    BufferedReader reader;
    String line;
    String returnedValues;
    String page="";
    String [] liste_noms =new String[1000];
    String [] liste_tel =new String[1000];
    String [] new_sms_nom =new String[1000];
    String [] new_sms =new String[1000];
    int i=0, nb_max_mess=0;
    public void setup() {
      size(700, 320);
      try {
    
        Process p = Runtime.getRuntime().exec(new String[] { 
          "bash", "-c", dataPath ( "" ) +"/adb -d shell 'sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db \"SELECT name, number FROM view_v1_phones ORDER BY name \"'"
        }
        );
    
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
        // read the output from the command
        while ( (returnedValues = stdInput.readLine ()) != null) {    
          String[] list_decoup = split(returnedValues, "|");      
          liste_noms [i] = list_decoup[0];
          liste_tel [i] = list_decoup[1];
          i++;
        }
      }
      catch(Exception e) {
        println(e);
      }
      try {
        Process sms = Runtime.getRuntime().exec(new String[] { 
          "bash", "-c", dataPath ( "" ) +"/adb -d shell 'sqlite3 /data/data/com.android.providers.telephony/databases/mmssms.db \"SELECT address, body FROM sms WHERE read=0\"'"
        }
        );
    
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(sms.getInputStream()));
    
        // read the output from the command
        while ( (returnedValues = stdInput.readLine ()) != null) { 
          println(returnedValues+"   "+i); 
          nb_max_mess=i; 
          String[] list_decoup = split(returnedValues, "|");      
          new_sms_nom [i] = list_decoup[0];
          new_sms [i] = list_decoup[1];
    
          i++;
        }   
        println(nb_max_mess);
      }
      catch(Exception e) {
        println(e);
      }
    
    
      createGUI();
      customGUI(); //./adb -d shell 'sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db "SELECT name, number FROM view_v1_phones"'
      for (int i=0;i<=nb_max_mess;i++) {
        if (new_sms [i]!=null) {
          page  +=new String(new_sms_nom[i]+" : "+new_sms [i] +"\r"+ "\n"+"\n");
        }
      }
    }
    
    public void draw() {
      background(230);
      fill(244, 0, 0);
      affiche_sms();
      //println(message.getText());
    }
    
    // Use this method to add additional statements
    // to customise the GUI controls
    public void customGUI() {
    }
    void envoi_sms(String num, String texte) {
    
    
    
      try {
        Runtime.getRuntime().exec(dataPath("")+"/adb shell input keyevent 3"); //send home button event (you can use keyevent 26 to emulate power button)
      }
      catch(Exception e) {
        println(e);
      }
    
      try {
        String command = dataPath("")+"/adb shell am start -a android.intent.action.SENDTO -d sms:"+num+" --es sms_body \""+texte+"\" --ez exit_on_sent true"; // enter in SMS app, and prepare the sms
        Runtime.getRuntime().exec(command);
      }
      catch(Exception e) {
        println(e);
      }
      delay(1000);
      try {
        Runtime.getRuntime().exec(dataPath("")+"/adb shell input keyevent 22"); // tab button
      }
      catch(Exception e) {
        println(e);
      }
      try {
        Runtime.getRuntime().exec(dataPath("")+"/adb shell input keyevent 66"); //enter button
      }
      catch(Exception e) {
        println(e);
      }
    }
    
    void affiche_sms() { // display SMS
      try {
        Process sms = Runtime.getRuntime().exec(new String[] { 
          "bash", "-c", dataPath ( "" ) +"/adb -d shell 'sqlite3 /data/data/com.android.providers.telephony/databases/mmssms.db \"SELECT address, body FROM sms WHERE read=0\"'"
        } // search in database SMS
        );
    
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(sms.getInputStream()));
    
        // read the output from the command
        while ( (returnedValues = stdInput.readLine ()) != null) { 
          println(returnedValues+"   "+i); 
          nb_max_mess=i; 
          String[] list_decoup = split(returnedValues, "|");      
          new_sms_nom [i] = list_decoup[0];
          new_sms [i] = list_decoup[1];
    
          i++;
        }   
        println(nb_max_mess);
      }
      catch(Exception e) {
        println(e);
      }
      for (int i=0;i<=nb_max_mess;i++) {
        if (new_sms [i]!=null) {
          page  +=new String(new_sms_nom[i]+" : "+new_sms [i] +"\r"+ "\n"+"\n");
        }
      }
      text(page, 350, 50, 300, 200);
      page="";
      i=0;
    }
    

    gui.pde

    /* =========================================================
     * ====                   WARNING                        ===
     * =========================================================
     * The code in this tab has been generated from the GUI form
     * designer and care should be taken when editing this file.
     * Only add/edit code inside the event handlers i.e. only
     * use lines between the matching comment tags. e.g.
    
     void myBtnEvents(GButton button) { //_CODE_:button1:12356:
         // It is safe to enter your event code here  
     } //_CODE_:button1:12356:
    
     * Do not rename this tab!
     * =========================================================
     */
    
    public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:205084:
      println(message.getText());
      println(numero.getText());
      println(dropList1.getSelectedText());
      String num=numero.getText();
      String texte=message.getText();
      num="+33"+num.substring(1, 10);
      envoi_sms(num, texte);
    } 
    public void dropList1_click1(GDropList source, GEvent event) { //_CODE_:dropList1:382391:
    // println("dropList1 - GDropList event occured " + System.currentTimeMillis()%10000000 );
      numero.setText(liste_tel [dropList1.getSelectedIndex()]);
    }
    
    // Create all the GUI controls. 
    // autogenerated do not edit
    public void createGUI(){
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if(frame != null)
        frame.setTitle("envoyer SMS");
      button1 = new GButton(this, 10, 283, 80, 30);
      button1.setText("Envoyer");
      button1.addEventHandler(this, "button1_click1");
      numero = new GTextField(this, 10, 9, 327, 23, G4P.SCROLLBARS_NONE);
      numero.setOpaque(true);
      numero.addEventHandler(this, "numero_change1");
      message = new GTextArea(this, 9, 40, 327, 228, G4P.SCROLLBARS_NONE);
      message.setOpaque(true);
      message.addEventHandler(this, "message_1");
      dropList1 = new GDropList(this, 354, 10, 220, 220, 10);
      dropList1.setItems(liste_noms, 0);
      dropList1.addEventHandler(this, "dropList1_click1");
    }
    
    // Variable declarations 
    // autogenerated do not edit
    GButton button1; 
    GTextField numero; 
    GTextArea message; 
    GDropList dropList1; 
    
Sign In or Register to comment.