Use javame for wireless message transmission

Source: Internet
Author: User

One of the most common features of a mobile phone is message transmission. Text messages or short messages have become increasingly popular since the beginning. Of course, multimedia messages (MMS) add images, sounds, videos, and other multimedia content.

When MMS emerged, some experts predicted that it would replace SMS. If its price was not so high, this prediction may indeed become a reality. Even so, this function that can transmit multimedia content between mobile phones is becoming increasingly popular. The success of recent mobile phone emails means that in the future, the main form of mobile phone message transmission may be email, but there is still a long way to go.

This month, I will discuss the Java me wireless messaging API. I will start with a few simple coding examples and discuss the various opportunities it brings to enterprise developers.

Wireless message API

The wireless message API (WMA) 2.0 defined in the JSR-205 is a set of optional APIs for wireless messages. the WMA connection is based on the universal connection framework, which is the basis for communication in the cldc-based Java me architecture. WMA 2.0 is backward compatible with WMA 1.1 and supports sending and receiving multimedia messages. In other words, the wireless message API provides high-level abstraction for wireless communication. It completely hides the transport layer, so all you need to do is create, send, and receive messages.

WMA supports text messages, Community broadcasts, and multimedia messages. Text messages are simple. Most people know what a text message is. SMS messages are extremely popular and have become part of daily life. WMA supports sending and receiving text messages. Sending is very simple: you only need to define the target address and text. The text is called payload ). Receiving is a little more complicated because the application is required.ProgramIt can listen on incoming messages. The following example shows how to read the incoming message.

Message Content

A binary message is a message with binary content. The content can be anything that you want to pass to or from an application to another client or server. Multiple messages can contain multiple media, such as text, audio, and video.

Residential broadcast may be the least known message type. The residential broadcasting service is a data service in which messages are broadcast by the base station and received by each mobile station listening to the base station. This service is unidirectional, which means that WMA can only be used to receive such messages.

Send and receive

It is easy to send text messages. The procedure is as follows:

    1. Set the mobile phone number (address ).
    2. Use the connector interface to create messageconnection.
    3. Create a new message and convert its type to textmessage.
    4. Set the Net Load.
    5. Call the send () method and pass the message instance to the method to send the message.

The sender information contained in the sent message is the same as that when the message is sent normally. Listing 1 shows how to use Java me WMA to send messagesCode.

List 1. Send messages

