Académique Documents
Professionnel Documents
Culture Documents
Chapter 09
Optional Packages
9.1 Objectives
In this section, we will be delving into writing, building, using the emulator and packaging
J2ME applications. The Integrated Programming Environment that we will be using is
Netbeans (www.netbeans.org).
After finishing this lesson, the student should be able to:
know the functionalities provided by the Mobile Media API
play simple tones
play an audio file from the network and from the JAR
send and receive SMS messages
9.2 Overview
Not all devices are created equal and each device class comes with different features. It
would be very difficult to create a standard specification that encompass all the existing
devices.
To accomodate the different capabilities of devices, MIDP has defined several optional
packages. These packages are specific to and cater only to the specific device feature.
In this section, we will be discussing how to start using the Mobile Media API (MMAPI) and
the Wireless Messaging API (WMA).
J.E.D.I.
The DataSource handles the details of getting the data from the source. The source may
be a file from the JAR or from the network (via HTTP), a record from the RMS, a streaming
connection from a server or other proprietary source. The Player does not have to worry
about where the data came from or the manner in which it would be fetched. All the Player
has to do is to read the data from the DataSource, process and display or playback the
media to the output device.
A third actor in our scene is the Manager. The Manager creates players from DataSources.
The Manager has methods to create Players from media locators (URL locations),
DataSources and InputStreams.
You can query MMAPI properties via the String System.getProperty(String key).
Key
Description
microedition.media.version
supports.mixing
J.E.D.I.
Key
Description
supports.audio.capture
supports.video.capture
supports.recording
9.3.1
Tone generation
Playing a tone is as simple as calling the static method Manager.playTone(int tone, int
duration, int volume). The valid values for the tone is from 0 to 127. The duration of the
playing of the tone is specified in milliseconds. The volume parameter is from 0 to 100.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
public ToneMIDlet() {
playCommand = new Command("Play", Command.OK, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
volumeGauge = new Gauge("Volume", true, MAX_VOLUME, volume);
J.E.D.I.
9.3.2
Audio playback
The convenience method Manager.createPlayer(String URI) creates a player which will play
the data from the URI.
J.E.D.I.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
public NetAudioMidlet() {
playCommand = new Command("Play", Command.OK, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
volumeGauge = new Gauge("Volume", true, MAX_VOLUME, volume);
try {
player = Manager.createPlayer(
"http://localhost:8084/Chapter07/bong.wav");
player.realize();
J.E.D.I.
player.prefetch();
} catch (IOException ioex) {
display.setCurrent(new Alert("IO Exception",
ioex.getMessage(),
null, AlertType.ERROR));
}
}
}
player.start();
}
null, AlertType.ERROR));
null, AlertType.ERROR));
}
}
}
}
You can also play media from a file in the JAR by creating a Stream from the file resource
J.E.D.I.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
public AudioMidlet() {
playCommand = new Command("Play", Command.OK, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
volumeGauge = new Gauge("Volume", true, MAX_VOLUME, volume);
J.E.D.I.
if (c == exitCommand) {
notifyDestroyed();
}
if (c == playCommand){
try {
InputStream stream = getClass().
getResourceAsStream("bong.wav");
player = Manager.createPlayer(stream, "audio/x-wav");
player.realize();
player.start();
}
null, AlertType.ERROR));
}
}
}
}
Sending SMS
Using the Wireless Messaging API is very similar to connection via Sockets and Datagrams.
In fact, it uses the same framework the Generic Connection Framework (GCF). The
connection URL format to use is "sms://+639178888888", where "+639178888888" is the
number of the phone you want to send messages to.
J.E.D.I.
number;
MessageConnection connection =
(MessageConnection) Connector.open(url);
TextMessage msg = (TextMessage) connection.newMessage(
MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(message);
connection.send(msg);
connection.close();
}
Development of wireless applications on Netbeans 6.5 is very convenient. You don't have
to send actual SMS messages just to test your application. Netbeans (with mobility pack)
comes with J2ME Wireless Toolkit. This toolkit comes with an emulator. It also includes
tools for testing sending and receiving SMS messages. You can configure the phone
number (address) of the emulated phone by opening the WMA preferences.
Tools
Java Platforms
Sun Java Wireless Toolkit 2.5.2 for CDLC
Tools and Extensions
Open Preferences -> WMA
Open Utilities -> WMA: Open Console
J.E.D.I.
10
J.E.D.I.
11
J.E.D.I.
12
J.E.D.I.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
public SMSMidlet() {
sendCommand = new Command("Send", Command.OK, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
13
J.E.D.I.
notifyDestroyed();
}
if (c == sendCommand) {
Thread thread = new Thread( this );
thread.start();
}
}
/**
* Sends an SMS message to number. This method will throw an exception
* if there is an error in connecting or sending the message.
* @param number
* @param message
*/
public void sendSMS(String number, String message) throws Exception{
String url = "sms://" +
number;
MessageConnection connection =
(MessageConnection) Connector.open(url);
TextMessage msg = (TextMessage) connection.newMessage(
MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(message);
connection.send(msg);
connection.close();
}
14
J.E.D.I.
15
J.E.D.I.
16
J.E.D.I.
17
J.E.D.I.
9.4.2
Receiving SMS
conn.setMessageListener(this);
The notifyIncomingMessage will be called by the AMS once a message is received by the
device. We will need to create a separate Thread for reading messages so that the Listener
callback method can exit immediately.
18
J.E.D.I.
// Received a message
// Check if this is a text message (and not MMS, etc.)
if (mesg != null && mesg instanceof TextMessage) {
TextMessage text = (TextMessage) mesg;
addressField.setText(text.getAddress());
mesgField.setText(text.getPayloadText());
dateField.setText("" + text.getTimestamp());
statusField.setText("Message received.");
}
} catch (Exception e) {
statusField.setText("Error: " + e.getMessage());
}
thread = null;
}
Here is the complete source code listing for our SMS Receiver:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
public SMSReceiverMidlet() {
exitCommand = new Command("Exit", Command.EXIT, 1);
19
J.E.D.I.
startReceiver();
display.setCurrent(form);
}
20
J.E.D.I.
conn.setMessageListener(this);
statusField.setText(
"waiting for message at port " + port);
}
} catch (Exception ex){
statusField.setText("Cannot open connection on port "
+ port + ":" + ex.getMessage());
}
// Received a message
// Check if this is a text message (and not MMS, etc.)
if (mesg != null && mesg instanceof TextMessage) {
TextMessage text = (TextMessage) mesg;
addressField.setText(text.getAddress());
mesgField.setText(text.getPayloadText());
dateField.setText("" + text.getTimestamp());
statusField.setText("Message received.");
} else {
21
J.E.D.I.
statusField.setText(
"Non-text message received: "
+ mesg.getClass().toString());
}
} catch (Exception e) {
statusField.setText("Error: " + e.getMessage());
}
thread = null;
}
22
J.E.D.I.
23
J.E.D.I.
9.5 Exercises
24
J.E.D.I.
9.5.1
Audio Player
Create a MIDlet that will play an audio file for an idefinite number of times (loop). The
audio file should be read from the JAR. Hint: you must set a property on the Player to
control the looping.
9.5.2
SMS Auto-Responder
Create a MIDlet that will automatically reply when it receives a text message. Hint: you
can modify the SMSReceiverMidlet and use the same connection to send the reply
message.
25