comunicación en serie mediante CommConnection en un dispositivo móvil

I have two issues with a serial program on mobile device:

  1. InputStream.available () always returns 0 according to documentation. Can't use DataInputStream because the data might be a file and not a string. If I simply call the read() on the InputStream it'll block the IO operation if the byte(s) doesn't exist on the other side. In addition, I don't know how much is the size of input data. So how do I find if data is available at port and how much is the size of available data?
  2. I'm using hyperterminal to test the serial port and mobile is only responding to Comandos AT, like ata and atd. So any string like "hello" is ignored and my app can't see it. so is this true? or I'm missing something? how can I make the data visible to my app?

well any suggestion? code snippets maybe?

preguntado el 31 de enero de 12 a las 08:01

your API reference looks slippery - it's for Java SE. For Java ME MIDP code running on mobile, reference has to be to JSR 118 API, like this: -

yes, thank you for that! -

1 Respuestas

You can add a Listener to a SerialPort that gets notified whenever data is available on that port. Inside this callback, in.available() should also return the number of bytes that can be read, but it is probably better to just consume bytes until read return -1.

final CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");

final SerialPort com1 = (SerialPort)"Test", 1000);
final InputStream in = com1.getInputStream();

com1.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
com1.addEventListener(new SerialPortEventListener() {
    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                byte[] buffer = new byte[4096];
                int len;
                while (-1 != (len = {
                    // do something with buffer[0..len]
            } catch (IOException ex) {

Respondido el 31 de enero de 12 a las 14:01

there's no CommPortIdentifier in Java ME MIDP API - nor many other classes you refer to. MIDP applications (MIDlets) are expected to use API - mosquito

@gnat, Sorry, I missed the java-me tag since the post did not mention ME explicitly and linked to the SE api for InputStream. - Jörn Horstmann

guys i fixed the reference. so what can i do about those problems specially the second one? - jim

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.