Search in sources :

Example 1 with MessageConnection

use of javax.wireless.messaging.MessageConnection in project CodenameOne by codenameone.

the class GameCanvasImplementation method sendSMS.

/**
 * @inheritDoc
 */
public void sendSMS(final String phoneNumber, final String message) throws IOException {
    String address = "sms://" + phoneNumber;
    MessageConnection con = null;
    try {
        con = (MessageConnection) Connector.open(address);
        TextMessage txtmessage = (TextMessage) con.newMessage(MessageConnection.TEXT_MESSAGE);
        txtmessage.setAddress(address);
        txtmessage.setPayloadText(message);
        con.send(txtmessage);
    } catch (Exception e) {
        throw new IOException("failed to send sms " + e.getMessage());
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
Also used : MessageConnection(javax.wireless.messaging.MessageConnection) IOException(java.io.IOException) TextMessage(javax.wireless.messaging.TextMessage) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException)

Example 2 with MessageConnection

use of javax.wireless.messaging.MessageConnection in project CodenameOne by codenameone.

the class BlackBerryImplementation method sendSMS.

public void sendSMS(final String phoneNumber, final String message, boolean i) throws IOException {
    // one for GSM
    if (!isCDMA()) {
        String address = "sms://" + phoneNumber;
        MessageConnection con = null;
        try {
            con = (MessageConnection) Connector.open(address);
            TextMessage txtmessage = (TextMessage) con.newMessage(MessageConnection.TEXT_MESSAGE);
            txtmessage.setAddress(address);
            txtmessage.setPayloadText(message);
            con.send(txtmessage);
        } catch (Exception e) {
            throw new IOException("failed to send sms " + e.getMessage());
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    } else {
        DatagramConnection connection = null;
        try {
            byte[] data = (message).getBytes("UTF-8");
            connection = (DatagramConnection) Connector.open("sms://" + phoneNumber);
            Datagram dg = connection.newDatagram(connection.getMaximumLength());
            dg.setData(data, 0, data.length);
            connection.send(dg);
        } catch (IOException e) {
            throw new IOException("failed to send sms " + e.getMessage());
        } finally {
            try {
                connection.close();
                connection = null;
            } catch (Exception e) {
            }
        }
    }
}
Also used : MessageConnection(javax.wireless.messaging.MessageConnection) Datagram(javax.microedition.io.Datagram) IOException(java.io.IOException) DatagramConnection(javax.microedition.io.DatagramConnection) TextMessage(javax.wireless.messaging.TextMessage) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException)

Aggregations

IOException (java.io.IOException)2 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)2 MediaException (javax.microedition.media.MediaException)2 RecordStoreException (javax.microedition.rms.RecordStoreException)2 MessageConnection (javax.wireless.messaging.MessageConnection)2 TextMessage (javax.wireless.messaging.TextMessage)2 Datagram (javax.microedition.io.Datagram)1 DatagramConnection (javax.microedition.io.DatagramConnection)1