Try {
String ADDR = "SMS ://+ 358401234567 ";
Messageconnection conn = (messageconnection) connector. Open (ADDR );
Textmessage MSG = (textmessage) Conn. newmessage (messageconnection. text_message );
MSG. setpayloadtext ("This Is A Test message! ";
Conn. Send (MSG );
} Catch (exception e ){...}

Receiving text messages is slightly complicated, but not too complicated. Open messageconnection (5432 in this example) on a port and read the entered message from messageconnection. You can use instanceof to test the message type. To receive notifications with incoming messages, the application must implement the messagelistener interface. Therefore, you must write code that is more complex than listing 2 to make it really work.

List 2. receive messages

 
Try {
String ADDR = "SMS: //: 5432 ";
 
Messageconnection conn = (messageconnection) connector. Open (ADDR );
 
Message MSG = NULL;
 
While (someexitcondition ){
 
MSG = conn. Receive ();
 
If (MSG instanceof textmessage)
 
{
 
Textmessage tmsg = (textmessage) MSG;
 
String receivedtext = tmsg. getpayloadtext ();
 
// Respond with the same text with hanks.
 
Tmsg. setpayloadtext (Hanks );
Conn. Send (tmsg );
 
} Else {
 
// Received message was not a text message, but e.g. Binary...
 
}
 
} // End while
 
} Catch (exception e ){...}

Sending a binary message is similar to sending a text message. First, you need to put the data in the byte array, then create messageconnection (as shown in listing 3), and create binary_message.

Listing 3. Sending binary messages

Try {
String STR = "Hello! ";
Byte [] MSG = Str. getbytes ();
String ADDR = "SMS ://+ 358401234567 ";
Messageconnection conn = (messageconnection) connector. Open (ADDR );
Binarymessage Bm = (binarymessage) MC. newmessage (messageconnection. binary_message );
If (URL! = NULL)
BM. setaddress (URL );
BM. setpayloaddata (MSG );
Conn. Send (BM );
} Catch (exception e ){}

For more information about how to use WMA to write code, see references at the end of this article. Now, I focus on the potential applications of WMA.

  

Endless Possibilities

What can I do with messages? It is really cool to send text messages from a Java me application-you can customize the user interface and usage based on your own ideas-but it is not a favorite application. Nokia and Sony-Ericsson have invested a lot of time and money in making this message as simple and convenient as possible.

Commercial applications and predefined text information bring true advantages to them. For example, in commercial applications, there may be situations where GPRS, WLAN, or other IP-based protocols are unavailable or unreliable, and traditional interfaces based on SMS are used; you can use text messages as the transmission protocol. For example, in a certain scenario, you can ask the server (such as the CRM server) to send text messages with the latest sales data, and then the mobile app can confirm the messages from the business service, use charts or other methods you like to present data (for a larger amount of data, HTTP or datagram will certainly be better ).

You can also use both text and binary messages in game programming. For example, a player can send game-specific content (such as the obtained Level, tool, or number of points) to friends playing the same mobile game. A friend's application receives messages and opens the sent content. Of course, binary data can also be used in commercial applications.

Multimedia messages are not very popular in applications or services, but they do provide interesting opportunities for the gaming field. For example, you can send game screen snapshots to friends who receive snapshots in the form of multimedia messages without relying on any specific applications. Sending weather forecasts in the form of images is another convenient service to use MMs.

Three types of message transmission

WMA message transmission can be divided into: Server to mobile phone, mobile phone to server, and mobile phone to mobile phone.

In server-to-mobile message transmission, the CRM application can send data to the mobile application, and the mobile application can receive data and present the data in a user-friendly manner. In the message transmission from the mobile phone to the server, the mobile phone application can send data to the server (HTTP connections can also do this, But HTTP connections cannot always exist ). In the message transmission from a mobile phone to a mobile phone, you can send application data or game data to a friend's mobile phone for game project exchange and other operations.

Conclusion

In this article, I introduced the use of Java me WMA for wireless message transmission. I provided some basic sending and receiving examples and discussed some usage methods of wireless message transmission. WMA supports sending and receiving text messages, binary messages, and multimedia messages, and supports receiving community broadcast messages. All these message types provide many opportunities for game developers and commercial application developers.

Message transmission (email and IM) has proved to be an important part of daily Internet applications. Text messages have become a crucial part of the mobile user experience. The simplicity of WMA makes it an honor to become a member of wireless application developers toolkit.

In order to receive the notificationMessageconnectionInstanceServerconnRegisterMessagelistenerObject.

Serverconn. setmessagelistener (messagelistener ml );

It is stillMessagelistenerInterface implementationNotifyincomingmessage (). When the incoming message arrivesMessageconnection, You can callNotifyincomingmessage ()Method. The application must useMessageconnectionOfReceive ()Method to retrieve the message.

The wmaserver application reads text or binary payload data from the incoming message and stores it in a string object for later display.

 Public void policyincomingmessage (messageconnection conn) {message MSG = NULL; // try reading (maybe block) A message try {MSG = Conn. receive ();} catch (exception e) {// handle reading errors system. out. println ("processmessage. receive "+ E);} // process the received ed message if (MSG instanceof textmessage) {textmessage tmsg = (textmessage) MSG; msgreceived = tmsg. getpayloadtext ();} else {// process received ed message if (MSG instanceof binarymessage) {binarymessage bmsg = (binarymessage) MSG; byte [] DATA = bmsg. getpayloaddata (); // handle the binary message... msgreceived = data. tostring () ;}}

Test code:

Import Javax. microedition. Io. connector;
Import Javax. microedition. lcdui. Command;
Import Javax. microedition. lcdui. commandlistener;
Import Javax. microedition. lcdui. display;
Import Javax. microedition. lcdui. displayable;
Import Javax. microedition. lcdui. form;
Import Javax. microedition. lcdui. textfield;
Import Javax. microedition. MIDlet. MIDlet;
Import Javax. Wireless. messaging. messageconnection;
Import Javax. Wireless. messaging. textmessage;

Public ClassSmsmidletExtendsMIDletImplementsCommandlistener {

PrivateCommand exit;
PrivateCommand send;
PrivateTextfield number;
PrivateTextfield content;
PrivateForm form;

Public Smsmidlet (){
Number =   New Textfield ( " Phone number: " , " 13488755864 " , 50 ,
Textfield. Any );
Content =   New Textfield ( " Content: " , " SMS Test " , 256 , Textfield. Any );

send = New command ( " send " , command. OK, 0 );
exit = New command ( " exit " , command. back, 1 );

Form= NewForm ("SMS Test");
Form. append (number );
Form. append (content );
Form. addcommand (send );
Form. addcommand (exit );
Form. setcommandlistener (This);
}

Public   Void Commandaction (command C, displayable d ){
If (D. Equals (form )){
If (C. Equals (send )){
String num = Number. getstring ();
String con = Content. getstring ();
If (Num ! =   Null   && Num. Length () >   1 ){
If (Con ! =   Null   && Con. Length () >   1 ){
Sendsms (Num, con );
}
}
} Else   If (C. Equals (exit )){
Yydestroyed ();
}
}
}

Protected VoidDestroyapp (BooleanArg0 ){
}

Protected VoidPauseapp (){
}

Protected VoidStartapp (){
Display. getdisplay (This). Setcurrent (form );
}

Public   Void Sendsms ( Final String phonenumber, Final String message ){
New Thread (){
Public   Void Run (){
Try {
// Address
String address =   " SMS :// "   + Phonenumber;

// Establish a connection
Messageconnection Conn = (Messageconnection) connector
. Open (Address );
Form. append ( " Establish a connection " );
// Set the Short Message Type to text. Short Messages can be text or binary.
Textmessage msg = (Textmessage) Conn
. Newmessage (messageconnection. text_message );
Form. append ( " Create information " );
// Set Information Content
MSG. setpayloadtext (Message );
Form. append ( " Settings " );
// Send
Conn. Send (MSG );
Form. append ( " Sent successfully " );
Conn. Close ();
Form. append ( " Close connection " );
} Catch (Exception e ){
Form. append (E. tostring ());
// Unprocessed
}< BR >}. start ();
}< BR >}< br>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.