Class SAMessage
- java.lang.Object
-
- com.samsung.android.sdk.accessory.SAMessage
-
public abstract class SAMessage extends java.lang.Object
SAMessage represents an instance for a Message Service between a Service Provider and a Service Consumer.Developer Guides
Both Service Provider and Service Consumer application must declare a subclass of
SAAgent
and implement the actions of an Accessory Peer Agent. The Message Service is a kind of representative for the simple dialogue between the twoSAAgent
objects.Both Service Provider and Service Consumer application must create an instance of
SAMessage
, and implement the message callbacks:onReceive(SAPeerAgent, byte[])
,onSent(SAPeerAgent, int)
andonError(SAPeerAgent, int, int)
in order to send a message from an Accessory Agent and receive a message from remote Accessory Peer Agent. The application must also define a Broadcast Receiver for theACTION_ACCESSORY_MESSAGE_RECEIVED
action on the receiver side for the message to be received.The application can call
send(SAPeerAgent, byte[])
to deliver the message to a specific remote Accessory Peer Agent. It can also callsecureSend(SAPeerAgent, byte[])
to deliver the message to a specific remote Accessory Peer Agent with encryption enabled.Note: An SAMessage instance will be associated with only one SAAgent and it should be created on start of the
SAAgent
service i.e inYourSubclassExtendsSAAgent().onCreate()
callback as follows:public YourSubclassExtendsSAAgent() extends SAAgent { SAMessage mMessage; @Override public void onCreate() { super.onCreate(); ... mMessage = new SAMessage(YourSubclassExtendsSAAgent.this){ @Override protected void onSent(SAPeerAgent peerAgent, int id) { } @Override protected void onError(SAPeerAgent peerAgent, int id, int errorCode) { } @Override protected void onReceive(SAPeerAgent peerAgent, byte[] message) { } }; } }
When you build your application in release mode, you must add -keep lines in the proguard.cfg file of your application, to prevent ProGuard from renaming your subclasses of
SAMessage
,SASocket
, andSAAgent
. ProGuard is a tool integrated into the Android build system that obfuscates the code by renaming classes and methods. For more information about How to configure ProGuard, please visit the ProGuard page of Android Development Site.
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.String
ACTION_ACCESSORY_MESSAGE_DISABLED
Broadcast Action: Indicates the Message Service has been disabled.static java.lang.String
ACTION_ACCESSORY_MESSAGE_ENABLED
Broadcast Action: Indicates the Message Service has been enabled.static java.lang.String
ACTION_ACCESSORY_MESSAGE_RECEIVED
Broadcast Action: Indicates an incoming message from the remote Accessory Peer Agent has been received.static int
ERROR_PEER_AGENT_NO_RESPONSE
The remote Accessory Peer Agent does not give any response within the timeout period which depends on Samsung Accessory Service Framework.static int
ERROR_PEER_AGENT_NOT_SUPPORTED
The remote Accessory Peer Agent does not support Message Service feature.static int
ERROR_PEER_AGENT_UNREACHABLE
The remote Accessory Peer Agent is not reachable or not alive to receive any messages.static int
ERROR_PEER_SERVICE_NOT_SUPPORTED
Samsung Accessory Service Framework on the remote device does not support Message Service feature.static int
ERROR_SERVICE_NOT_SUPPORTED
Samsung Accessory Service Framework on this device does not support Message Service feature.static int
ERROR_UNKNOWN
The remote Accessory Peer Agent has some internal error which occurred on the remote device.
-
Constructor Summary
Constructors Constructor and Description SAMessage(SAAgent agent)
Creates anSAMessage
instance with the specified SAAgent.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description protected abstract void
onError(SAPeerAgent peerAgent, int id, int errorCode)
Called when the message cannot be sent or is not delivered to the remote Accessory Peer Agent.protected abstract void
onReceive(SAPeerAgent peerAgent, byte[] message)
Called when a message is received from a remote Accessory Peer Agent.protected abstract void
onSent(SAPeerAgent peerAgent, int id)
Called when the message is successfully delivered to the remote Accessory Peer Agent.int
secureSend(SAPeerAgent peerAgent, byte[] message)
Same likesend(SAPeerAgent, byte[])
but the message will be encrypted internally.int
send(SAPeerAgent peerAgent, byte[] message)
Send a message to a remote Accessory Peer Agent.
-
-
-
Field Detail
-
ERROR_PEER_AGENT_UNREACHABLE
public static final int ERROR_PEER_AGENT_UNREACHABLE
The remote Accessory Peer Agent is not reachable or not alive to receive any messages.Constant Value: 1793 (0x00000701)
- Since:
- 2.5.0
- See Also:
onError(SAPeerAgent, int, int)
, Constant Field Values
-
ERROR_PEER_AGENT_NO_RESPONSE
public static final int ERROR_PEER_AGENT_NO_RESPONSE
The remote Accessory Peer Agent does not give any response within the timeout period which depends on Samsung Accessory Service Framework.Constant Value: 1794 (0x00000702)
- Since:
- 2.5.0
- See Also:
onError(SAPeerAgent, int, int)
, Constant Field Values
-
ERROR_PEER_AGENT_NOT_SUPPORTED
public static final int ERROR_PEER_AGENT_NOT_SUPPORTED
The remote Accessory Peer Agent does not support Message Service feature. It needs to establish legacy Service Connection.Constant Value: 1795 (0x00000703)
- Since:
- 2.5.0
- See Also:
onError(SAPeerAgent, int, int)
, Constant Field Values
-
ERROR_PEER_SERVICE_NOT_SUPPORTED
public static final int ERROR_PEER_SERVICE_NOT_SUPPORTED
Samsung Accessory Service Framework on the remote device does not support Message Service feature. It needs to establish legacy Service Connection.Constant Value: 1796 (0x00000704)
- Since:
- 2.5.0
- See Also:
onError(SAPeerAgent, int, int)
, Constant Field Values
-
ERROR_SERVICE_NOT_SUPPORTED
public static final int ERROR_SERVICE_NOT_SUPPORTED
Samsung Accessory Service Framework on this device does not support Message Service feature. It needs to establish legacy Service Connection.Constant Value: 1797 (0x00000705)
- Since:
- 2.5.0
- See Also:
onError(SAPeerAgent, int, int)
, Constant Field Values
-
ERROR_UNKNOWN
public static final int ERROR_UNKNOWN
The remote Accessory Peer Agent has some internal error which occurred on the remote device.Constant Value: 1798 (0x00000706)
- Since:
- 2.5.0
- See Also:
onError(SAPeerAgent, int, int)
, Constant Field Values
-
ACTION_ACCESSORY_MESSAGE_ENABLED
public static final java.lang.String ACTION_ACCESSORY_MESSAGE_ENABLED
Broadcast Action: Indicates the Message Service has been enabled.Constant Value: "com.samsung.accessory.action.MESSAGE_ENABLED"
- Since:
- 2.5.0
- See Also:
- Constant Field Values
-
ACTION_ACCESSORY_MESSAGE_DISABLED
public static final java.lang.String ACTION_ACCESSORY_MESSAGE_DISABLED
Broadcast Action: Indicates the Message Service has been disabled.Constant Value: "com.samsung.accessory.action.MESSAGE_DISABLED"
- Since:
- 2.5.0
- See Also:
- Constant Field Values
-
ACTION_ACCESSORY_MESSAGE_RECEIVED
public static final java.lang.String ACTION_ACCESSORY_MESSAGE_RECEIVED
Broadcast Action: Indicates an incoming message from the remote Accessory Peer Agent has been received.Note: The broadcast receiver must be registered in the receiving application's manifest
Constant Value: "com.samsung.accessory.action.MESSAGE_RECEIVED"
- Since:
- 2.5.0
- See Also:
- Constant Field Values
-
-
Method Detail
-
send
public int send(SAPeerAgent peerAgent, byte[] message) throws java.io.IOException
Send a message to a remote Accessory Peer Agent. To find a matching Accessory Peer Agent, please useSAAgent.findPeerAgents()
.Sender will receive the delivery status via
onSent(SAPeerAgent peerAgent, int id)
oronError(SAPeerAgent peerAgent, int id, int errorCode)
callback. The message will be delivered to the remote Accessory Peer Agent through itsonReceive(SAPeerAgent, byte[])
callback in itsSAMessage
implementation.Note: This method is a blocking call hence do not invoke this method on the main thread of the application. It is recommended to use a separate thread to invoke this API. DO NOT send a byte array bigger than
SAPeerAgent.getMaxAllowedMessageSize()
. Only one thread can send a message to one Accessory Peer Agent at a time.- Parameters:
peerAgent
- The remote Accessory Peer Agent to receive the message.message
- The byte array of the message to be sent.- Returns:
- ID of sending a message.
- Throws:
java.io.IOException
- Thrown if an I/O-related error occurs while sending a message.java.lang.IllegalArgumentException
- Thrown if any arguments is invalid.- Since:
- 2.5.0
- See Also:
onReceive(SAPeerAgent, byte[])
,onSent(SAPeerAgent, int)
,onError(SAPeerAgent, int, int)
-
secureSend
public int secureSend(SAPeerAgent peerAgent, byte[] message) throws java.io.IOException
Same likesend(SAPeerAgent, byte[])
but the message will be encrypted internally. To find a matching Accessory Peer Agent, please useSAAgent.findPeerAgents()
.Sender will receive the delivery status via
onSent(SAPeerAgent peerAgent, int id)
oronError(SAPeerAgent peerAgent, int id, int errorCode)
callback. The message will be delivered to the remote Accessory Peer Agent through itsonReceive(SAPeerAgent, byte[])
callback in itsSAMessage
implementation.Note: This method is a blocking call hence do not invoke this method on the main thread of the application. It is recommended to use a separate thread to invoke this API. DO NOT send a byte array bigger than
SAPeerAgent.getMaxAllowedMessageSize()
. Only one thread can send a message to one Accessory Peer Agent at a time.- Parameters:
peerAgent
- The remote Accessory Peer Agent to receive the message.message
- The byte array of the message to be sent.- Returns:
- ID of sending a message.
- Throws:
java.io.IOException
- Thrown if an I/O-related error occurs while sending message.java.lang.IllegalArgumentException
- Thrown if any argument is invalid.- Since:
- 2.5.0
- See Also:
onReceive(SAPeerAgent, byte[])
,onSent(SAPeerAgent, int)
,onError(SAPeerAgent, int, int)
-
onReceive
protected abstract void onReceive(SAPeerAgent peerAgent, byte[] message)
Called when a message is received from a remote Accessory Peer Agent.- Parameters:
peerAgent
- The remote Accessory Peer Agent who has sent the message.message
- The byte[] message- Since:
- 2.5.0
-
onSent
protected abstract void onSent(SAPeerAgent peerAgent, int id)
Called when the message is successfully delivered to the remote Accessory Peer Agent.- Parameters:
peerAgent
- The remote Accessory Peer Agent to receive the message.id
- ID of sending a message. It matches with the returned value insend(SAPeerAgent, byte[])
orsecureSend(SAPeerAgent, byte[])
.- Since:
- 2.5.0
-
onError
protected abstract void onError(SAPeerAgent peerAgent, int id, int errorCode)
Called when the message cannot be sent or is not delivered to the remote Accessory Peer Agent.- Parameters:
peerAgent
- The remote Accessory Peer Agent to receive the message.id
- ID of sending a message. It matches with the returned value insend(SAPeerAgent, byte[])
orsecureSend(SAPeerAgent, byte[])
.-1
if Message Service feature is not supported between the Accessory Agent and the Accessory Peer Agent.errorCode
- The error code corresponding to ERROR_*.- Since:
- 2.5.0
- See Also:
ERROR_PEER_AGENT_UNREACHABLE
,ERROR_PEER_AGENT_NO_RESPONSE
,ERROR_PEER_AGENT_NOT_SUPPORTED
,ERROR_PEER_SERVICE_NOT_SUPPORTED
,ERROR_SERVICE_NOT_SUPPORTED
,ERROR_UNKNOWN
-
-