Yougeshwar Khatri
Yougeshwar Khatri

Reputation: 41

How to Receive SMS Messages using Javax.Comm Serial Event Listener

I'm writing a program in Java to send and receive SMS text messages. I'm using AT commands and a bluetooth connection with my Nokia set. I've written a class to send messages. But i can't figure out how to get java serial events to notify me when i have received a text message.

To receive messages at the moment I'm writing the appropriate AT commands to the phone, then I've written a class to send a newline statement to the phone every 10 seconds this displays any new messages.

I would really prefer to handle incoming messages using serial events. Any information on how to do this, or Java code would be hugely appreciated.

Upvotes: 4

Views: 1021

Answers (1)

Donald_W
Donald_W

Reputation: 1823

Take a look at org.smslib: http://smslib.org/

Example use of that library here: https://groups.google.com/forum/#!topic/smslib/6b4dR5pJjBY

Alternatively, if you really need to do it using javax.commm alone - some example code to get you started is here:

http://www.java2s.com/Code/JavaAPI/javax.comm/SerialPortaddEventListenerSerialPortEventListenerarg0.htm

In particular:

You need to call SerialPort.addEventListener(SerialPortEventListener arg0) and then serialPort.notifyOnDataAvailable(true);

When this is setup you can then act on SerialPortEventListener's callback like so:

public void serialEvent(SerialPortEvent event) {
  switch (event.getEventType()) {
  case SerialPortEvent.BI:
  case SerialPortEvent.OE:
  case SerialPortEvent.FE:
  case SerialPortEvent.PE:
  case SerialPortEvent.CD:
  case SerialPortEvent.CTS:
  case SerialPortEvent.DSR:
  case SerialPortEvent.RI:
  case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
  case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[20];

    try {
      while (inputStream.available() > 0) {
        int numBytes = inputStream.read(readBuffer);
      }   
      System.out.print(new String(readBuffer));
    } catch (IOException e) {
    }   
    break;
  }   
}

Upvotes: 1

Related Questions