Search in sources :

Example 1 with DatagramConnection

use of javax.microedition.io.DatagramConnection 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)1 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)1 Datagram (javax.microedition.io.Datagram)1 DatagramConnection (javax.microedition.io.DatagramConnection)1 MediaException (javax.microedition.media.MediaException)1 RecordStoreException (javax.microedition.rms.RecordStoreException)1 MessageConnection (javax.wireless.messaging.MessageConnection)1 TextMessage (javax.wireless.messaging.TextMessage)1