Envoie des sms via les commandes at

Contenu du snippet

Cette application vous permet d'envoyer des SMS via le protocole de communication pc/GSM "AT Commands".
Il vous suffit un modem GSM liè à votre pc avec un cable USB.
Allez Les amis j'attends vos commentaires .
Bon courage,

Source / Exemple :

//Classe frame.java
package essai1;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Enumeration;

import java.util.logging.Level;

import java.util.logging.Logger;



import javax.comm.CommPortIdentifier;

import javax.comm.NoSuchPortException;

import javax.comm.SerialPort;

import javax.swing.*;





public class frame extends JFrame{



	private static final long serialVersionUID = 1L;

	private JButton bouton = new JButton("Go");

	 private JButton bouton2 = new JButton("START");

	 String reponse="";

	private JPanel container = new JPanel();

    private JComboBox combo = new JComboBox();

    private JLabel label = new JLabel("les ports");

    CommPortIdentifier portId;

	 BufferedReader in;

	 PrintWriter out;

	 SerialPort serialPort;

	 



	  private static ClassePort1 port1;

	    static final private char cntrlZ=(char)26;

    

    public frame (){            	

    this.setTitle("	Choix");

    this.setSize(200, 100);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setLocationRelativeTo(null);  



    container.setBackground(Color.white);

    container.setLayout(new BorderLayout());

    combo.addActionListener(new FormeListener());

    bouton2.addActionListener(new Bouton2Listener());

    bouton.addActionListener(new BoutonListener());

    combo.setPreferredSize(new Dimension(100,20));

  listePortsDispo();

    JPanel south = new JPanel();

    south.add(bouton);

    south.add(bouton2);

    container.add(south, BorderLayout.SOUTH);   

            

    JPanel top = new JPanel();

    top.add(label);

    top.add(combo);

    

    container.add(top, BorderLayout.NORTH);

    this.setContentPane(container);

    this.setVisible(true);            

    }

    

   

	public void listePortsDispo(){

		 System.err.println("recherche..." );

		 Enumeration portList=CommPortIdentifier.getPortIdentifiers();

		 if (portList==null)

		 {

			 System.err.println("Aucun port de communication détecté" );

			 return;

		 }

		 while (portList.hasMoreElements()){

			 portId=(CommPortIdentifier)portList.nextElement();

			 combo.addItem(portId.getName());

    

			 System.out.println("* "+portId.getName());

		 } //fin while

	  } //fin de la methode listePortsDispo()



	 public class BoutonListener implements ActionListener{

	

         public void actionPerformed(ActionEvent arg0) {

        	 port1.openPort(reponse);

                 

         }

         

 }

	 class FormeListener implements ActionListener{

		 

         public void actionPerformed(ActionEvent e) {

        	 reponse=combo.getSelectedItem().toString();

          	

         }               

 } 

	public static void envoie_sms(String sms, String phn)throws IOException

	   {	

		   try

	       {          

                  port1.envoyer("AT+CMGF=1"+"\r");

	               port1.envoyer("at+cmgs=\""+phn+"\"\r");

	               port1.envoyer(sms+cntrlZ+"\r");

                   Thread.sleep(1500);



   }

   catch (Exception ex)

    {

   Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);

}



	        

	   }

	class Bouton2Listener  implements ActionListener{

		 

        /**

         * Redéfinitions de la méthode actionPerformed

         */

        public void actionPerformed(ActionEvent e) {

                

        	String[] a = {le numéro de GSM (exple:0021699000000)};

		    try

		    {

		        for(int i=0;i<a.length;i++)

		          {

		    	

		           envoie_sms("Si tu recois ce message fais moi un bip pour tester mon programme , c rahma",a[i]);

		          }

		    }

		    	catch (Exception ex)

			       {

			           Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);

			       }	 

		  

        }

        

}  

	

	  public static void main(String args[]) throws NoSuchPortException{

		port1=new ClassePort1();

		 new frame ();

		  port1.closePort();

		  

		  

	  }

	  

}

         

   

	

	


//Classe ClassePort1.java

package essai1;

import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.JOptionPane;
import java.io.*;
import com.sun.comm.Win32Driver;

public class ClassePort1 implements SerialPortEventListener{
	
	   static String TimeStamp;     

       SerialPort comPort;
       CommPortIdentifier portId;
       OutputStream os;
       static InputStream is;
       static PrintWriter sortie;
       Thread readThread;
       private static BufferedReader bufRead;
    

       public ClassePort1()
{

                Win32Driver w32Driver = new Win32Driver();
		w32Driver.initialize();
}
       

      public void openPort(String Portname)
      { 
    	  try{
    			 portId=CommPortIdentifier.getPortIdentifier(Portname);
    			 ComControl();

    	  }
    	  
    	    catch
    	      (Exception e) {
    	      TimeStamp = new java.util.Date().toString();
    	      System.out.println(TimeStamp + ": ERROR " + portId);
    	      System.out.println(TimeStamp + ": msg1 - " + e); 
    	     }
      }
      public void ComControl() {
    	  
    	  try {
              TimeStamp = new java.util.Date().toString();

            
            comPort = (SerialPort) portId.open("essai1", 10000);
            System.out.println("Le port : " + portId.getName() + " est ouvert");
            
            
    	  } catch (PortInUseException e) {}
          try {
             
              os = comPort.getOutputStream();
           
              bufRead = new BufferedReader(new InputStreamReader(comPort.getInputStream()));

              sortie = new PrintWriter(os, true);
              System.out.println("c bon");
        
          } catch (IOException e) {System.out.println("Yaraab");}
        
         try {
        	 comPort.addEventListener(this);
          } catch (TooManyListenersException e) {}
          comPort.notifyOnDataAvailable(true);
          try {
   
        	  comPort.setSerialPortParams(4800,
                  SerialPort.DATABITS_8,
                  SerialPort.STOPBITS_1,
                  SerialPort.PARITY_NONE);
        	  comPort.setFlowControlMode(comPort.FLOWCONTROL_NONE) ;
   
        	  comPort.setDTR(false);
        	  comPort.setRTS(false);
   
           
   
          } catch (UnsupportedCommOperationException e) {}
   
          readThread = new Thread();
          readThread.start();             
                 
        }
      
       
    public void closePort()
      {
          comPort.close();
      }

    
   public static void ModeText(String str)
     {String reponse="";
           try
         {	int c;        
             
        	   System.out.println("nom");
             sortie.println(str);
         reponse=  (String)  bufRead.readLine().trim();
             System.out.println( "hhh"+reponse);
             Thread.sleep(3000);            

         }
         catch (Exception e)
         {
            JOptionPane.showMessageDialog(null,"Exeption dans envoyer()2. "+e.getMessage(),"Envoi...",JOptionPane.ERROR_MESSAGE);

         }

     }

public void envoyer(String str) throws IOException
  {
	os.write( (byte[]) str.getBytes());
	   os.flush();
	    os.close();
      
         System.out.println("Le port : est ouvert");
       
        try
        {
            Thread.sleep(3000);

        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);
        }

  }

 public void serialEvent(SerialPortEvent arg0)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }




}

Conclusion :

Si quelqu'un a besoin d'aide je serais à votre service.

A voir également