Samsung Electronics logo

Exchanging Files with Peer Agents



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

  1. To get the SAFileTransfer object specified for the SA agent, use the getSAFileTransfer() method:

    /* SASocket object has been obtained using the setServiceConnectionListener() method */
       
    var filetransfer = SAAgent.getSAFileTransfer();
  2. To send a file to a remote peer agent:

    1. Define the event handlers for file transfer notifications using the SAFileSendCallback listener interface:

      var sendfilecallback = 
      {
         onprogress: function(transferId, progress)
         { 
            console.log("onprogress transferId: " + transferId + ", progress : " + progress);
         },
         oncomplete: function(transferId, localPath)
         { 
            console.log("File transfer complete.  transferId: " + transferId);
         },
         onerror: function(errorCode, transferId)
         { 
            console.log("FileSendError transferId: " + transferId + " code : " + errorCode);
         }
      }
      
    2. To send the file, use the sendFile() method :

      var filePath = "file:///opt/usr/media/Downloads/Image.jpg";
      var transferId = null;
      var filetransfer = null;
      
      var peeragentfindcallback = 
      {
         onpeeragentfound: function(peerAgent) 
         {
            if (peerAgent.appName == "expected app name") 
            {
               transferId = filetransfer.sendFile(peerAgent, filePath);
            } 
         }
      }
      
      var sendfilecallback = 
      {
         onprogress: function(transferId, progress)
         { 
            console.log("onprogress transferId: " + transferId + ", progress: " + progress);
         },
         oncomplete: function(transferId, localPath)
         { 
            console.log("File transfer complete.  transferId: " + transferId);
         },
         onerror: function(errorCode, transferId)
         { 
            console.log("FileSendError transferId: " + transferId + " code : " + errorCode);
         }
      };
      
      filetransfer = agent.getSAFileTransfer();
      filetransfer.setFileSendListener(sendfilecallback);
      agent.setPeerAgentFindListener(peeragentfindcallback);
      agent.findPeerAgents();
      
  3. To receive a file from a remote peer agent:

    1. Define the event handlers for process notifications using the SAFileReceiveCallback listener interface:

      var receivefilecallback = 
      {
         onreceive: function(transferId, fileName)
         { 
            console.log("Incoming file transfer request form the remote peer agent. 
                        transferId: " + transferId + " file name : " + fileName); 
         },
         onprogress: function(transferId, progress)
         { 
            console.log("onprogress transferId: " + transferId + ", progress : " + progress);
         },
         oncomplete: function(transferId, localPath)
         { 
            console.log("File transfer complete.  transferId: " + transferId); 
         },
         onerror: function(errorCode, transferId)
         { 
            console.log("FileReceiveError transferId: " + transferId + " code : " + errorCode);
         } 
      }
      
      filetransfer.setFileReceiveListener(receivefilecallback);
      
    2. To accept the download of the file, use the receiveFile() method:
      var newFilePath = "file:///opt/usr/media/Downloads/ReceivedImage.jpg";
      
      var receivefilecallback = 
      {
         onreceive: function(transferId, fileName) 
         {
            filetransfer.receiveFile(transferId, newFilePath);
         }
      }
    3. To reject the incoming transfer request from a remote peer agent, use the rejectFile() method:

      var receivefilecallback = 
      {
         onreceive: function(transferId, fileName) 
         {
            if (1) /* User-defined condition */ 
            {
               filetransfer.rejectFile(transferId);
            }
         },
      }
    4. To cancel the download, use the cancelFile() method with the transfer ID:

      var receivefilecallback = 
      {
         onprogress: function(transferId, progress)
         { 
            filetransfer.cancelFile(transferId);
         }
      }