Todos los equipos Nextel con Java ME
tienen la capacidad de establecer una conexión de datos por el puerto
serial o mini USB desde una aplicación Java.
En la terminal receptora, es simplemente necesario poder detectar al equipo conectado como si fuese un modem.
Es muy sencillo. El código adjunto ofrece un ejemplo.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import javax.microedition.io.CommConnection;
import java.io.IOException;
import java.io.DataOutputStream; public class SocketTest extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private Command exitCommand;
private Command okCommand;
private Form form;
private StringItem stringItem;
/**
* The SocketTest constructor.
*/
public SocketTest() {
}
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
*/
private void initialize() {
}
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
switchDisplayable(null, getForm());
}
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
}
/**
* Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
openSocketConnection();
}
}
}
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 0);
}
return exitCommand;
}
/**
* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {
form = new Form("Prueba", new Item[] { getStringItem() });
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
}
return form;
}
/**
* Returns an initiliazed instance of stringItem component.
* @return the initialized component instance
*/
public StringItem getStringItem() {
if (stringItem == null) {
stringItem = new StringItem("Prueba", "Prueba conexion serial");
}
return stringItem;
}
/**
* Returns an initiliazed instance of okCommand component.
* @return the initialized component instance
*/
public Command getOkCommand() {
if (okCommand == null) {
okCommand = new Command("Ok", Command.OK, 0);
}
return okCommand;
}
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
public void openSocketConnection() {
try {
CommConnection cc = (CommConnection)Connector.open("comm:com0;baudrate=19200");
DataOutputStream dos = cc.openDataOutputStream();
dos.writeChars("Sample output");
dos.flush();
cc.close();
}
catch (IOException ex){
Alert alert = new Alert("Exception: ", ex.getMessage() ,null,AlertType.ERROR);
switchDisplayable(alert,getDisplay().getCurrent());
}
}
}
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import javax.microedition.io.CommConnection;
import java.io.IOException;
import java.io.DataOutputStream; public class SocketTest extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private Command exitCommand;
private Command okCommand;
private Form form;
private StringItem stringItem;
/**
* The SocketTest constructor.
*/
public SocketTest() {
}
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
*/
private void initialize() {
}
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
switchDisplayable(null, getForm());
}
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
}
/**
* Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
openSocketConnection();
}
}
}
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 0);
}
return exitCommand;
}
/**
* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {
form = new Form("Prueba", new Item[] { getStringItem() });
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
}
return form;
}
/**
* Returns an initiliazed instance of stringItem component.
* @return the initialized component instance
*/
public StringItem getStringItem() {
if (stringItem == null) {
stringItem = new StringItem("Prueba", "Prueba conexion serial");
}
return stringItem;
}
/**
* Returns an initiliazed instance of okCommand component.
* @return the initialized component instance
*/
public Command getOkCommand() {
if (okCommand == null) {
okCommand = new Command("Ok", Command.OK, 0);
}
return okCommand;
}
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
public void openSocketConnection() {
try {
CommConnection cc = (CommConnection)Connector.open("comm:com0;baudrate=19200");
DataOutputStream dos = cc.openDataOutputStream();
dos.writeChars("Sample output");
dos.flush();
cc.close();
}
catch (IOException ex){
Alert alert = new Alert("Exception: ", ex.getMessage() ,null,AlertType.ERROR);
switchDisplayable(alert,getDisplay().getCurrent());
}
}
}
No hay comentarios.:
Publicar un comentario