views:

133

answers:

1

hey i made a lot of search and found some similar types of code. I tried for gsm

method 1 gives IllegalArgumentException

try
{
MessageConnection _mc = (MessageConnection)Connector.open("sms://");
TextMessage tm = (TextMessage) _mc.newMessage(MessageConnection.TEXT_MESSAGE);
tm.setPayloadText(smsText);
tm.setAddress("965xxxxxxx");
_mc.send(tm);
_mc.close();
}catch(exception e){}

method 2: gives java.lang.error exception

  try
    {
    MessageConnection _mc = (MessageConnection)Connector.open("sms://");
    TextMessage tm = (TextMessage) _mc.newMessage(MessageConnection.TEXT_MESSAGE,
"//9790XXXXXX");
    tm.setPayloadText(text);
    _mc.send(tm);
    _mc.close();
        }catch(Exception e){}

I think the problem is with address i also tried : but no success +91965xxxxxxx , 0091965xxxxxxx , 0965xxxxxxx

How my application works----

i have created 2 applications--

1) Application 1 is a background app that is a System module as well as 
    startup application.
2) Another is a uiapplication

the background app runs in background.If there comes an incoming call then a flag value is set in persistent object and after checking that value as true the sms is send to that no from whom call is made.

+1  A: 

ok try this

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import net.rim.device.api.system.RadioInfo;

public class SendSMS extends Thread {

    private String to;
    private String msg;

    public SendSMS(String to, String msg) {
        this.to = to;
        this.msg = msg;

    }

    public void run() {
        if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
            DatagramConnection dc = null;
            try {
                dc = (DatagramConnection) Connector.open("sms://" + to);
                byte[] data = msg.getBytes();
                Datagram dg = dc.newDatagram(dc.getMaximumLength());
                dg.setData(data, 0, data.length);
                dc.send(dg);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dc.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }

        } else {
            MessageConnection mc = null;
            try {
                mc = (MessageConnection) Connector
                        .open("sms://" + to);
                TextMessage m = (TextMessage) mc
                        .newMessage(MessageConnection.TEXT_MESSAGE);
                m.setPayloadText(msg);
                mc.send(m);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    mc.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());

                }
            }
        }
    }

}

and call like this

public void callDisconnected(int callId) {
        final PhoneCall call = Phone.getCall(callId);
        final String number = call.getDisplayPhoneNumber();
        SendSMS sendSMS = new SendSMS(number, "message");
        sendSMS.start();
        super.callDisconnected(callId);
    }
Vivart
hi vivart,your suggestion did not give ant exception but it did not send the message to the number specified by me.What to do???????????????
SWATI
this is a tested code it works fine for me. have you tested on real device?
Vivart
ya i tested on the device but msg was not sendi think i should explain u the whole logic in my question.
SWATI
hey 1 more thing if i place System.out.println("message sent"); statement between _mc.send(tm); and _mc.close();then statement does not executes
SWATI
hey Vivart your refined suggestion throws=====net.rim.device.api.io.IOCancelledException Exception while sending the message on the simulator
SWATI
moreover i found that this exception is generated 3 times
SWATI
i made a check that code executes only once but still IOCancelledException exists
SWATI
if i am using two simulators and my code with port number, its working fine. I have successfully received sms in 2nd simulator.
Vivart
thanks so much vivart..................ur method worked on the deviceI m really thankful to u!!!!
SWATI
hey Vivart can u tell me--Does this code works for both gsm and cdma or it only works for gsm mobiles.Sorry i unchecked the ans just to get response from u
SWATI
see my edited post.
Vivart
thanku so muchbut how to test cdma code on simulator
SWATI
well thank u so much for all your help.i will test it somehow.Thanks so much!!!!
SWATI
usually xx30 model number for blackberry is CDMA. So start any simulator like 8130,8830,etc it will be CDMA.
Vivart
oh this is good i dnt know thanks.Thank u
SWATI