Jsr120 sup

618 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
618
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Jsr120 sup

  1. 1. <ul><li>JSR 120 </li></ul>
  2. 2. Introduction <ul><li>Wireless Messaging API (WMA) is an optional package based on </li></ul><ul><li>the Generic Connection Framework (GCF) </li></ul><ul><li>It supports Java 2 Platform, Mobile Edition (J2ME) applications </li></ul><ul><li>targeted at cell phones and other devices that can send and receive </li></ul><ul><li>wireless messages </li></ul><ul><li>The design of the messaging functionality is similar to datagram </li></ul><ul><li>functionality that is used for UDP in the Generic Connection </li></ul><ul><li>Framework </li></ul><ul><li>The WMA provides a common interface you can use to enable an </li></ul><ul><li>application based on the Mobile Information Device Profile (MIDP) </li></ul><ul><li>to send and receive short text and binary messages </li></ul>
  3. 3. Check WMA API <ul><li>To determine if the optional WMA API is available and which version is available you have to call: </li></ul><ul><li>String currentVersion = System.getProperty(“javax.wireless.messaging”) </li></ul><ul><li>If the WMA API is available a string with the version will be returned (eg: “1.0”) </li></ul><ul><li>If the WMA API is not available a null value is returned </li></ul>
  4. 4. WMA 1.0 Package All WMA-specific interfaces and classes are contained in a single package, javax.wireless.messaging, which defines all the APIs required for sending and receiving wireless text, binary Subinterface of the GCF Connection, which provides a factory of Messages, and methods to send and receive Messages MessageConnection Subinterface of Message that provides methods to set and get the text payload TextMessage Defines the listener interface to implement asynchronous notification of Message objects MessageListener Subinterface of Message that provides methods to set and get the binary payload BinaryMessage Base Message interface, from which subinterfaces (such as TextMessage and BinaryMessage) are derived Message Description Interfaces
  5. 5. WMA 1.0 Overview <ul><li>The J2ME Wireless Messaging API (WMA) specifies a standard set of APIs </li></ul><ul><li>that J2ME applications running on SMS-enabled devices can use to </li></ul><ul><li>communicate with network peers via the SMS and CBS protocols </li></ul>
  6. 6. WMA API Overview <ul><li>The interface javax.wireless.messaging.Message is the base for all types of messages communicated using the WMA </li></ul><ul><li>In some respects, a Message looks similar to a Datagram: it has source and destination addresses, a payload, and ways to send and block for a message </li></ul><ul><li>WMA provides Listener for receiving text and binary message </li></ul><ul><li>A MessageConnection can be created in one of two modes: as a client connection or as a server connection </li></ul><ul><li>In a client mode connection, messages can only be sent and the URL for a client connection includes a destination address, as in </li></ul><ul><li>MessageConnection clientConn = (MessageConnection)Connector.open(&quot;sms:// +18643630999:5000&quot;); </li></ul><ul><li>In a server mode connection, messages can be sent or received and the URL for a server connection specifies a local address, as in </li></ul><ul><li>serverConn = (MessageConnection)Connector.open(&quot;sms://:5000&quot;); </li></ul><ul><li>Trying to bind to an already reserved local address causes an IOException to be thrown . </li></ul>
  7. 7. Message Connection and Its Relationship to GCF
  8. 8. WMA API Overview <ul><li>The URL scheme for creating a MessageConnection is not specific to a single protocol instead it is intended to support many wireless protocols </li></ul><ul><li>The WMA specification defines the following protocol adapters: </li></ul><ul><ul><li>&quot;sms&quot; for Short Messaging System. SMS is bi-directional: you can create, send, and receive messages (and thus support both client and server connections). </li></ul></ul><ul><ul><li>&quot;cbs&quot; for Cell Broadcast Short Message. CBS messages are broadcast by a base station, and can only be received. Attempting to send on a cbs connection results in an IOException. </li></ul></ul>
  9. 9. How To Send A Text Message <ul><li>Destination address with telephone number and port number </li></ul><ul><li>String addr = “sms://+358401234567:1234”; </li></ul><ul><li>A client mode connection is created by passing a string identifying a </li></ul><ul><li>destination address to the Connector.open() method. This method </li></ul><ul><li>returns a MessageConnection </li></ul><ul><li>MessageConnection conn = (MessageConnection)Connector.open(addr); </li></ul><ul><li>String constant TEXT_MESSAGE of MessageConnection class is passed </li></ul><ul><li>to the newMessage() factory method </li></ul><ul><li>TextMessage msg = </li></ul><ul><li>(TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE); </li></ul><ul><li>The message is passed as parameter in setPayloadText() of TextMessage </li></ul><ul><li>msg.setPayloadText(“Hello World!”); </li></ul><ul><li>Now pass TextMessage object to send() method to send a message </li></ul><ul><li>conn.send(msg); </li></ul>
  10. 10. How To Send a Binary Message <ul><li>Destination address with telephone number and port number </li></ul><ul><li>String addr = “sms://+358401234567:1234”; </li></ul><ul><li>A client mode connection is created by passing a string identifying a </li></ul><ul><li>destination address to the Connector.open() method. This method </li></ul><ul><li>returns a MessageConnection </li></ul><ul><li>MessageConnection conn = (MessageConnection)Connector.open(addr); </li></ul><ul><li>String constant BINARY_MESSAGE of MessageConnection class is passed </li></ul><ul><li>to the newMessage() factory method </li></ul><ul><li>BinaryMessage bmsg = (BinaryMessage)conn.newMessage(MessageConnection.BINARY_MESSAGE); </li></ul><ul><li>The message in byte array is passed as parameter in setPayloadData() of TextMessage </li></ul><ul><li>byte[] msg=“HelloWorld”.getBytes(); </li></ul><ul><li>msg.setPayloadData(msg); </li></ul><ul><li>Now pass BinaryMessage object to send() method to send a message </li></ul><ul><li>conn.send(msg); </li></ul>
  11. 11. Waiting for Incoming Message <ul><li>Only a server-mode MessageConnection can receive messages. </li></ul><ul><li>There are two approaches to receiving messages: </li></ul><ul><li>Synchronously, having a thread wait for incoming messages </li></ul><ul><li>Asynchronously, having the system notify the application when new messages directed to the application have arrived </li></ul>
  12. 12. Synchronous messaging <ul><li>This thread loops for messages, invoking Message.receive() to block while it waits for incoming messages </li></ul><ul><li>Create a server mode connection </li></ul><ul><li>MessageConnection sconn = (MessageConnection) Connector.open(&quot;sms://:3333&quot;); </li></ul><ul><li>Now check in a new thread for a message continuously </li></ul><ul><li>while (true) { </li></ul><ul><li>//listen for a new message and handle the incomming message </li></ul><ul><li>Message msg = sconn.receive(); </li></ul><ul><li>//checks whether the received message is TextMessage </li></ul><ul><li>if (msg instanceof TextMessage) { </li></ul><ul><li>TextMessage tmsg = (TextMessage) msg; </li></ul><ul><li>//getPayloadText() of TextMessage returns the message </li></ul><ul><li>String msgText = tmsg.getPayloadText(); </li></ul><ul><li>// Construct the return message </li></ul><ul><li>TextMessage rmsg = (TextMessage) sconn.newMessage( </li></ul><ul><li>MessageConnection.TEXT_MESSAGE); </li></ul><ul><li>rmsg.setAddress ( tmsg.getAddress() ); </li></ul><ul><li>rmsg.setPayloadText( &quot;Thanks!&quot; ); </li></ul><ul><li>sconn.send(rmsg); </li></ul><ul><li>} </li></ul>
  13. 13. Asynchronous Messaging <ul><li>WMA defines the MessageListener interface, with a single method, </li></ul><ul><li>notifyIncomingMessage(), which the platform invokes each time it receives a Message </li></ul><ul><li>Asynchronously. </li></ul><ul><li>public class MyClass implements MessageListener { </li></ul><ul><li>public void notifyIncomingMessage(MessageConnection connection) { </li></ul><ul><li>//read the message in a new thread </li></ul><ul><li>} </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>to enable asynchronous messaging, the application must register a message listener </li></ul><ul><li>with the connection </li></ul><ul><li>... </li></ul><ul><li>MessageConnection sconn = (MessageConnection) Connector.open(&quot;sms://:3333&quot;); </li></ul><ul><li>sconn.setMessageListener(MyClass); </li></ul><ul><li>... </li></ul>
  14. 14. <ul><li>Thank You </li></ul>

×