© 2014 Samsung Electronics Co., Ltd. All rights reserved.
The Samsung Accessory Service Framework allows service discovery and the creation of Accessory Service Connections between Application Level Entities (ALE) for data exchange using the Samsung Accessory Protocol(SAP). This functionality is available through the SAP Service API.
The entities in the Samsung Accessory Service are:
Interface | Method |
---|---|
SAManagerObject | |
SAManager | void requestSAAgent(SAAgentSuccessCallback successCallback, ErrorCallback? errorCallback) void setDeviceStatusListener(SADeviceStatusCallback statusCallback) |
SAAgent | void requestServiceConnection(SAPeerAgent peerAgent) void setServiceConnectionListener(ServiceConnectionCallback connectionCallback) void authenticatePeerAgent(SAPeerAgent peerAgent, SAAuthenticatePeerAgentSuccessCallback successCallback, ErrorCallback? errorCallback) void acceptServiceConnectionRequest(SAPeerAgent peerAgent) void rejectServiceConnectionRequest(SAPeerAgent peerAgent) void findPeerAgents() void setPeerAgentFindListener(SAPeerAgentFindCallback foundCallback) SAFileTransfer getSAFileTransfer() |
SAAuthenticationToken | |
SAPeerAccessory | |
SAPeerAgent | |
SASocket | void close() boolean isConnected() void sendData(SAChannelId channelId, DOMString data) void sendSecureData(SAChannelId channelId, DOMString data) void setDataReceiveListener(SADataReceiveCallback receiveCallback) void setSocketStatusListener(SASocketStatusCallback statusCallback) |
SAFileTransfer | long sendFile(SAPeerAgent peerAgent, DOMString filePath) void setFileSendListener(SAFileSendCallback sendCallback) void setFileReceiveListener(SAFileReceiveCallback receiveCallback) void receiveFile(TransferId id, DOMString localPath) void cancelFile(TransferId id) void rejectFile(TransferId id) |
SADataReceiveCallback | void onreceive(SAChannelId channelId, DOMString data) |
SASocketStatusCallback | void onconnectionlost(SASocketStatusErrorCode reason) |
SAFileReceiveCallback | void onreceive(TransferId id, DOMString fileName) void onprogress(TransferId id, unsigned long progress) void oncomplete(TransferId id, DOMString localPath) void onerror(SAFileReceiveErrorCode errorCode, TransferId id) |
SAFileSendCallback | void onprogress(TransferId id, unsigned long progress) void oncomplete(TransferId id, DOMString localPath) void onerror(SAFileSendErrorCode errorCode, TransferId id) |
SAAgentSuccessCallback | void onsuccess(SAAgent[] agents) |
SAPeerAgentFindCallback | void onpeeragentfound(SAPeerAgent peerAgent) void onpeeragentupdated(SAPeerAgent peerAgent, PeerAgentStatus status) void onerror(SAPeerAgentFindErrorCode errorCode) |
ServiceConnectionCallback | void onrequest(SAPeerAgent peerAgent) void onconnect(SASocket socket) void onerror(SAServiceConnectionErrorCode errorCode) |
SADeviceStatusCallback | void ondevicestatus(TransportType type, DeviceStatus status) |
SAAuthenticatePeerAgentSuccessCallback | void onsuccess(SAPeerAgent peerAgent, SAAuthenticationToken authToken) |
Roles of an SAAgent
enum SARole {"PROVIDER", "CONSUMER"};
The supported roles of an SA are:
Transport types
enum TransportType {"TRANSPORT_BLE", "TRANSPORT_BT", "TRANSPORT_USB", "TRANSPORT_WIFI"};
The supported transport types are:
Device status types of connections with a peer device.
enum DeviceStatus {"ATTACHED", "DETACHED"};
The supported device status types are:
Status types of a peer agent.
enum PeerAgentStatus {"AVAILABLE", "UNAVAILABLE"};
The supported peer agent status types are:
Authentication types
enum AuthenticationType {"AUTHENTICATION_TYPE_NONE", "AUTHENTICATION_TYPE_CERTIFICATE_X509"};
The supported authentication types are:
Error codes for a peer agent search.
enum SAPeerAgentFindErrorCode {"PEER_NOT_FOUND", "DEVICE_NOT_CONNECTED", "DUPLICATE_REQUEST"};
The possible error codes are :
Error codes for a Service Connection.
enum SAServiceConnectionErrorCode {"ALREADY_EXIST", "NETWORK_ERROR", "DEVICE_UNREACHABLE", "INVALID_PEERAGENT", "PEERAGENT_NO_RESPONSE", "PEERAGENT_REJECTED", "ERROR_REFLECTION_EXCEPTION"};
The possible error codes are :
Error codes for sending a file.
enum SAFileSendErrorCode {"FILE_IO", "PEER_BUSY", "PEER_CHANNEL_IO", "PEER_CONN_LOST", "PEER_NO_RESPONSE", "PEER_REJECTED", "REQUEST_NOT_QUEUED", "SPACE_NOT_AVAILABLE", "UNKNOWN_ERROR"};
The possible error codes are :
Error codes for receiving a file.
enum SAFileReceiveErrorCode {"FILE_IO", "PEER_CHANNEL_IO", "PEER_CONN_LOST", "PEER_NO_RESPONSE", "PEER_REJECTED", "SPACE_NOT_AVAILABLE", "UNKNOWN_ERROR"};
The possible error codes are :
Error codes for socket status.
enum SASocketStatusErrorCode {"PEER_DISCONNECTED", "DEVICE_DETACHED", "UNKNOWN_REASON"};
The possible error codes are :
The Transaction ID given by the file transfer utility.
typedef long TransferId;
The Channel ID value specified in the Accessory Service Profile.
typedef long SAChannelId;
Defines what is instantiated in the webapis object.
[NoInterfaceObject] interface SAManagerObject { readonly attribute SAManager sa; };
WebAPIs implements SAManagerObject;
There is a webapis.sa object that allows accessing the functionality of the SAP API.
The SAManager interface gets SAAgents specified in the Accessory Service Profile.
[NoInterfaceObject] interface SAManager { void requestSAAgent(SAAgentSuccessCallback successCallback, optional ErrorCallback? errorCallback); void setDeviceStatusListener(SADeviceStatusCallback statusCallback); };
This interface is always available within the WebAPIs object in the ECMAScript hierarchy (window.webapis.sa).
requestSAAgent
Requests the SAAgent specified in the Accessory Service Profile.
void requestSAAgent(SAAgentSuccessCallback successCallback, optional ErrorCallback? errorCallback);
The ErrorCallback is launched with these error types:
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
function onsuccess(agents){ for(var i = 0; i < agents.length; i++) { if(agents[i].role == "PROVIDER") { console.log("Service Provider found! Name : " + agents[i].name); } if(agents[i].role == "CONSUMER") { console.log("Service Consumer found! Name : " + agents[i].name); } } } function onerror(e) { console.log("Error name : " + e.name + ", Error message : " + e.message); } try { webapis.sa.requestSAAgent(onsuccess, onerror); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setDeviceStatusListener
Sets a listener that is called when a peer device is attached or detached.
void setDeviceStatusListener(SADeviceStatusCallback statusCallback);
This status handler is useful to notice the host and Gear device disconnected(DETACHED case) or connected(ATTACHED case). Note that in the ATTACHED case, you need to call findePeerAgents() to eventually re-establish a new SASocket connection.
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
function ondevicestatus(type, status){ if (status == "ATTACHED") { console.log("Attached remote peer device. : " + type); } else if (status == "DETACHED") { console.log("Detached remote peer device. : " + type); } } try { webapis.sa.setDataReceiveListener(ondevicestatus); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
The SAAgent interface is the main interface between Service Provider/Service Consumer applications and the Accessory Service Framework.
[NoInterfaceObject] interface SAAgent { readonly attribute DOMString id; readonly attribute DOMString name; readonly attribute SARole role; readonly attribute SAChannelId[] channelIds; void requestServiceConnection(SAPeerAgent peerAgent); void setServiceConnectionListener(ServiceConnectionCallback connectionCallback); void authenticatePeerAgent(SAPeerAgent peerAgent, SAAuthenticatePeerAgentSuccessCallback successCallback, optional ErrorCallback? errorCallback); void acceptServiceConnectionRequest(SAPeerAgent peerAgent); void rejectServiceConnectionRequest(SAPeerAgent peerAgent); void findPeerAgents(); void setPeerAgentFindListener(SAPeerAgentFindCallback foundCallback); SAFileTransfer getSAFileTransfer(); };
This interface is the the main interface for an application to interact with the Accessory Service Framework. The application should provide Accessory Service Profile information in an XML file, for example /res/<yourname>.xml, and declare the path of the XML file in the config.xml file of the your web application project as follows.
<tizen:metadata key="AccessoryServicesLocation" value="res/serviceprofile.xml"/>
An example of an accessory XML file (/res/<yourname>.xml) specifying the Accessory Service Profile details is illustrated below:
A single application may include multiple Service Providers and/or Service Consumers. To do that, declare multiple <serviceProfile> elements inside the <application> element.
The <supportedTransports> element declares the transport(s) on which a Service Provider or Service Consumer is able to operate, as shown in the example code. Currently, the Samsung Accessory Service Framework supports the following transport types:
If a Service Provider or Service Consumer supports multiple transport types, it should declare multiple <transport> elements.
The example code shows a Service Provider application supporting both Bluetooth and Wi-Fi.
The <serviceChannel> elements may have the following attributes:
<resources> <application name = "my application"> <serviceProfile role="provider" name="smartview" id="/system/gallery" version="2.0" serviceLimit="ONE_PEERAGENT" serviceTimeout="30"> <supportedTransports> <transport type="TRANSPORT_BT"/> </supportedTransports> <serviceChannel id="100" dataRate = "high" priority = "low" reliability= "enable"/> </serviceProfile> <serviceProfile role="provider" name="message_service_provider" id="/system/messages" version="2.0" autoLaunchAppId="VcGvmQlo1i.HelloAccessory" serviceLimit="ANY"> <supportedTransports> <transport type="TRANSPORT_BT"/> </supportedTransports> <serviceChannel id="902" dataRate = "low" priority = "low" reliability= "disable"/> </serviceProfile> </application> </resources>
readonly
DOMString id
The ID read from the Service Profile, which is the Service Profile identifier of a Service Provider or Service Consumer.
readonly
DOMString name
The name read from the Service Profile, which is the friendly name of a Service Provider or Service Consumer.
readonly
SARole role
The role of the SAAgent read from the Service Profile. The available values are "PROVIDER" and "CONSUMER".
readonly
SAChannelId[] channelIds
The channel information read from the Service Profile.
requestServiceConnection
Requests to establish a Service Connection with a remote Accessory Peer Agent.
void requestServiceConnection(SAPeerAgent peerAgent);
A Service Provider or Service Consumer application may call this method to establish a Service Connection with a remote Accessory Peer Agent. The application is notified of the result of a Service Connection request via the ServiceConnectionCallback registered by setServiceConnectionListener(), which reports whether the request is accepted and a Service Connection is established, or the request for a Service Connection did not succeed.
The application is expected to ensure the availability of the Accessory Peer Agent before calling this method. This method is typically called in the SAPeerAgentFindSuccessCallback callback.
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ // DO SOMETHING }, onconnect : function(socket){ // Connected }, onerror : function(errorCode){ // DO SOMETHING } }; function onpeeragentfound(peerAgent) { if(peerAgent.appName == "expected app name") { try { agent.requestServiceConnection(peerAgent); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } } function onpeeragentupdated(peerAgent, status) { if(status == "AVAILABLE") { try { agent.requestServiceConnection(peerAgent); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } else if(status == "UNAVAILABLE") { console.log("Uninstalled application package of peerAgent on remote device."); } } function onerror(errorCode) { console.log("Error code : " + errorCode); if(errorCode == "PEER_NOT_FOUND") { console.log("If remote application is not already installed on the remote device, Please wait onpeeragentupdated callback."); } } var peeragentfindcallback = { onpeeragentfound : onpeeragentfound, onpeeragentupdated : onpeeragentupdated, onerror : onerror }; try { agent.setServiceConnectionListener(callback); agent.setPeerAgentFindListener(peeragentfindcallback); agent.findPeerAgents(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setServiceConnectionListener
Sets a listener to get both a connection request and connection establish event.
void setServiceConnectionListener(ServiceConnectionCallback connectionCallback);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ // DO SOMETHING }, onconnect : function(socket){ // DO SOMETHING }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
authenticatePeerAgent
Gets authentication info of remote peer application.
void authenticatePeerAgent(SAPeerAgent peerAgent, SAAuthenticatePeerAgentSuccessCallback successCallback, optional ErrorCallback? errorCallback);
The ErrorCallback is launched with these error types:
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ try { agent.authenticatePeerAgent(peerAgent, function(peerAgent, authToken) { if (authToken.key == localPublicKey) { agent.acceptServiceConnectionRequest(peerAgent); } else { agent.rejectServiceConnectionRequest(peerAgent); } }, function(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } }, onconnect : function(socket){ // DO SOMETHING }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
acceptServiceConnectionRequest
Accepts an incoming Service Connection request from a remote Accessory Peer Agent.
void acceptServiceConnectionRequest(SAPeerAgent peerAgent);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ if(peerAgent.appName == "expected_appName") { try { agent.acceptServiceConnectionRequest(peerAgent); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } else { // DO SOMETHING } }, onconnect : function(socket){ // DO SOMETHING }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
rejectServiceConnectionRequest
Rejects an incoming Service Connection request from a remote Accessory Peer Agent.
void rejectServiceConnectionRequest(SAPeerAgent peerAgent);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ if(peerAgent.appName == "expected_appName") { // DO SOMETHING } else { try { agent.rejectServiceConnectionRequest(peerAgent); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, onconnect : function(socket){ // DO SOMETHING }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
findPeerAgents
Finds a matching Accessory Peer Agent.
void findPeerAgents();
This method starts finding matching Accessory Peer Agents. If any matching Accessory Peer Agent is found, the application notifies via the onpeeragentfound callback that is registered by the setPeerAgentFindListener function. If multiple matching Accessory Peer Agents are found, the callback is invoked multiple times, once for each matching Accessory Peer Agent.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. function onpeeragentfound(peerAgent) { if(peerAgent.appName == "expected app name") { //DO SOMETHING } } function onpeeragentupdated(peerAgent, status) { if(status == "AVAILABLE") { try { //DO SOMETHING } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } else if(status == "UNAVAILABLE") { console.log("Uninstalled application package of peerAgent on remote device."); } } function onerror(errorCode) { console.log("Error code : " + errorCode); if(errorCode == "PEER_NOT_FOUND") { console.log("If remote application is not already installed on the remote device, Please wait onpeeragentupdated callback."); } } var peeragentfindcallback = { onpeeragentfound : onpeeragentfound, onpeeragentupdated : onpeeragentupdated, onerror : onerror }; try { agent.setPeerAgentFindListener(peeragentfindcallback); agent.findPeerAgents(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setPeerAgentFindListener
Sets a listener to set the found remote Accessory Peer Agents.
void setPeerAgentFindListener(SAPeerAgentFindCallback foundCallback);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. function onpeeragentfound(peerAgent) { if(peerAgent.appName == "expected app name") { //DO SOMETHING } } function onpeeragentupdated(peerAgent, status) { if(status == "AVAILABLE") { //DO SOMETHING } } function onerror(errorCode) { console.log("Error code : " + errorCode); } var peeragentfindcallback = { onpeeragentfound : onpeeragentfound, onpeeragentupdated : onpeeragentupdated, onerror : onerror }; try { agent.setPeerAgentFindListener(peeragentfindcallback); agent.findPeerAgents(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
getSAFileTransfer
Gets an object with which all file transfer functionalities can be accessed.
SAFileTransfer getSAFileTransfer();
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. try { var filetransfer = agent.getSAFileTransfer(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
The SAAuthenticationToken interface contains authentication information of remote peer agent.
[NoInterfaceObject] interface SAAuthenticationToken { readonly attribute AuthenticationType authenticationType; readonly attribute DOMString key; };
readonly
AuthenticationType authenticationType
The type of the Authentication token.
readonly
DOMString key
The key corresponding to this authentication token.
The SAPeerAccessory interface contains information of the Accessory Device on which an Accessory Peer Agent runs.
[NoInterfaceObject] interface SAPeerAccessory { readonly attribute DOMString deviceAddress; readonly attribute DOMString deviceName; readonly attribute DOMString productId; readonly attribute TransportType transportType; readonly attribute DOMString vendorId; };
readonly
DOMString deviceAddress
The address of the Accessory Device.
readonly
DOMString deviceName
The name of the Accessory Device.
readonly
DOMString productId
The product identifier of the Accessory Device.
readonly
TransportType transportType
The transport type supported by the Accessory Device.
readonly
DOMString vendorId
The vendor identifier of the Accessory Device.
The SAPeerAgent interface contains information of an Accessory Peer Agent and the Accessory Device on which the Accessory Peer Agent runs.
[NoInterfaceObject] interface SAPeerAgent { readonly attribute SAPeerAccessory peerAccessory; readonly attribute DOMString appName; readonly attribute long maxAllowedDataSize; readonly attribute DOMString peerId; readonly attribute DOMString profileVersion; };
readonly
SAPeerAccessory peerAccessory
The Accessory Device on which the Accessory Peer Agent runs.
readonly
DOMString appName
The name of the application to which the Accessory Peer Agent belongs.
readonly
long maxAllowedDataSize
The maximum payload size supported for sendData() and secureSendData().
readonly
DOMString peerId
The identifier of the Accessory Peer Agent.
readonly
DOMString profileVersion
The Service Profile specification version that the Accessory Peer Agent supports.
Represents an instance of a Service Connection between a Service Provider and Service Consumer.
[NoInterfaceObject] interface SASocket { readonly attribute SAPeerAgent peerAgent; void close(); boolean isConnected(); void sendData(SAChannelId channelId, DOMString data); void sendSecureData(SAChannelId channelId, DOMString data); void setDataReceiveListener(SADataReceiveCallback receiveCallback); void setSocketStatusListener(SASocketStatusCallback statusCallback); };
readonly
SAPeerAgent peerAgent
The object containing information about a connected remote Accessory Peer Agent, and information about the Accessory Device on which the Accessory Peer Agent runs.
close
Closes the Service Connection established with a remote Accessory Peer Agent.
void close();
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ // DO SOMETHING }, onconnect : function(socket){ if(socket.peerAgent.appName != "expected_appName") { try { socket.close(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
isConnected
Returns the status of an underlying Accessory Service Connection.
boolean isConnected();
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ // DO SOMETHING }, onconnect : function(socket){ if(socket.peerAgent.appName != "expected_appName") { try { var result = socket.isConnected(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
sendData
Sends sting data to a remote Accessory Peer Agent.
void sendData(SAChannelId channelId, DOMString data);
Sends a string to the specified Service Channel. The string must not be longer than the value of the maxAllowedDataSize attribute in the SAPeerAgent interface.
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
with error type IOError, if an I/O-related error occurs while sending data.
with error type InvalidValuesError, if the length of the string is longer than the value of the maxAllowedDataSize attribute or the channel is invalid.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ // DO SOMETHING }, onconnect : function(socket){ for(var i = 0; i < agent.channelIds.length; i++) { try { socket.sendData(agent.channelIds[i], "Test String to "+i+"th channel."); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
sendSecureData
Sends encrypted sting data to a remote Accessory Peer Agent.
void sendSecureData(SAChannelId channelId, DOMString data);
Sends an encrypted string to the specified Service Channel. The string must not be longer than value of the maxAllowedDataSize attribute in the SAPeerAgent interface.
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
with error type IOError, if an I/O-related error occurs while sending data.
with error type InvalidValuesError, if the length of the string is longer than the value of the maxAllowedDataSize attribute or the channel is invalid.
// Assume agent object has been obtained by using requestSAAgent() method. var callback = { onrequest : function(peerAgent){ // DO SOMETHING }, onconnect : function(socket){ for(var i = 0; i < agent.channelIds.length; i++) { try { socket.sendSecureData(agent.channelIds[i], "Test String to "+i+"th channel."); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, onerror : function(errorCode){ // DO SOMETHING } }; try { agent.setServiceConnectionListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setDataReceiveListener
Sets a listener that is called when data is received from a remote Accessory Peer Agent.
void setDataReceiveListener(SADataReceiveCallback receiveCallback);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume socket object has been obtained by using setServiceConnectionListener() method. function onreceive(channelId, data){ console.log("Message received from the channel " + channelId + " : " + data); } try { socket.setDataReceiveListener(onreceive); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setSocketStatusListener
Sets a listener to monitor the socket's connection status.
void setSocketStatusListener(SASocketStatusCallback statusCallback);
The connection may be lost for various reasons. For example:
The reason for the Service Connection being lost is indicated via the errorCode.
The application is expected to implement appropriate error handling mechanisms in this callback method.
For example, the application should stop sending data on channels of the Service Connection if the connection is lost.
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported
// Assume socket object has been obtained by using setServiceConnectionListener() method. function onconnectionlost (reason){ console.log("Service Connection disconnected due to following reason : "+ reason); } try { socket.setSocketStatusListener(onconnectionlost); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
Represents an instance of a File Transfer between a Service Provider and Service Consumer.
[NoInterfaceObject] interface SAFileTransfer { readonly attribute DOMString defaultReceivePath; long sendFile(SAPeerAgent peerAgent, DOMString filePath); void setFileSendListener(SAFileSendCallback sendCallback); void setFileReceiveListener(SAFileReceiveCallback receiveCallback); void receiveFile(TransferId id, DOMString localPath); void cancelFile(TransferId id); void rejectFile(TransferId id); };
readonly
DOMString defaultReceivePath
The default path where the received file is stored.
sendFile
Sends a file to a connected peer.
long sendFile(SAPeerAgent peerAgent, DOMString filePath);
The filePath is either a path from the virtual root or the URI path as described in the Filesystem API.
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
with error type InvalidValuesError, if the URI scheme name of the file path is invalid or the virtual root is invalid.
// Assume agent object has been obtained by using requestSAAgent() 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") { try { transferId = filetransfer.sendFile(peerAgent, filePath); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, onerror : function(errorCode){ // DO SOMETHING } }; try { filetransfer = agent.getSAFileTransfer(); agent.setPeerAgentFindListener(peeragentfindcallback); agent.findPeerAgents(); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setFileSendListener
Sets a listener to obtain a file's send status, such as on file progress, on send complete, and on any error status.
void setFileSendListener(SAFileSendCallback sendCallback);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume filetransfer object has been obtained by using getSAFileTransfer() method. var callback = { 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); } }; try { filetransfer.setFileSendListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
setFileReceiveListener
Sets a listener to obtain a file's receive status, such as on receive file, on file progress, on receive complete, and on any error situation.
void setFileReceiveListener(SAFileReceiveCallback receiveCallback);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
// Assume filetransfer object has been obtained by using getSAFileTransfer() method. var callback = { 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); } }; try { filetransfer.setFileReceiveListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
receiveFile
Called by the receiver of the file.
void receiveFile(TransferId id, DOMString localPath);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
with error type InvalidValuesError, if the URI scheme name of the file path is invalid or the virtual root is invalid, or the transaction ID is invalid.
// Assume filetransfer object has been obtained by using getSAFileTransfer() method. var newFilePath = "file:///opt/usr/media/Downloads/ReceivedImage.jpg"; var callback = { onreceive : function(transferId, fileName){ try { filetransfer.receiveFile(transferId, newFilePath); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } }, 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); } }; try { filetransfer.setFileReceiveListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
cancelFile
Cancels an ongoing file transfer. This may be called by the sender or receiver of the file.
This API is available for both the sender and receiver side.
void cancelFile(TransferId id);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
with error type InvalidValuesError, if the transaction ID is invalid.
// Assume filetransfer object has been obtained by using getSAFileTransfer() method. var callback = { onreceive : function(transferId, fileName){ try { console.log("Incoming file transfer request form the remote peer agent. transferId : " + transferId + " file name : " + fileName); filetransfer.receiveFile(transferId, newFilePath); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } }, onprogress : function(transferId, progress){ if(1) { // User wanted condition try { filetransfer.cancelFile(transferId); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, oncomplete : function(transferId, localPath){ console.log("File transfer complete. transferId : " + transferId); }, onerror : function(errorCode, transferId){ console.log("FileReceiveError transferId : " + transferId + " code : " + errorCode); } }; try { filetransfer.setFileReceiveListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
rejectFile
Rejects an incoming transfer request from a remote Accessory Peer Agent.
void rejectFile(TransferId id);
with error type TypeMismatchError, if any input parameter is not compatible with the expected type for that parameter.
with error type SecurityError, if this functionality is not allowed.
with error type NotSupportedError, if this feature is not supported.
with error type InvalidValuesError, if the transaction ID is invalid.
// Assume filetransfer object has been obtained by using getSAFileTransfer() method. var callback = { onreceive : function(transferId, fileName){ if(1) { // User wanted condition try { filetransfer.rejectFile(transferId); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); } } }, 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); } }; try { filetransfer.setFileReceiveListener(callback); } catch(e) { console.log("Error Exception, error name : " + e.name + ", error message : " + e.message); }
Called when data is received from a remote Accessory Peer Agent.
[Callback=FunctionOnly, NoInterfaceObject] interface SADataReceiveCallback { void onreceive(SAChannelId channelId, DOMString data); };
onreceive
Called when data is received from a remote Accessory Peer Agent.
void onreceive(SAChannelId channelId, DOMString data);
The callback function to be invoked when a Service Connection is lost.
[Callback=FunctionOnly, NoInterfaceObject] interface SASocketStatusCallback { void onconnectionlost(SASocketStatusErrorCode reason); };
onconnectionlost
Called to indicate to the application that the Service Connection with a Accessory Peer Agent is lost.
void onconnectionlost(SASocketStatusErrorCode reason);
The callback function to be invoked when receiving a file.
[Callback, NoInterfaceObject] interface SAFileReceiveCallback { void onreceive(TransferId id, DOMString fileName); void onprogress(TransferId id, unsigned long progress); void oncomplete(TransferId id, DOMString localPath); void onerror(SAFileReceiveErrorCode errorCode, TransferId id); };
onreceive
Invoked on the receiver side when there is an incoming file transfer request from the sender. The receiver needs to be registered beforehand for the file transfer service to receive this callback.
void onreceive(TransferId id, DOMString fileName);
onprogress
Provides the progress information.
void onprogress(TransferId id, unsigned long progress);
oncomplete
Indicates that the file transfer is complete.
void oncomplete(TransferId id, DOMString localPath);
onerror
Invoked when an error occurs while receiving a file.
void onerror(SAFileReceiveErrorCode errorCode, TransferId id);
The callback function to be invoked when sending a file.
[Callback, NoInterfaceObject] interface SAFileSendCallback { void onprogress(TransferId id, unsigned long progress); void oncomplete(TransferId id, DOMString localPath); void onerror(SAFileSendErrorCode errorCode, TransferId id); };
onprogress
Provides the progress information.
void onprogress(TransferId id, unsigned long progress);
oncomplete
Indicates that the file transfer is complete.
void oncomplete(TransferId id, DOMString localPath);
onerror
Invoked when an error occurs while sending a file.
void onerror(SAFileSendErrorCode errorCode, TransferId id);
This callback function is invoked when the SAAgent object is successfully retrieved from the Accessory Service Profile.
[Callback=FunctionOnly, NoInterfaceObject] interface SAAgentSuccessCallback { void onsuccess(SAAgent[] agents); };
onsuccess
Invoked when agents specified in the Service Profile are successfully retrieved.
void onsuccess(SAAgent[] agents);
This callback function is invoked when finding remote Accessory Peer Agents.
[Callback, NoInterfaceObject] interface SAPeerAgentFindCallback { void onpeeragentfound(SAPeerAgent peerAgent); void onpeeragentupdated(SAPeerAgent peerAgent, PeerAgentStatus status); void onerror(SAPeerAgentFindErrorCode errorCode); };
onpeeragentfound
Called whenever a remote Accessory Peer Agent is found
void onpeeragentfound(SAPeerAgent peerAgent);
onpeeragentupdated
This callback function is invoked to notify an application of a change in the status of a peer agent on a remote device. This callback is invoked only if a service discovery request has been previously initiated by a call to findPeerAgents(). The status of the discovered peer is contained in the result parameter of the callback and indicates the availability or unavailability of the peer agent. If there is more than one service agent for which the status is updated (on one or more connected Accessory Devices), this callback is invoked once for each peer service agent.
void onpeeragentupdated(SAPeerAgent peerAgent, PeerAgentStatus status);
onerror
Called when any kind of error occurs while finding remote Accessory Peer Agents.
void onerror(SAPeerAgentFindErrorCode errorCode);
This callback function is invoked for sending notifications of connection request events.
[Callback, NoInterfaceObject] interface ServiceConnectionCallback { void onrequest(SAPeerAgent peerAgent); void onconnect(SASocket socket); void onerror(SAServiceConnectionErrorCode errorCode); };
onrequest
Invoked when a remote Accessory Peer Agent requests for a Service Connection with an application.
void onrequest(SAPeerAgent peerAgent);
Application calls SAAgent.acceptServiceConnectionRequest() in this callback to accept a Service Connection request, or calls SAAgent.rejectServiceConnectionRequest() to reject it.
onconnect
Invoked when a connection between a Service Provider and Service Consumer is established.
void onconnect(SASocket socket);
onerror
Invoked when any kind of error occurs during a connect/request operation.
void onerror(SAServiceConnectionErrorCode errorCode);
This callback function is invoked when a peer device is attached or detached.
[Callback=FunctionOnly, NoInterfaceObject] interface SADeviceStatusCallback { void ondevicestatus(TransportType type, DeviceStatus status); };
ondevicestatus
Invoked when a peer device is attached or detached.
void ondevicestatus(TransportType type, DeviceStatus status);
This callback function is invoked when authentication information of remote peer are successfully returned.
[Callback=FunctionOnly, NoInterfaceObject] interface SAAuthenticatePeerAgentSuccessCallback { void onsuccess(SAPeerAgent peerAgent, SAAuthenticationToken authToken); };
Application can verify remote peer agent's identity by checking its authentication token information, then decide whether to request a service connection with the remote peer agent, or whether to accept incoming service connection requests from the remote peer agent.
onsuccess
Invoked when authentication information of remote peer are successfully returned
void onsuccess(SAPeerAgent peerAgent, SAAuthenticationToken authToken);
This is the list of URIs used to declare this API's features, for use in the widget config.xml. For each URL, the list of functions covered is provided.
To guarantee the running of this application on a device with SAP feature, define the following requirements in the config file:
module SAPService { enum SARole {"PROVIDER", "CONSUMER"}; enum TransportType {"TRANSPORT_BLE", "TRANSPORT_BT", "TRANSPORT_USB", "TRANSPORT_WIFI"}; enum DeviceStatus {"ATTACHED", "DETACHED"}; enum PeerAgentStatus {"AVAILABLE", "UNAVAILABLE"}; enum AuthenticationType {"AUTHENTICATION_TYPE_NONE", "AUTHENTICATION_TYPE_CERTIFICATE_X509"}; enum SAPeerAgentFindErrorCode {"PEER_NOT_FOUND", "DEVICE_NOT_CONNECTED", "DUPLICATE_REQUEST"}; enum SAServiceConnectionErrorCode {"ALREADY_EXIST", "NETWORK_ERROR", "DEVICE_UNREACHABLE", "INVALID_PEERAGENT", "PEERAGENT_NO_RESPONSE", "PEERAGENT_REJECTED", "ERROR_REFLECTION_EXCEPTION"}; enum SAFileSendErrorCode {"FILE_IO", "PEER_BUSY", "PEER_CHANNEL_IO", "PEER_CONN_LOST", "PEER_NO_RESPONSE", "PEER_REJECTED", "REQUEST_NOT_QUEUED", "SPACE_NOT_AVAILABLE", "UNKNOWN_ERROR"}; enum SAFileReceiveErrorCode {"FILE_IO", "PEER_CHANNEL_IO", "PEER_CONN_LOST", "PEER_NO_RESPONSE", "PEER_REJECTED", "SPACE_NOT_AVAILABLE", "UNKNOWN_ERROR"}; enum SASocketStatusErrorCode {"PEER_DISCONNECTED", "DEVICE_DETACHED", "UNKNOWN_REASON"}; typedef long TransferId; typedef long SAChannelId; [NoInterfaceObject] interface SAManagerObject { readonly attribute SAManager sa; }; WebAPIs implements SAManagerObject; [NoInterfaceObject] interface SAManager { void requestSAAgent(SAAgentSuccessCallback successCallback, optional ErrorCallback? errorCallback); void setDeviceStatusListener(SADeviceStatusCallback statusCallback); }; [NoInterfaceObject] interface SAAgent { readonly attribute DOMString id; readonly attribute DOMString name; readonly attribute SARole role; readonly attribute SAChannelId[] channelIds; void requestServiceConnection(SAPeerAgent peerAgent); void setServiceConnectionListener(ServiceConnectionCallback connectionCallback); void authenticatePeerAgent(SAPeerAgent peerAgent, SAAuthenticatePeerAgentSuccessCallback successCallback, optional ErrorCallback? errorCallback); void acceptServiceConnectionRequest(SAPeerAgent peerAgent); void rejectServiceConnectionRequest(SAPeerAgent peerAgent); void findPeerAgents(); void setPeerAgentFindListener(SAPeerAgentFindCallback foundCallback); SAFileTransfer getSAFileTransfer(); }; [NoInterfaceObject] interface SAAuthenticationToken { readonly attribute AuthenticationType authenticationType; readonly attribute DOMString key; }; [NoInterfaceObject] interface SAPeerAccessory { readonly attribute DOMString deviceAddress; readonly attribute DOMString deviceName; readonly attribute DOMString productId; readonly attribute TransportType transportType; readonly attribute DOMString vendorId; }; [NoInterfaceObject] interface SAPeerAgent { readonly attribute SAPeerAccessory peerAccessory; readonly attribute DOMString appName; readonly attribute long maxAllowedDataSize; readonly attribute DOMString peerId; readonly attribute DOMString profileVersion; }; [NoInterfaceObject] interface SASocket { readonly attribute SAPeerAgent peerAgent; void close(); boolean isConnected(); void sendData(SAChannelId channelId, DOMString data); void sendSecureData(SAChannelId channelId, DOMString data); void setDataReceiveListener(SADataReceiveCallback receiveCallback); void setSocketStatusListener(SASocketStatusCallback statusCallback); }; [NoInterfaceObject] interface SAFileTransfer { readonly attribute DOMString defaultReceivePath; long sendFile(SAPeerAgent peerAgent, DOMString filePath); void setFileSendListener(SAFileSendCallback sendCallback); void setFileReceiveListener(SAFileReceiveCallback receiveCallback); void receiveFile(TransferId id, DOMString localPath); void cancelFile(TransferId id); void rejectFile(TransferId id); }; [Callback=FunctionOnly, NoInterfaceObject] interface SADataReceiveCallback { void onreceive(SAChannelId channelId, DOMString data); }; [Callback=FunctionOnly, NoInterfaceObject] interface SASocketStatusCallback { void onconnectionlost(SASocketStatusErrorCode reason); }; [Callback, NoInterfaceObject] interface SAFileReceiveCallback { void onreceive(TransferId id, DOMString fileName); void onprogress(TransferId id, unsigned long progress); void oncomplete(TransferId id, DOMString localPath); void onerror(SAFileReceiveErrorCode errorCode, TransferId id); }; [Callback, NoInterfaceObject] interface SAFileSendCallback { void onprogress(TransferId id, unsigned long progress); void oncomplete(TransferId id, DOMString localPath); void onerror(SAFileSendErrorCode errorCode, TransferId id); }; [Callback=FunctionOnly, NoInterfaceObject] interface SAAgentSuccessCallback { void onsuccess(SAAgent[] agents); }; [Callback, NoInterfaceObject] interface SAPeerAgentFindCallback { void onpeeragentfound(SAPeerAgent peerAgent); void onpeeragentupdated(SAPeerAgent peerAgent, PeerAgentStatus status); void onerror(SAPeerAgentFindErrorCode errorCode); }; [Callback, NoInterfaceObject] interface ServiceConnectionCallback { void onrequest(SAPeerAgent peerAgent); void onconnect(SASocket socket); void onerror(SAServiceConnectionErrorCode errorCode); }; [Callback=FunctionOnly, NoInterfaceObject] interface SADeviceStatusCallback { void ondevicestatus(TransportType type, DeviceStatus status); }; [Callback=FunctionOnly, NoInterfaceObject] interface SAAuthenticatePeerAgentSuccessCallback { void onsuccess(SAPeerAgent peerAgent, SAAuthenticationToken authToken); }; };