Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
timnelson
Posts: 102
Registered: ‎07-15-2008
My Device: Not Specified

PhoneListener not firing on incoming call

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

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: PhoneListener not firing on incoming call

i don't see the issue. did you try it on a simulator with breakpoints set?
for better readability (and easier debugging) i would suggest to put the listeners into own classes. you can then extend abstractphonelistener to have the class a bit more tidy.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
timnelson
Posts: 102
Registered: ‎07-15-2008
My Device: Not Specified

Re: PhoneListener not firing on incoming call

[ Edited ]

Argh....works fine in the debugger. Now I really haven't a clue.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: PhoneListener not firing on incoming call

I have a safeguard against some errors i experienced in my app.
On callDisconnected and callFailed i remove the phonelistener and add it again.
Just in case you have the same strange issue i encountered some years ago, give it a try.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
timnelson
Posts: 102
Registered: ‎07-15-2008
My Device: Not Specified

Re: PhoneListener not firing on incoming call

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.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: PhoneListener not firing on incoming call

the phonelistener runs in the context of the phone application.
if you want to interact with your regular application, the best way is to use global events.

don't do any heavy processing in the listener itself, if it blocks it can kill the phone app (which is restarted by a watchdog, but still not good).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
hannsl
Posts: 5
Registered: ‎12-03-2011
My Device: 9800

Re: PhoneListener not firing on incoming call

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.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: PhoneListener not firing on incoming call

i'd suggest that you use global events to post the events, not invokelater.
if that does not help, no clue, probably open an own thread with the problem.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
hannsl
Posts: 5
Registered: ‎12-03-2011
My Device: 9800

Re: PhoneListener not firing on incoming call

Thanks for the reply, just out of curiosity, why do you recommend against using invokelater?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: PhoneListener not firing on incoming call

because you use the event thread of the phone application to do stuff, which is not a good idea.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter