Samsung Electronics logo

Exchanging Data with Peer Agents



Learning how to exchange string data with peer agents is a basic SAP management skill:

  1. To send a string message to a remote peer agent, use the sendData() method of the SASocket interface with the channel ID:

    /* SAAgent object has been obtained using the requestSAAgent() method */
    var SASocket;
    var connectioncallback = 
    {
       /* Connection between provider and consumer is established */
       onconnect: function(socket) 
       {
          SASocket = socket;
          for (var i = 0; i < SAAgent.channelId.length; i++) 
          {
             SASocket.sendData(SAAgent.channelIds[i], "send message to " + i + "th channel.");
          }
       }
    }
    
    SAAgent.setServiceConnectionListener(connectioncallback);
    

    To send an encrypted string message to a remote accessory peer agent, use the sendSecureData() method instead of the sendData() method:

    SASocket.sendSecureData(SAAgent.channelIds[i], "send message to " + i + "th channel.");
    
  2. To receive a string message from a remote peer agent, use the setDataReceiveListener() method. This method registers the SADataReceiveCallback listener interface, which is invoked when a string message from a peer agent is received.

    function onreceive(channelId, data) 
    {
       console.log("Message received - " + channelId + " : " + data); 
    }
    
    SASocket.setDataReceiveListener(onreceive);