Samsung Electronics logo

Setting up a Connection



Learning how to request Samsung accessory (SA) agents and connect to remote peer agents is a basic SAP management skill:

  1. To get the SA agent specified in an accessory service profile, use the requestSAAgent() method:

    var SAAgent;
    function onsuccess(agents) 
    {
       SAAgent = agents[0];
       for (var i = 0; i < agents.length; i++) 
       {
          console.log(i + ". " + agents[i].name);
          /* Process the SA agents */
       }
    }
    
    webapis.sa.requestSAAgent(onsuccess, onerror); 
    
  2. Define an event handler for peer device status notifications using the SADeviceStatusCallback listener interface and set the listener to the SAAgent object. This handler is used to notify the user when the host and Wearable devices are disconnected (DETACHED) or connected (ATTACHED).

    Note
    When the status is ATTACHED, you must call the findPeerAgents() method to establish a new SA socket connection.
    function ondevicestatus(type, status) 
    {
       if (status == "ATTACHED") 
       {
          console.log("Attached remote peer device. : " + type);
          SAAgent.findPeerAgents();
       } 
       else if (status == "DETACHED") 
       {
          console.log("Detached remote peer device. : " + type);
       }
    }
    
    webapis.sa.setDeviceStatusListener(ondevicestatus);
    
  3. Define an event handler for connection event notifications using the ServiceConnectionCallback listener interface:

    Note
    In case of the autoLaunchAppId attribute, if a peer agent requests a service connection to the service profile while it is not running, the system launches your application and calls the onrequest() event handler of the ServiceConnectionCallback listener interface.
    var SASocket;
    var connectioncallback = 
    {
       /* Remote peer agent requests a service connection */
       onrequest: function(peerAgent)
       {
          if (peerAgent.appName == "expected app name") 
          {
             SAAgent.acceptServiceConnectionRequest(peerAgent);
          } 
          else 
          {
             SAAgent.rejectServiceConnectionRequest(peerAgent);
          }
       },
       /* Connection between provider and consumer is established */
       onconnect: function(socket)
       {
          SASocket = socket;
       }
    }
    
    SAAgent.setServiceConnectionListener(connectioncallback);
    
  4. Define an event handler for finding the remote peer agent using the SAPeerAgentFindCallback listener interface. To establish a service connection with a remote peer agent, use the requestServiceConnection() method.

    The listener is invoked once for each found peer.

    Note
    The onpeeragentupdated() event listener is mostly used on the host SA to detect the availability of a Wearable SA. The AVAILABLE status means that the corresponding Wearable SA is installed.
    function onpeeragentfound(peerAgent) 
    {
       if (peerAgent.appName == "expected app name") 
       {
          SAAgent.requestServiceConnection(peerAgent);
       }
    }
    
    function onpeeragentupdated(peerAgent, status) 
    {
       if (status == "AVAILABLE") 
       {
          SAAgent.requestServiceConnection(peerAgent);
       }
       else if (status == "UNAVAILABLE") 
       {
          console.log("Uninstalled application package of peerAgent on remote device.");
       }
    }
    
    var peeragentfindcallback = 
    {
       onpeeragentfound: onpeeragentfound,
       onpeeragentupdated: onpeeragentupdated
    };
    
  5. To retrieve a list of peer agents, set the listener with the defined event handlers, and use the findPeerAgents() method of the SAAgent interface.

    SAAgent.setPeerAgentFindListener(peeragentfindcallback);
    SAAgent.findPeerAgents();