hi, all
the code bellow is to establish a usb connection between phone and pc, can text from pc to phone using hyperterminal, but how to text from phone to pc,does anyone know that?




import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

/**
*
* MIDlet reads System properties Comm ports
* opens CommConnection to port COM0
* attempts to get and set Baud rate
* writes text string over CommConnection
* then listens and displays bytes received from CommConnection.
*
*/

public class HelloComComm extends MIDlet implements CommandListener {

private Command exitCommand; // The exit command
private Display display; // The display for this MIDlet
private TextBox t;
public Object sync = new Object();
private CommConnection comm = null;
private InputStream is = null;
private OutputStream os = null;

public HelloComComm() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
}

/**
* Start up the MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp()
{
int i;
t = new TextBox("HelloComComm", "", 8000, 0);
t.addCommand(exitCommand);
t.setCommandListener(this);
display.setCurrent(t);
try
{
try
{
Thread.sleep(3000);
}
catch(InterruptedException ie) { }
Tracer.outln("HelloComComm : testing CommConnection for COM ports", t);
Tracer.outln("Calling System.getProperty(microedition.commports)", t);
String ports = System.getProperty("microedition.commports");
Tracer.outln("got ports="+ports, t);
i = ports.indexOf("COM", 0);
if(i==-1)
{
throw new RuntimeException("No COM port found in System Property");
}
System.out.println("System.out redirection ACTIVE!");
Tracer.outln("Going to call Connector.open(comm:com0)", t);
Tracer.outln("In case of using RS-232: plug cable now! If it is already plugged remove it and restart MIDlet.", t);
comm = (CommConnection)Connector.open("comm:USB2");
Tracer.outln("Calling getBaudRate()", t);
int orgBaudRate = comm.getBaudRate();
Tracer.outln("Calling setBaudRate()", t);
int oldBaudRate = comm.setBaudRate(9600 /* dummy */);
Tracer.outln("Calling getBaudRate()", t);
int newBaudRate = comm.getBaudRate();
if(orgBaudRate != newBaudRate) throw new RuntimeException("BaudRate changed!");

Tracer.outln("Calling openOutputStream()", t);
os = comm.openOutputStream();
Tracer.outln("writing to COM0", t);
String text = "Hello ComComm! Anybody there at COM0?...";
os.write(text.getBytes());

Tracer.outln("Calling openInputStream()", t);
is = comm.openInputStream();
Tracer.outln("attempting read upto 500 bytes from COM0 (quits when received string: EXIT)", t);
Tracer.outln("read:>", t);
byte[] buffer = new byte[500];
for(i=0;i<500
{
try
{
Thread.sleep(10);
}
catch(InterruptedException ie) { }
int available = is.available();
if(available==0)
{
continue;
}
int count = is.read( buffer, i, available);
if(count>0)
{ String outText = new String(buffer, i, count);
Tracer.out(outText, t);
i=i+count;
}
String total = new String(buffer,0,i);
if ((i>3)&&(-1 != total.indexOf("EXIT")))
{
Tracer.outln("<quitting.", t);
break;
}
}
Tracer.outln("Calling OutputStream.close()", t);
os.close();
Tracer.outln("Calling InputStream.close()", t);
is.close();
Tracer.outln("Calling connection.close()", t);
comm.close();
} catch (NullPointerException e) {
// red key pressed
} catch (Exception e) {
Tracer.outln("FAILED with"+e.getClass()+":"+e.getMessage(), t);
return;
}
System.out.println("CommConnection closed.");
Tracer.outln("SUCCEEDED.", t);
}

public void pauseApp() {
}

/**
* Destroy must cleanup everything not handled by the garbage collector.
*/
public void destroyApp(boolean unconditional) {
}

/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
Tracer.outln("Exiting...", t);
destroyApp(false);
notifyDestroyed();
}
}
}

class TestFailedException extends IOException
{
public TestFailedException() {;}
public TestFailedException(String s) {super(s);}
}

class Tracer
{
private static TextBox myTextBox;
public Tracer (TextBox t)
{
myTextBox = t;
}
public static void out (String msg)
{
myTextBox.setString((myTextBox.getString()).concat("\n"+msg));
}
public static void outln (String msg, TextBox t)
{
t.setString((t.getString()).concat("\n"+msg));
}
public static void out (String msg, TextBox t)
{
t.setString((t.getString()).concat(msg));
}
}