10-13-2011 09:57 AM
I created a simple app to push an SMS to another phone when I get an incoming SMS or phone call. The code works for the incoming SMS, but when a call comes in "callIncoming()" never fires. Targeting BB OS 5 phones and greater, testing on BB Style. Can anyone spot my issue?
public class TickleMeApplication extends UiApplication implements MessageListener, PhoneListener {
private TheMainScreen theMainScreen;
private MessageConnection _mc;
private final String toPhone = "5558675309";
public static void main(String[] args)
{
TickleMeApplication theApp = new TickleMeApplication();
theApp.enterEventDispatcher();
}
public TickleMeApplication()
{
theMainScreen = new TheMainScreen();
pushScreen(theMainScreen);
try {
_mc = (MessageConnection)Connector.open("sms://:0");
_mc.setMessageListener(this);
Phone.addPhoneListener(this);
Log("Listeners started");
} catch (IOException e) {
Log("ERROR: " + e.getMessage());
}
}
private byte[] stringToByte(String s)
{
char[] sa = s.toCharArray();
byte[] ba = new byte[sa.length];
for (int i = 0; i < ba.length; i++) {
ba[i] = (byte) (sa[i] & 0xFF);
}
return ba;
}
private void sendCDMAText(String nr, String message) throws IOException
{
DatagramConnection conn = (DatagramConnection) Connector.open("sms://+" + nr + ":5016");
byte[] bytes = stringToByte(message);
Datagram msg = conn.newDatagram(bytes, bytes.length);
conn.send(msg);
}
private void sendSMS(String nr, String message) throws IOException
{
MessageConnection conn = (MessageConnection) Connector.open("sms://" + nr);
TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(message);
conn.send(msg);
}
private void sendTextMessage(String nr, String message) throws IllegalArgumentException, InterruptedIOException, NullPointerException, SecurityException, IOException
{
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA)
sendCDMAText(nr, message);
else
sendSMS(nr, message);
}
private void Log(final String mess) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (theMainScreen.getFieldCount() > 10)
theMainScreen.deleteAll();
theMainScreen.add(new RichTextField(mess));
}
});
}
public void notifyIncomingMessage(MessageConnection conn) {
Message m;
try {
Log("Received SMS");
m = _mc.receive();
String address = m.getAddress();
String msg = null;
if ( m instanceof TextMessage ) {
TextMessage tm = (TextMessage)m;
msg = tm.getPayloadText();
Log("SMS: " + address + " " + msg);
sendTextMessage(toPhone, "SMS: " + address + " " + msg);
}
} catch (InterruptedIOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void callIncoming(int callId) {
Log("Incoming call " + callId);
PhoneCall callInfo = Phone.getCall(callId);
if ( callInfo != null ) {
try {
Log("From " + callInfo.getDisplayPhoneNumber());
sendTextMessage(toPhone, "Incoming call from " + callInfo.getDisplayPhoneNumber());
} catch (Exception e) {
e.printStackTrace();
Log("Error " + e.getMessage());
}
}
}
public void callAdded(int arg0) {}
public void callAnswered(int callId) {}
public void callConferenceCallEstablished(int callId) {}
public void callConnected(int callId) {}
public void callDirectConnectConnected(int callId) {}
public void callDirectConnectDisconnected(int callId) {}
public void callDisconnected(int callId) {}
public void callEndedByUser(int callId) {}
public void callFailed(int callId, int reason) {}
public void callHeld(int callId) {}
public void callInitiated(int callid) {}
public void callRemoved(int callId) {}
public void callResumed(int callId) {}
public void callWaiting(int callid) {}
public void conferenceCallDisconnected(int callId) {}
}
final class TheMainScreen extends MainScreen
{
public TheMainScreen()
{
super();
LabelField title = new LabelField("Tickle Me", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose()
{
System.exit(0);
return true;
}
}
10-13-2011 10:06 AM
10-13-2011 10:13 AM - edited 10-13-2011 10:16 AM
Argh....works fine in the debugger. Now I really haven't a clue.
10-13-2011 10:25 AM
10-13-2011 10:30 AM
I am thinking it has something to do with trying to log messages to the UI. In my Log() function is that the proper way to update the screen? The debugger with that code in is crashing out with something about an event lock.
10-13-2011 10:38 AM
01-25-2012 09:30 AM
Hi,
I'm having exactly the same problem on os 6 and I'm definitely not doing anything that could potentially throw an exception or cause the listener to stop working. When I receive a phonelistener event, I simply post the event to my app's event thread using invokelater. I'm also certain that its not just a matter of my app not running.
One theory I have is that the phoneapp stops running and as a result loses the list of registered listeners. If this were the case, would there be a way of detecting that it has exited and restarted. Obviously I could use a timer to periodically re-register my listener, but this would not be very elegant.
01-25-2012 09:38 AM
01-25-2012 09:41 AM
Thanks for the reply, just out of curiosity, why do you recommend against using invokelater?
01-25-2012 09:47 AM