Skip to content

Receiving calls

juzna edited this page Jun 21, 2011 · 3 revisions

Receiving a call

When a call state is changed in general, an event is fired. It's the same event when your dialed number started ringing on the other side, when somebody called you and phone is ringing in your browser, or when a call has been terminated. This event is called callStateChanged and as an argument, call state is passed to whatever method you assign as event handler.

// Firefox, Chrome and other browsers; for IE you must use something else
phone.addEventListener('callStateChanged', function(call, state, msg) {
  console.log('Call state changed:', call, state, msg);
  if(state == 1) { // 1 = IncomingReceived
    displayPrompt({ // assuming displayPrompt is your method
      msg: 'Answer call?',
      onOK: function() { call.accept(); }
    });
  }
 
  else if(state == 13) { // 13 = End
    alert('Call terminated');
  }
});

This will attach an event listener, a method which will be executed every time call state of any call gets changed. First argument is the call object (see Placing calls), on which you can operate. Second parameter is call state represented as a number, telling you what is right now happening to your call. Last parameter is phone state as a string, which could be nice to be displayed to the user, but you shouldn't depend on it. All call states are listed below.

Call states:

For all possible states of a call, see Call states.