Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 18
Registered: ‎11-08-2008
My Device: BlackBerry Bold 9900
My Carrier: Vodafone

Sending SMS from in-app

We've made Talk To You Later app few months ago. Small but usefull app greatly recieved by BlackBerry users. Till few days ago we had no problem but in the last few days we are getting reports (and also found this bug on our test devices too) from our users that text message is not sent from inside application.

 

We have problem, to get SMS send function to work. We had our own implementation of the problem, that worked well at the start and then stopped working for an unknown reason.
So we decided to copy the implementation of the blackberry sample code. But there was still no change... the SMS will not get send.

 

Bellow is the code so please check it out and let us know if you are able to help us out with it.

 

/**
     * A simple abstraction of an SMS message, used by the SendThread class
     */
    private static final class SmsMessage
    {
        private String _address;  
            
        private String _msg;    
        
        
        /**
         * Creates a SMS message
         * @param address The address of the recipient of the SMS message
         * @param msg The message to send
         */
        public SmsMessage(String address, String msg)
        {
            _address = address;         
            _msg = msg;
        }
        
        
        /**
         * Returns a Message object representing this SMS message
         * @param mc The MessageConnection source with which to create the Message from
         * @return The Message object representing the SMS message
         */
        public Message toMessage(MessageConnection mc)
        {      
            // If the user chose to have messages routed to the inbox (port = 0),
            // we need to specify an address without a port number.
            String addressString = "//" + _address;
            
            TextMessage m = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE , addressString);
            m.setPayloadText(_msg);
            
            return m;
        }
        
        
        /**
         * Returns a Datagram object representing this SMS message
         * @param datagramConnectionBase The DatagramConnectionBase object with which to create the Datagram from
         * @return The Datagram object representing the SMS message
         */
        public Datagram toDatagram(DatagramConnectionBase datagramConnectionBase) throws IOException
        {       
            DatagramBase datagram = null;                            
            byte[] data = _msg.getBytes("ISO-8859-1");
            datagram = (DatagramBase) datagramConnectionBase.newDatagram();
            SmsAddress smsAddress = new SmsAddress("//" + _address); 
            SMSPacketHeader smsPacketHeader = smsAddress.getHeader();
            smsPacketHeader.setMessageCoding(SMSPacketHeader.MESSAGE_CODING_ISO8859_1);
            datagram.setAddressBase(smsAddress);            
            datagram.write(data, 0, data.length);         
            
            return datagram;
        }
    }
 
/**
     * A thread to manage outbound transactions
     */
    private class SendThread extends Thread
    {
        private boolean _stopped = false;        
        // Create a vector of SmsMessage objects with an initial capacity of 5.
        // For this implementation it is unlikely that more than 5 msgs will be
        // queued at any one time.
        private Vector _msgs = new Vector(5);       
        
        /**
         * Queues message send requests to send later
         * @param address The address to send the message to
         * @param msg The message to send
         */
        public void send(String address, String msg)
        {
            SmsMessage message = new SmsMessage(address, msg);
            synchronized (this._msgs)
            {
                if (! this._stopped)
                {
                    this._msgs.addElement(message);
                    this._msgs.notifyAll();
                }
            }
        }
        /**
         * Stops this thread from sending any more messages
         */
        public void stop()
        {
            synchronized (this._msgs)
            {
                this._stopped = true;
                this._msgs.notifyAll();
                this._msgs.removeAllElements();       
                try
                {
                    if ( _conn != null )
                    {
                        _conn.close();
                    }
                } 
                catch (IOException ioe ) 
                {                
                }             
            }
        }
        
        /**
         * Sends any queued messages until stop() is called
         * @see #stop()
         * @see java.lang.Runnable#run()
         */
        public void run()
        {
            while (true)
            {
                final SmsMessage smsMessage;
                synchronized (this._msgs)
                {
                    if (this._stopped)
                    {
                        return;
                    }
                    else if (this._msgs.isEmpty())
                    {
                        try
                        {
                            this._msgs.wait();
                        }
                        catch (InterruptedException ie)
                        {
                            return;
                        }
                    }                    
                    if (this._stopped)
                    {
                        return;
                    }
                    else
                    {
                        smsMessage = (SmsMessage) this._msgs.elementAt(0);
                        this._msgs.removeElementAt(0);
                    }
                }
                try
                {
                    if(isCDMA())
                    {
                        DatagramConnectionBase dcb = (DatagramConnectionBase)_conn; 
                        dcb.send(smsMessage.toDatagram(dcb));
                    }
                    else
                    {
                        MessageConnection mc = (MessageConnection)_conn;
                        mc.send(smsMessage.toMessage(mc));
                    }
                }
                catch (IOException ioe) 
                {                    
                    //updateStatus(ioe.toString());
                }
            }
        }
    }
Branko Nikolic (aka Hedone Hawker)

www.bbdevbalkan.com
www.i-mentalist.com
www.hedonedesign.com
Developer
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2
My Carrier: IND XL

Re: Sending SMS from in-app

i think the problem was the "//" ?

sms should be "sms://"

Contributor
Posts: 18
Registered: ‎03-17-2012
My Device: playbook
My Carrier: mobitel

Re: Sending SMS from in-app

The connection is established earlier

_conn  = Connector.open("sms://:0");

 This part is just the address string, so that shouldn't be the problem.

 

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Sending SMS from in-app

Any exceptions or errors in the logs?

 

Does it happen on all devices/OS versions?

 

E.

Highlighted
New Developer
Posts: 18
Registered: ‎11-08-2008
My Device: BlackBerry Bold 9900
My Carrier: Vodafone

Re: Sending SMS from in-app

It happens on all devices and OSes (as far as we have reports from our customers and as far as we tested it on our devices).
Branko Nikolic (aka Hedone Hawker)

www.bbdevbalkan.com
www.i-mentalist.com
www.hedonedesign.com