The javax.wireless.messaging has good support for sending/receiving SMS from a MIDlet application. Text or binary messages can be prepared and send using a few lines of code. Sample class for sending the SMS is given below.
class SMSSender implements Runnable
{
String destinationAddress;
String smsPort;
String message;
SMSSender(String address, String port, String msg)
{
this.destinationAddress = address;
this.smsPort = port;
this.message = msg;
}
public void run()
{
//String address = "sms://" + destinationAddress + ":" + smsPort;
String address = "sms://" + destinationAddress;
MessageConnection smsconn = null;
try
{
/** Open the message connection. */
smsconn = (MessageConnection)Connector.open(address);
TextMessage txtmessage =
(TextMessage)smsconn.newMessage(MessageConnection.TEXT_MESSAGE);
txtmessage.setAddress(address);
txtmessage.setPayloadText(message);
smsconn.send(txtmessage);
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
if (smsconn != null)
{
try
{
smsconn.close();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}
For this code to work, the following packages should be imported to the MIDlet application.
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
Next, the MIDlet should have permission to send/receive SMS. This permission is granted by adding the required entries to the MIDlet-Permissions attribute in the JAD file. Sample JAD file entry is given below.
MIDlet-Permissions: javax.microedition.io.Connector.sms, javax.wireless.messaging.sms.send, javax.wireless.messaging.mms.receive
In a NetBeans project, this can be done easily from the Project Properties -> Application Descriptor. The required attribute can be added using the GUI option, as shown.
For the SMS functionality to work, the device configuration used should be CDLC-1.1 or higher and the device profile should be MIDP-2.0 or higher. In a NetBeans project, this can be set easily from Project Properties -> Platform.
Couple of important points. 1) The address can be of the form "sms://address:port". But this will not work in all devices. 2) Since the sending function works asynchronously, the code for sending SMS must be executed in a separate thread.
Receiving SMS is not as straight forward as sending it. First of all, the port number should be specified for accepting the message. While sending an SMS, it is necessary to specify the port number. The message will be delivered to the default inbox of the target device. For receiving, since the port number is to be specified, only messages arriving at that port can be processed by the MIDlet. This is because of security reasons - MIDlets are not allowed to access the default inbox. (if this is allowed, an application written with wrong intention can scan the user's inbox and it will be possible to extract personal information easily). One major problem here is; many devices will not support specifying port number for sending messages. Sample code for receiving SMS is given below.
class SMSReceiver implements Runnable, MessageListener
{
String portNumber = "";
MessageConnection conn = null;
boolean finished = false;
SMSReceiver(String portNo)
{
portNumber = portNo;
}
public void run()
{
if (conn == null)
{
try
{
conn = (MessageConnection)Connector.open("sms://:" + portNumber);
conn.setMessageListener(this);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
try
{
while (!finished)
{
Message msg = conn.receive();
if ( msg != null )
{
if (msg instanceof TextMessage)
{
this.processMessage(msg);
}
else if (msg instanceof BinaryMessage)
{
/* Binary message */
}
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void notifyIncomingMessage(MessageConnection conn)
{
/* Add code for notification handling */
}
public void stopListening()
{
finished = true;
}
private void processMessage(Message msg)
{
/* Add message processing code */
}
}
When new message arrive at the listening port, notifyIncomingMessage() will be invoked automatically. receive() is a blocking call and so, code for listening for message should be written in a separate thread.
One last point; when this application is executed in the simulator, it will ask permission to use text message functionality.
This is because the code is in untrusted domain, by default. To avoid this confirmation message, code should be signed with a valid certificate. This process will create the necessary entries in the JAD file and the application will be able to use text message service, without the need of user intervention. Some of the devices like Nokia 6600 will accept unsigned applications, but this is not the case with many other device manufacturers. In the simulator, it will be possible to switch to the trusted or manufacturer domain. But to sign the code for the real device, we need to get the signature from a service provider such as VeriSign or Thawte.
No comments:
Post a Comment