Top Banner
J2ME Messaging Khanh Le
20

J2ME Messaging

Jan 22, 2016

Download

Documents

jolie

Khanh Le. J2ME Messaging. Objective. The objective of wireless messaging is to extend the networking and I/O capabilities of J2ME applications to send and receive messages using the messaging services on GSM networks, like Short Message Service (SMS) and Cell Broadcast Service (CBS). - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: J2ME Messaging

J2ME Messaging

Khanh Le

Page 2: J2ME Messaging

Objective

The objective of wireless messaging is to extend the networking and I/O capabilities of J2ME applications to send and receive messages using the messaging services on GSM networks, like Short Message Service (SMS) and Cell Broadcast Service (CBS).

Page 3: J2ME Messaging

The Wireless Messaging System - an overview

The notion of wireless messaging provides a whole new vista to J2ME. J2ME applications powered by wireless messaging have a platform-independent access to wireless communication resources like Short Message Service (SMS) and Cell Broadcast Service (CBS) for Global System for Mobile Communication (GSM) Networks, a mobile telephony system permitting inter-country communication.

Before proceeding with the details on how J2ME Wireless Messaging works, I will discuss, in short, the SMS and CBS messaging systems in a GSM Network.

Page 4: J2ME Messaging

GSM Short Message Service (SMS)

SMS is the transmission of short text messages to and from a mobile phone, fax machine, and/or IP address in a GSM network.

Messages must be no longer than 160 alphanumeric characters and contain no images or graphics.

The main features of this service are speed, cheap rates, and the guarantee that the message will reach the target person, even if he is out of radio coverage or his phone is turned off.

Page 5: J2ME Messaging

GSM Short Message Service (SMS)

Page 6: J2ME Messaging

GSM cell broadcast service The GSM cell broadcast service allows messages to be sent to

every Mobile Station (MS), such as a mobile phone, fax machine, and/or IP address currently in a particular cell.

Cell broadcast messages are repeated at intervals over a period of time, which allows an MS to receive the message even if entering the cell after the first transmission.

The data can be sent either as binary data or ASCII text up to 15 pages in length, with a page being up to 93 characters in length; the test set only provides support for ASCII messages.

Cell broadcast messages are classified by topic and allocated a channel number, message code, update number, and language

While SMS is a one-to-one and one-to-few messaging system, CBS provides one-to-many messaging within a certain geographical area.

Page 7: J2ME Messaging

The wireless messaging system

This system can be viewed as a 3-tiered architecture, consisting of the Interface Layer, Implementation Layer and Transport Layer.

The Interface Layer constitutes a generic set of messaging interfaces, independent of any messaging protocol.

The Implementation Layer contains classes which implement each Interface Layer to access wireless messaging like SMS or CBS functionalities on a GSM mobile device..

The Transport Layer contains classes that are the actual implementation of protocols that carry messages to the mobile device.

Page 8: J2ME Messaging

The wireless messaging system

Page 9: J2ME Messaging

The Generic Messaging API, javax.wireless.messaging

This API defines all the interfaces for sending and receiving wireless messages. Following is a list of interfaces:

Message. This provides the basic definition of a message, which acts as a container holding address, payload, and flags to send and block for a message. It is a superinterface to TextMessage, a message object with a text payload attribute, and a BinaryMessage, a message object with a binary payload attribute.

Page 10: J2ME Messaging

The Generic Messaging API, javax.wireless.messaging

MessageConnection. This provides the basic functionality of receiving and sending messages. It contains a method for sending and receiving messages, a factory method for creating new Message objects, and a method that calculates the number of segments of the underlying protocol that are needed to send a specified Message object.

This class is instantiated by a call to Connector.open().

Page 11: J2ME Messaging

The Generic Messaging API, javax.wireless.messaging

In a client mode connection, messages can only be sent. A client mode connection is created by passing a string identifying a destination address to the Connector.open() method. This method returns a MessageConnection object.

clientConn = (MessageConnection)Connector.open("sms:// +18643630999:5000");

In a server mode connection, messages can be sent or received. A server mode connection is created by passing a string that identifies an end point (protocol dependent identifier, for example, a port number) on the local host to the Connector.open() method.

serverConn = (MessageConnection)Connector.open("sms://:5000");

MessageListener. This provides a basic mechanism to notify the MIDlet application of an incoming message. It allows a MIDlet to receive a callback when new messages are available to be read.

Page 12: J2ME Messaging

The Short Message Service API

The com.sun.midp.io.j2me.sms package provides an API for the Short Message Service Messaging system and allows MIDlet to access SMS functionality on a GSM mobile device.

The main components of the package -- MessageObject and Protocol -- support the sending and receiving of SMS messages.

Page 13: J2ME Messaging

The Short Message Service API

MessageObject. MessageObject is the implementation of a SMS message. In the Implementation layer, the javax.wireless.messaging.Message interface is implemented as a buffer. The MessageObject handles the creation of message buffers and the input/output operations out of a buffer. Furthermore, it has two subclasses -- TextObject and BinaryObject. These classes implement a SMS message with a payload, whether text or binary.

Protocol. This implements the message connection to a low-level Transport mechanism required to transmit a SMS message. In the process, it checks for all runtime configuration parameters and handles exceptions related to invalid URL syntax, security violations, I/O violations, and illegal arguments. Protocol also handles the sending and receiving of messages using a datagram or serial port connection.

Page 14: J2ME Messaging

The Cell Broadcast Messaging API

The com.sun.midp.io.j2me.cbs package provides an API for the Cell Broadcast Messaging system and allows MIDLlet to access CBS functionality on a GSM mobile device.

The main component of the package, com.sun.midp.io.j2me.cbs.Protocol, supports the receipt of a CBS Message.

The CBS differs from SMS in that: The URL Connection string does not support a designated host,

and It is meant for an inbound-only protocol. A MIDlet having CBS

capability can receive messages, but cannot send them.

Page 15: J2ME Messaging

A J2ME messaging application

In this section, I will show an example of a WMAServer that waits for incoming SMS messages and then displays them on a phone screen. The javax.microedition.lcdui package provides a set of features for implementation of user interfaces for the applications.

The WMAServer MIDlet creates a server mode connection by passing a string that identifies an end point (protocol-dependent identifier -- like a port number, for example ) on the local host to the Connector.open() method.

In order to get notified of an incoming message, the MIDlet registers a MessageListener object at the MessageConnection instance, serverConn.

serverConn.setMessageListener(MessageListener ml);

Page 16: J2ME Messaging

A J2ME messaging application

It also implements notifyIncomingMessage() in the MessageListener interface. When an incoming message arrives at the MessageConnection, the notifyIncomingMessage() method is called. The application must retrieve the message using the receive() method of the MessageConnection.

The WMAServer application reads the payload data from the incoming message, whether text or binary, and stores it in a string object to be displayed later.

Page 17: J2ME Messaging

A J2ME messaging application

public void notifyIncomingMessage(MessageConnection conn) { Message msg = null; // Try reading (maybe block for) a message try { msg = conn.receive(); } catch (Exception e) { // Handle reading errors System.out.println("processMessage.receive " + e); } // Process the received message if (msg instanceof TextMessage) { TextMessage tmsg = (TextMessage)msg; msgReceived = tmsg.getPayloadText(); } else { // process received message if (msg instanceof BinaryMessage) { BinaryMessage bmsg = (BinaryMessage)msg; byte[] data = bmsg.getPayloadData(); // Handle the binary message... msgReceived = data.toString(); } }

Page 18: J2ME Messaging

A J2ME messaging application

The application provides a destroyApp() method when the connection resources and associated listener objects must be released.

public void destroyApp(boolean unconditional) {

try {

if (serverConn != null) {

serverConn.setMessageListener(null);

serverConn.close();

}

} catch (IOException e) {

// Handle the exception...

e.printStacktrace();

}

Page 19: J2ME Messaging

A J2ME messaging application

Next is an example of a WMA client that sends a SMS message to a GSM mobile phone. The WMA Client can only send messages. It creates a MessageConnection by passing a string identifying a destination address (which is a valid mobile number) to the Connector.open() method.

try { clientConn = (MessageConnection)Connector.open("sms:// +18643630999:5000"); } catch (IOException ioExc) { System.out.println("Client connection could not be obtained"); ioExc.printStackTrace(); }

Page 20: J2ME Messaging

A J2ME messaging application

It then creates a valid TextMessage object by setting the payload and destination address and sends the message through the MessageConnection.

TextMessage tmsg = (TextMessage)clientConn.newMessage (MessageConnection.TEXT_MESSAGE);tmsg.setAddress("sms://18643630999:5000");tmsg.setPayloadText(msgToSend); clientConn.send(tmsg);