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 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.