addConnectionListener( ConnectionStateListener, [containingObject] ) method

Register the connection state listener.

Syntax

<static> addConnectionListener( ConnectionStateListener, [containingObject] )

Parameters

Name Type Argument Description
ConnectionStateListener anonymous.ConnectionStateListener   Callback for connection state changes.
containingObject Object (optional) Object containing definition for ConnectionStateListener.If a connection state callback function references variables in its containing object, then the containing object should be passed to this function.

Example

// doSomething is a global function that gets called from the connection listener.
var doSomething = function()
{
   alert("sample function that gets executed when the hwc becomes connected");
}
// connectionListener is the callback function that is given to addConnectionListener.
// When there is a connection event, connectionListener will be invoked with the details.
var connectionListener = function( event, errorCode, errorMessage )
{
   if( event == hwc.CONNECTED )
   {
      doSomething();
   }
}
hwc.addConnectionListener( connectionListener );
// connectionStateManager is an object that will contain the connection listener callback as well as
// a variable used by the callback.
var connectionStateManager = {};
// The connectionStateManager keeps track of whether the HWC is connected or not.
connectionStateManager.connected = false;
// A function called by the listener.
connectionStateManager.doSomething = function()
{
   if( this.connected )
   {
      alert("this alert gets displayed if the hwc is connected");
   }
}
// This is the callback function that will be passed to addConnectionListener.  This callback references variables
// from the containing object (this.connected and this.doSomething), so when we call addConnectionListener we have
// to give the containing object as the second parameter.
connectionStateManager.listener = function( event, errorCode, errorMessage )
{
   if( event == hwc.CONNECTED )
   {
      this.connected = true;
   }
   else
   {
      this.connected = false;
   }
   this.doSomething();
}
// Pass both the listener and the containing object.  This enables the listener to refer to variables in the containing object when it is invoked.
hwc.addConnectionListener( connectionStateManager.listener, connectionStateManager );

Source

hwc-api.js, line 370.