简体   繁体   English

如何使用Javax.Comm串行事件侦听器接收SMS消息

[英]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. 我正在用Java编写程序来发送和接收SMS文本消息。 I'm using AT commands and a bluetooth connection with my Nokia set. 我正在使用AT命令和我​​的诺基亚设备的蓝牙连接。 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. 但是当我收到短信时,我无法弄清楚如何让java串行事件通知我。

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. 为了在我正在向手机写入适当的AT命令的那一刻接收消息,然后我写了一个类,每隔10秒向手机发送一行换行语,显示任何新消息。

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. 任何有关如何执行此操作或Java代码的信息都将非常受欢迎。

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

Example use of that library here: https://groups.google.com/forum/#!topic/smslib/6b4dR5pJjBY 此处使用该库的示例: https//groups.google.com/forum/#!topic / swslib / 6b4dR5pJjBY

Alternatively, if you really need to do it using javax.commm alone - some example code to get you started is here: 或者,如果你真的需要单独使用javax.commm - 这里有一些示例代码可以帮到你:

http://www.java2s.com/Code/JavaAPI/javax.comm/SerialPortaddEventListenerSerialPortEventListenerarg0.htm 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); 您需要调用SerialPort.addEventListener(SerialPortEventListener arg0)然后调用serialPort.notifyOnDataAvailable(true);

When this is setup you can then act on SerialPortEventListener 's callback like so: 设置完成后,您可以对SerialPortEventListener的回调进行操作,如下所示:

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;
  }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM