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

Native Development

Reply
Visitor
yourit
Posts: 1
Registered: ‎06-18-2013
My Device: Z10 STL100-2
Accepted Solution

SMS Listener

Hi - I was just wondering if there were any plans to make available a way to implement an SMS Listener for incoming / outgoing SMS messages (this was possible on the old Java SDKs). I can't see anything equivalent in the latest SDKs, nor on the roadmap.

 

Thanks in advance :-)

Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: SMS Listener

[ Edited ]

Hi

 

in fact we do already support something very similar to the listeners from the Java platform. 

 

SMS is part of the PIM API and the key classes is 

 

bb::pim::message::SmsTransport.

 

This class allows (amongst other things) registration for inbound SMS messages addressed to a given port. You will then receive SMS messages addressed to that port and be able to extract their payload as a byte array. The class uses Qt signals and slots so it's pretty easy to use. Here are a few code snippets to get you started:

 

// in header

 

public slots:
void onMessageReceived(unsigned int, const Message &);
void onRegisterResultReceived(unsigned int port, bb::pim::message::SmsTransportRegistrationResult::Type status );

private:
bb::pim::message::SmsTransport* _sms_transport;

 

// in cpp source, my class called SmsReceiver:

 

SmsReceiver::SmsReceiver(bb::cascades::Application *app) :
   QObject(app), _initialised_ok(false), _sms_transport(new bb::pim::message::SmsTransport(this)) {

    connect(_sms_transport, SIGNAL(registerResultReceived (unsigned int,  
                    bb::pim::message::SmsTransportRegistrationResult::Type)), this,
                   SLOT(onRegisterResultReceived(unsigned int,
                                                                   bb::pim::message::SmsTransportRegistrationResult::Type)));

    connect(_sms_transport, SIGNAL(messageReceived (unsigned int, const Message &)), this, 
                   SLOT(onMessageReceived(unsigned int, const Message &)));

    // registering to receive SMS messages addressed to port 16000
    _sms_transport->registerPort(16000);
}

void SmsReceiver::onRegisterResultReceived(unsigned int port, bb::pim::message::SmsTransportRegistrationResult::Type status) {

	if (status == 1) {
		log(QString("Port %1 registered OK").arg(port));
	} else {
		log("Failed to register port:" + status);
		log("<b>Error:</b>" + getResultReceivedName(status));
	}

}

void SmsReceiver::onMessageReceived(unsigned int port, const Message& message) {

  // the short_message payload is an Attachment to the PIM Message object and the .data() method returns it as a byte array	
	bb::pim::message::Attachment attachment = message.attachmentAt(0);
	hexlog(attachment.data());

}

 

Key points:

  • registration will succeed or fail and you should use the registerResultReceived signal to receive notification of the outcome of this step.
  • when a message addressed to your registered port is received by the OS, a messageReceived signal will be emitted so you should connect one of your methods to this signal. Your slot will receive a Message object. The short_message attribute of the SMS message ("short_message" per GSM 03.40) will be the data part of an Attachment object associated with the Message object. Extract the Attachment object and invoke data() to obtain short_message as a byte array. Note that to properly decode that byte array you really need to know independenly what data_coding value was used when creating and sending the SMS message and whether or not a user data header (UDH) is present.

I hope this helps.

 

Martin

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Developer
kamalakshan
Posts: 107
Registered: ‎06-12-2012
My Device: Dev Alpha C, Playbook

Re: SMS Listener

What if two apps are using the same port. Also is there any range for ports to listen to.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: SMS Listener

Bear with me and I'll answer shortly... there are some other points I want to share at the same time.

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: SMS Listener

Bit snowed under so I'll answer those questions now after all :-)

 

Ports get allocated on a first come, first served basis at present. You get a "Port in use" error in the callback to your slot connected to the signal registerResultReceived if another app tries to register for a port that is already registered elsewhere on the device.

 

Ports are actually defined in ETSI TS 123 040 and there are both 8 bit and 16 bit addressing schemes. You can use 8 bit values in the range 240-255 and 16 bit values in the range 16000 - 16999.

 

The API I described here is really intended to be used by solutions wanting to exploit SMS as a kind of data transport (bearer) and a means of getting binary data into applications. Carriers use this approach sometimes because of course they have the infrastructure in place and all mobile phones (smart or otherwise) support SMS.

 

There's another API you might want to look at though and that's the bb::smileytongue:im::message "Messages API" which can also let you interact with the SMS-MMS "account" on your device to send and receive messages.

 

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Developer
kamalakshan
Posts: 107
Registered: ‎06-12-2012
My Device: Dev Alpha C, Playbook

Re: SMS Listener

Thank you for the explanation.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.