© 2014 Samsung Electronics Co., Ltd. All rightsreserved.
Learning how to request Samsung accessory (SA) agents and connect to remote peer agents is a basic SAP management skill:
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);
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);
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);
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 };
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();