IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Mobile GSM SMS


Sujet :

Java

  1. #1
    Membre du Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Décembre 2012
    Messages
    109
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Décembre 2012
    Messages : 109
    Points : 47
    Points
    47
    Par défaut Mobile GSM SMS
    bonjour

    depuis un moment j'ai essayer de créer un application java qui envairer un sms par intermédiaire du mobile connecter a mon ordi.
    le problème est que si j'exécute l'application il ne ce passe rien mes si je déconnecte la prise usb du mobile alors il ce mes tous
    de suite après a envoyer le sms.

    voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
     
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
     
    public class PortWriter
    {
    static Enumeration ports;
    static CommPortIdentifier pID;
    static OutputStream outStream;
    static SerialPort serPort;
    static BufferedReader is = null;
    static PrintStream os;
    static int i=0;
    public PortWriter() throws Exception{
    try
    {
    //serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000);
    serPort = (SerialPort)pID.open("COM3",2000);
        System.out.println();
    System.out.println();
    serPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
     
    try
    {
    is = new BufferedReader(new InputStreamReader(serPort.getInputStream()));
    }
    catch (IOException e)
    {
    System.err.println("Can't open input stream: write-only");
    is = null;
    }
     
    os = new PrintStream(serPort.getOutputStream(),true, "US-ASCII");
     
     
    }
    catch (PortInUseException e)
    {
    System.out.println("PortInUseException : "+e);
    }
    catch (Exception e)
    {
    System.out.println("PortInUseException : "+e);
    }
     
    }
     
    public static void main(String[] args) throws Exception{
    ports = CommPortIdentifier.getPortIdentifiers();
     
    while(ports.hasMoreElements())
    {
    pID = (CommPortIdentifier)ports.nextElement();
    System.out.println("Port " + pID.getName());
     
    if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
    {
    //if (pID.getName().equals("/dev/ttyUSB0"))
    if (pID.getName().equals("COM3"))
    {
    PortWriter pWriter = new PortWriter();
    System.out.println("USB found");
     
    sendData();
     
    try
    {
    closingPort();
    }
    catch (Exception e)
    {
    System.out.println("error in calling closingPort()");
    System.out.println(e.toString());
    }
    }
    }
    }
    }
    public static void sendData()
    {
    try
    {
     
     
     
    String sCMGF="AT+CMGF=0\r\n";
    send(sCMGF);
    Thread.sleep(1000);
    readLine();
    String sCMGS="AT+CMGS=\"+33600000000\"\r\n";
    send(sCMGS);
    Thread.sleep(1000);
    readLine();
    String smsMessage="hi test4 testing done\032\r\n";
    send(smsMessage);
     
     
     
    }
    catch (Exception e)
    {
    System.out.println("could not write to outputstream:");
    System.out.println(e.toString());
    }
    }
    //called finally to close the port
    public static void closingPort() throws Exception
    {
    try
    {
    if (is != null)
    is.close();
    }
    catch (IOException e)
    {
    System.out.println("could not close is");
    System.out.println(e.toString());
    }
     
    try
    {
    if (os != null)
    os.close();
    }
    catch (Exception e)
    {
    System.out.println("could not close os");
    System.out.println(e.toString());
    }
     
    try
    {
    if (serPort != null)
    serPort.close();
    }
    catch (Exception e)
    {
    System.out.println("could not close serPort");
    System.out.println(e.toString());
    }
    }
    //send the string to the gsm modem
    public static void send(String cmd)
    {
    try
    {
    os.write(cmd.getBytes());
    }
    catch (IOException e)
    {
    System.out.println("IO Exception : "+e);
    }
    }
    // to read line after each send of string
    public static void readLine()
    {
    try
    {
    // Read the response
    String response = is.readLine();
    System.out.println("Response is :"+response);
    } catch (IOException e)
    {
    System.out.println("IO Exception : "+e);
    }
    }
     
     
     
    }
    ou

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
     
    import javax.comm.*;
    import java.io.*;
    import java.util.*;
     
    public class SimpleWrite implements Runnable, SerialPortEventListener {
     
        public void run() {
        }
        static Enumeration portList;
        static CommPortIdentifier portId;
        static String messageString = "AAA";
        static char ch = '"';
        static String dest = ch + "+33600000000" + ch;  // 11 Digit Mobile Number.
        static InputStream inputStream;
        static SerialPort serialPort;
        static OutputStream outputStream;
     
    public void serialEvent(SerialPortEvent event) {
        switch (event.getEventType()) {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            case SerialPortEvent.DATA_AVAILABLE: {
     
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                try {
     
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    System.err.println("Error while reading Port " + e);
                }
                break;
     
            }
        } //switch
    }
     
    public SimpleWrite(SerialPort serial) {
        try {
            inputStream = serial.getInputStream();
            try {
                serial.addEventListener(this);
            } catch (TooManyListenersException e) {
                System.out.println("Exception in Adding Listener" + e);
            }
            serial.notifyOnDataAvailable(true);
     
        } catch (Exception ex) {
            System.out.println("Exception in getting InputStream" + ex);
        }
     
    }
     
    public static void main(String[] args) {
        String line1 = "AT+CSMS=1\r\n";
        String line2 = "AT+CMGS=" + dest + "\r\n";
        String line3 = messageString + "\r\n";
     
        portList = CommPortIdentifier.getPortIdentifiers();
     
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM3")) {
                    System.out.println("SMS Sending....Port Found");
                    try {
                        serialPort = (SerialPort) portId.open("com3.modem", 2000);
                        SimpleWrite wr = new SimpleWrite(serialPort);
     
                    } catch (PortInUseException e) {
                        System.out.println("Port In Use " + e);
                    } 
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {
                        System.out.println("Error writing to output stream " + e);
                    }
                    try {
                        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {
                    }
                    try {
                        outputStream.write(line1.getBytes());
                        outputStream.write(line1.getBytes());
                        outputStream.write(line2.getBytes());
                        outputStream.write(line3.getBytes());
                        outputStream.write(26);
                        outputStream.flush();
                    } catch (Exception e) {
                        System.out.println("Error writing message " + e);
                    }
                }
            }
        }
    }
     
    /**
     * show text in the text window
     *
     * @param Text text string to show on the display
     */
    public static void showText(String Text) {
        System.out.println(Text);
     
    }
     
    }
    Merci.

  2. #2
    Membre actif
    Homme Profil pro
    Ingénieur de construction de réseaux
    Inscrit en
    Août 2012
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Ingénieur de construction de réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2012
    Messages : 406
    Points : 235
    Points
    235
    La force d'un programmeur ne réside pas dans le fait qu'il écrive des codes puissants mais dans sa capacité à les maintenir!!!

Discussions similaires

  1. réseau mobile GSM
    Par DOZAR dans le forum Mobiles
    Réponses: 0
    Dernier message: 26/09/2014, 11h56
  2. PC mobile et SMS
    Par kalare dans le forum Général Java
    Réponses: 8
    Dernier message: 12/06/2014, 09h26
  3. Besoin d'un composant equivalent a Dialer pour Mobile GSM
    Par zoheir_hm dans le forum Composants VCL
    Réponses: 2
    Dernier message: 14/02/2008, 21h28
  4. Comment interfacer une apllication pour mobile gsm avec une application sur pc
    Par appsn dans le forum Développement Mobile en Java
    Réponses: 2
    Dernier message: 19/08/2006, 13h22
  5. Internet sur PC portable depuis Téléphone Mobile (via GSM)
    Par krugernet dans le forum Développement
    Réponses: 4
    Dernier message: 16/11/2003, 17h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo