How to write SMS application in blackberry?

后端 未结 3 1391
广开言路
广开言路 2020-12-25 08:59

Can any one help me to write application to send and recieve sms in blackberry.If u can provide me some code snippet .

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-25 09:21

    It seems sending SMS from a BlackBerry is not always that simple. I thought I had it figured out, but it didn't work when the BlackBerry user was on a CDMA network as opposed to GSM, which I was using.

    I found the solution here and adapted it to this. Different from that example is also the port number, I use port 5016 as suggested on the Blackberry support forum and the Blackberry knowledge center.

    private static byte[] stringToByte(String s)
    {
        char[] sa = s.toCharArray();
        byte[] ba = new byte[sa.length];
    
        for (int i = 0; i < ba.length; i++) {
            ba[i] = (byte) (sa[i] & 0xFF);
        }
    
        return ba;
    }
    
    private static void sendCDMAText(String nr, String message) throws IOException
    {
        DatagramConnection conn = (DatagramConnection) Connector.open("sms://+" + nr + ":5016");
        byte[] bytes = stringToByte(message);
        Datagram msg = conn.newDatagram(bytes, bytes.length);
        conn.send(msg);
    
    }
    
    private static void sendSMS(String nr, String message) throws IOException
    {
        MessageConnection conn = (MessageConnection) Connector.open("sms://" + nr);
        TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
        msg.setPayloadText(message);
        conn.send(msg);
    }
    
    public static void sendTextMessage(String nr, String message) throws IllegalArgumentException, InterruptedIOException, NullPointerException, SecurityException, IOException
    {
        if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
            sendCDMAText(nr, message);
            return;
        }
    
        sendSMS(nr, message);
    }
    

    To send a text message, you would call it like this:

    sendTextMessage("555123123", "The little text message you wanted to send.");
    

    (Where 555123123 is a Hollywood phone number.)

提交回复
热议问题