import javax.wireless.messaging.*; import javax.microedition.io.Connector; import java.io.IOException; import java.io.InterruptedIOException; class SmsReceiver extends Thread { Runnable listener = null; byte[] data = null; volatile MessageConnection conn = null; IOException exception = null; int port; SmsReceiver(int port, Runnable listener) { this.listener = listener; this.port = port; } void close() { if (conn != null) { try { conn.close(); } catch (IOException e) { } conn = null; } } /* void notifyIncomingMessage(MessageConnection conn) { } */ /* void receiveOnce() { //conn.setMessageListener(this); data = null; new Thread(this).start(); } */ public void run() { try { conn = (MessageConnection) Connector.open("sms://:" + port); while (true) { exception = null; data = ((BinaryMessage) conn.receive()).getPayloadData(); M.callSerially(listener); } } catch (InterruptedIOException e) { //connection was closed, ok, nothing to do } catch (IOException e) { exception = e; data = null; close(); M.callSerially(listener); } } }