1 /*
2 * Created on 6-Apr-2005
3 *
4 * To change the template for this generated file go to
5 * Window>Preferences>Java>Code Generation>Code and Comments
6 */
7 package ubc.midp.mobilephoto.sms;
8
9 import java.io.IOException;
10 import java.io.InterruptedIOException;
11
12 import javax.microedition.io.Connector;
13 import javax.microedition.io.PushRegistry;
14 import javax.wireless.messaging.BinaryMessage;
15 import javax.wireless.messaging.Message;
16 import javax.wireless.messaging.MessageConnection;
17 import javax.wireless.messaging.TextMessage;
18
19 import ubc.midp.mobilephoto.core.comms.BaseMessaging;
20
21 /**
22 * @author trevor
23 *
24 * Insert Comments here
25 *
26 */
27 public class SmsMessaging extends BaseMessaging {
28
29 //Port number for SMS message to be sent to:
30 //MobilePhoto should have a default port (to send direct to MobilePhoto app if the receiver is listening
31 //TODO: What is the default port for standard SMS messaging. So you can send messages to non-MobilePhoto users?
32 private String smsSendPort;
33 private String smsReceivePort = "1000"; //Use port=1000 as the default for MobilePhoto incoming SMS Messages
34 private String destinationPhoneNumber;
35
36 private String smsProtocolPrefix = "sms://";
37
38 private MessageConnection smsConn = null;
39 private Message msg;
40 private String[] connections; //list of active connections
41
42
43 public SmsMessaging() {
44 //Set some defaults
45 smsSendPort = "1000"; //Change this to whatever the standard SMS listen port is?
46 smsReceivePort = "1000";
47 }
48
49 public SmsMessaging(String smsDstPort, String destinationPhoneNumber) {
50 this.destinationPhoneNumber = destinationPhoneNumber;
51 this.smsSendPort = smsDstPort;
52 }
53
54 /* (non-Javadoc)
55 * @see ubc.midp.mobilephoto.core.comms.BaseMessaging#sendImage(byte[])
56 */
57 public boolean sendImage(byte[] imageData) {
58 boolean success = true;
59
60 String address = destinationPhoneNumber;
61 if ( (smsSendPort != null) && (smsSendPort != "") )
62 address = smsProtocolPrefix + address + ":" + smsSendPort+1;
63
64 System.out.println("SmsMessaging::sendImage: Sending binary message to: " + address);
65
66 MessageConnection smsconn = null;
67
68 try {
69
70 //Open the message connection.
71 smsconn = (MessageConnection) Connector.open(address);
72
73 //Prepare for send of binary data
74 BinaryMessage binmsg = (BinaryMessage)smsconn.newMessage( MessageConnection.BINARY_MESSAGE );
75
76 //**Device Specific Notes**
77 //Motorola only supports sending of a single segment, with a maximum of 132 bytes of data
78
79 binmsg.setPayloadData(imageData);
80
81 int i = smsconn.numberOfSegments(binmsg);
82 System.out.println("SmsMessaging::sendImage() num segments to send is: " + i);
83
84 smsconn.send(binmsg);
85
86 } catch (Throwable t) {
87 System.out.println("Send caught: ");
88 t.printStackTrace();
89 return false;
90 }
91
92 //Close any open connections and perform cleanup
93 cleanUpConnections(smsconn);
94
95 return success;
96 }
97
98 /* (non-Javadoc)
99 * @see ubc.midp.mobilephoto.core.comms.BaseMessaging#receiveImage()
100 */
101 public byte[] receiveImage() throws InterruptedIOException, IOException {
102
103 System.out.println("SmsMessaging::receiveImage() - start");
104 byte[] receivedData = null;
105 String smsConnection = smsProtocolPrefix + ":" + smsReceivePort;
106 String senderAddress;
107
108 if (smsConn == null) {
109 try {
110 smsConn = (MessageConnection) Connector.open(smsConnection);
111 } catch (IOException ioe) {
112 ioe.printStackTrace();
113 }
114 }
115
116 connections = PushRegistry.listConnections(true);
117 if (connections == null || connections.length == 0) {
118 System.out.println("Waiting for SMS on " + smsConnection + "...");
119 }
120
121 // Check for sms connection
122
123 //TODO: Use MessageListener interface to handle incoming messages,
124 //instead of blocking on the thread
125
126 //This will block until a message is received
127 System.out.println("DEBUG 1: before smsConn.receive():"+smsConn);
128 msg = smsConn.receive();
129 System.out.println("DEBUG 2: after smsConn.receive()");
130
131 if (msg != null) {
132 senderAddress = msg.getAddress();
133 System.out.println("From: " + senderAddress);
134
135 //Handle Text Message
136 if (msg instanceof TextMessage) {
137 String incomingMessage = ((TextMessage) msg).getPayloadText();
138 System.out.println("Incoming SMS Message with Payload:" + incomingMessage);
139
140 //Handle Binary Message
141 } else {
142 System.out.println("Incoming Binary SMS Message...");
143 StringBuffer buf = new StringBuffer();
144 receivedData = ((BinaryMessage) msg).getPayloadData();
145 System.out.println("SmsMessaging::receiveImage: sender address = " + senderAddress.toString());
146 System.out.println("SmsMessaging::receiveImage: buffer length = " + buf.length() + " contents = " + buf.toString());
147 }
148 }
149
150
151 System.out.println("SmsMessaging::receiveImage() - Finish");
152
153 //Return success if we reached this far
154 return receivedData;
155 }
156
157 /* (non-Javadoc)
158 * @see ubc.midp.mobilephoto.core.comms.BaseMessaging#cleanUpConnections()
159 */
160 public void cleanUpConnections(MessageConnection smsConn) {
161
162 //Cleanup the connection
163 if (smsConn != null) {
164 try {
165 smsConn.close();
166 } catch (IOException ioe) {
167 System.out.println("Closing connection caught: ");
168 ioe.printStackTrace();
169 }
170 }
171
172 }
173
174 public void cleanUpReceiverConnections() {
175
176 //Cleanup the connection
177 if (smsConn != null) {
178 try {
179 smsConn.close();
180 smsConn = null;
181 } catch (IOException ioe) {
182 System.out.println("Closing connection caught: ");
183 ioe.printStackTrace();
184 }
185 }
186
187 }
188 /**
189 * @return Returns the destinationPhoneNumber.
190 */
191 public String getDestinationPhoneNumber() {
192 return destinationPhoneNumber;
193 }
194 /**
195 * @param destinationPhoneNumber The destinationPhoneNumber to set.
196 */
197 public void setDestinationPhoneNumber(String destinationPhoneNumber) {
198 this.destinationPhoneNumber = destinationPhoneNumber;
199 }
200 /**
201 * @return Returns the smsReceivePort.
202 */
203 public String getSmsReceivePort() {
204 return smsReceivePort;
205 }
206 /**
207 * @param smsReceivePort The smsReceivePort to set.
208 */
209 public void setSmsReceivePort(String smsReceivePort) {
210 this.smsReceivePort = smsReceivePort;
211 }
212 /**
213 * @return Returns the smsSendPort.
214 */
215 public String getSmsSendPort() {
216 return smsSendPort;
217 }
218 /**
219 * @param smsSendPort The smsSendPort to set.
220 */
221 public void setSmsSendPort(String smsSendPort) {
222 this.smsSendPort = smsSendPort;
223 }
224 }
|