public abstract class SAMessage
extends java.lang.Object
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 two SAAgent
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)
and onError(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 the
ACTION_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 call
secureSend(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 in YourSubclassExtendsSAAgent().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
, and
SAAgent
. 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.
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 and Description |
---|
SAMessage(SAAgent agent)
Creates an
SAMessage instance with the specified SAAgent. |
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 like
send(SAPeerAgent, byte[]) but the message will be
encrypted internally. |
int |
send(SAPeerAgent peerAgent,
byte[] message)
Send a message to a remote Accessory Peer Agent.
|
public static final int ERROR_PEER_AGENT_UNREACHABLE
Constant Value: 1793 (0x00000701)
onError(SAPeerAgent, int, int)
,
Constant Field Valuespublic static final int ERROR_PEER_AGENT_NO_RESPONSE
Constant Value: 1794 (0x00000702)
onError(SAPeerAgent, int, int)
,
Constant Field Valuespublic static final int ERROR_PEER_AGENT_NOT_SUPPORTED
Constant Value: 1795 (0x00000703)
onError(SAPeerAgent, int, int)
,
Constant Field Valuespublic static final int ERROR_PEER_SERVICE_NOT_SUPPORTED
Constant Value: 1796 (0x00000704)
onError(SAPeerAgent, int, int)
,
Constant Field Valuespublic static final int ERROR_SERVICE_NOT_SUPPORTED
Constant Value: 1797 (0x00000705)
onError(SAPeerAgent, int, int)
,
Constant Field Valuespublic static final int ERROR_UNKNOWN
Constant Value: 1798 (0x00000706)
onError(SAPeerAgent, int, int)
,
Constant Field Valuespublic static final java.lang.String ACTION_ACCESSORY_MESSAGE_ENABLED
Constant Value: "com.samsung.accessory.action.MESSAGE_ENABLED"
public static final java.lang.String ACTION_ACCESSORY_MESSAGE_DISABLED
Constant Value: "com.samsung.accessory.action.MESSAGE_DISABLED"
public static final java.lang.String ACTION_ACCESSORY_MESSAGE_RECEIVED
Note: The broadcast receiver must be registered in the receiving application's manifest
Constant Value: "com.samsung.accessory.action.MESSAGE_RECEIVED"
public int send(SAPeerAgent peerAgent, byte[] message) throws java.io.IOException
SAAgent.findPeerAgents()
.
Sender will receive the delivery status via
onSent(SAPeerAgent peerAgent, int id)
or
onError(SAPeerAgent peerAgent, int id, int errorCode)
callback. The message will be delivered to the remote Accessory Peer
Agent through its onReceive(SAPeerAgent, byte[])
callback in its
SAMessage
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.
peerAgent
- The remote Accessory Peer Agent to receive the message.message
- The byte array of the message to be sent.java.io.IOException
- Thrown if an I/O-related error occurs while sending a message.java.lang.IllegalArgumentException
- Thrown if any arguments is invalid.onReceive(SAPeerAgent, byte[])
,
onSent(SAPeerAgent, int)
,
onError(SAPeerAgent, int, int)
public int secureSend(SAPeerAgent peerAgent, byte[] message) throws java.io.IOException
send(SAPeerAgent, byte[])
but the message will be
encrypted internally. To find a matching Accessory Peer Agent, please use
SAAgent.findPeerAgents()
.
Sender will receive the delivery status via
onSent(SAPeerAgent peerAgent, int id)
or
onError(SAPeerAgent peerAgent, int id, int errorCode)
callback. The message will be delivered to the remote Accessory Peer
Agent through its onReceive(SAPeerAgent, byte[])
callback in its
SAMessage
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.
peerAgent
- The remote Accessory Peer Agent to receive the message.message
- The byte array of the message to be sent.java.io.IOException
- Thrown if an I/O-related error occurs while sending message.java.lang.IllegalArgumentException
- Thrown if any argument is invalid.onReceive(SAPeerAgent, byte[])
,
onSent(SAPeerAgent, int)
,
onError(SAPeerAgent, int, int)
protected abstract void onReceive(SAPeerAgent peerAgent, byte[] message)
peerAgent
- The remote Accessory Peer Agent who has sent the message.message
- The byte[] messageprotected abstract void onSent(SAPeerAgent peerAgent, int id)
peerAgent
- The remote Accessory Peer Agent to receive the message.id
- ID of sending a message. It matches with the returned value
in send(SAPeerAgent, byte[])
or secureSend(SAPeerAgent, byte[])
.protected abstract void onError(SAPeerAgent peerAgent, int id, int errorCode)
peerAgent
- The remote Accessory Peer Agent to receive the message.id
- ID of sending a message. It matches with the returned value
in send(SAPeerAgent, byte[])
or secureSend(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_*.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
Copyright © Samsung Electronics, Co., Ltd. All rights reserved.