If the broadcast receiver is not added in the AndroidManifest.xml file, no intents handled by the Samsung Accessory Service Framework are delivered to your application.
The action names are changed from 2.3.0. For backward compatibility, old actions are not supported anymore in latest SDK.
Communicating with a remote Peer Agent requires the service declaration in the AndroidManifest.xml file. SAService can handle receiving request of service connections. When the tested devices are O OS or above, SAService should be running in foreground because of the background limitation. This ensures that the application is derived from the SAAgentV2 class.
SAAgentV2 is a new version of SAAgent to support Android O OS or above without running in foreground. It has same functionalities as SAAgent but code generation has few differences from SAAgent. The SAAgentV2 class does not extend the Android service. SAService can be executed in a worker thread and make SAAgentV2 to handle asynchronous Accessory-related intents. For devices having O OS or above, please add foreground permission to manifest and make sure your application has an icon which will be used by notification for foreground service.
3. Defining the Accessory Service Profile
Communicating with a remote Peer Agent requires the declaration of descriptions about the Accessory Service Profile. This is declared in a separate file in the /res/xml folder in your application project. The path of the actual XML file can be added in the application’s AndroidManifest.xml file.
When the application is installed, the Samsung Accessory Service Framework automatically registers its Accessory Peer Agents using the information specified in your service profile XML file. Similarly, the Accessory Peer Agents are deregistered when the application is uninstalled. An error log is dumped if the registration process fails to register the Accessory Service Profile implementation. To define the Accessory Service Profile, see Validating Accessory Service Profile XML in this.
4. Setting up the Service Connection
If the application wants to establish a service connection with only one Accessory Peer Agent, check the first callback. You can also check the identity or properties of the discovered Accessory Peer Agents by calling the functions provided by the SAPeerAgent class to decide which Accessory Peer Agent you want to form a service connection with. The application can initiate a service connection with an Accessory Peer Agent by calling the SAAgentV2.requestServiceConnection() function.
This function is called from a worker thread. If you need to do any heavy lifting or long latency work in the callback, spawn a separate thread.
If a service provider connects only with a specific service consumer, or a service consumer with a specific service provider, the service provider and consumer are called ”companion applications”. When you only want to connect to a companion service provider or service consumer, call the functions provided by the SAPeerAgent class for specific information, such as model number or vendor information, before calling the SAAgentV2.requestServiceConnection() function. For example, when a photo printer service provider on an accessory device from a company only wants to connect to a photo printer service consumer on a smart device from the same company, they are companion applications.
The remote Accessory Peer Agent either accepts or rejects your service connection request. Your application is notified with the SAAgentV2.onServiceConnectionResponse() callback. The request can be accepted (and a service connection established) or rejected, or it can fail to establish the service connection for other reasons.
When a service connection is successfully established, the requesting Accessory Peer Agent gets an instance of the SASocket object, which is used to handle service connection events and send or receive data to and from Accessory Peer Agents.
public class ProviderService extends SAAgentV2
{
private static final int CHANNEL_ID = 110;
private ProviderServiceSocket mProviderServiceSocket = null;
public ProviderService(Context context)
{
super(TAG, context, ProviderServiceSocket.class);
}
@Override
public void
onCreate()
{
super.onCreate();
SA mAccessory = new SA();
try
{
mAccessory.initialize(this);
}
catch (SsdkUnsupportedException e)
{
// try to handle SsdkUnsupportedException
}
catch (Exception e1)
{
e1.printStackTrace();
stopSelf();
}
}
@Override
protected void
onServiceConnectionResponse(SAPeerAgent peerAgent, SASocket socket, int result)
{
if (result == SAAgentV2.CONNECTION_SUCCESS)
{
if (socket != null)
{
mProviderServiceSocket = (ProviderServiceSocket) socket;
Toast.makeText(getBaseContext(), "Galaxy Watch connection is established",
Toast.LENGTH_SHORT).show();
}
}
else if (result == SAAgentV2.CONNECTION_ALREADY_EXIST)
{
Toast.makeText(getBaseContext(), "Galaxy Watch connection already exists",
Toast.LENGTH_SHORT).show();
}
}
public class ProviderServiceSocket extends SASocket
{
}
}
5. Handling the Setup Service Connection Request
The service provider or consumer application is notified with the SAAgentV2.onServiceConnectionRequested() callback when a remote Accessory Peer Agent wants to create a service connection with it:
The Accessory Peer Agent implementation can accept or reject service connection requests by calling the acceptServiceConnectionRequest() or rejectServiceConnectionRequest() function.
The default implementation of the SAAgentV2.onServiceConnectionRequested() callback is to accept every incoming service connection request from any remote Accessory Peer Agent. Your Accessory Peer Agent implementation can override this function, usually to check the identity and properties of the requesting remote Accessory Peer Agent before accepting or rejecting incoming service connection requests.
The SAAgentV2.onServiceConnectionRequested() callback can check for Accessory Peer Agent-specific information before accepting service connection requests. You can use the SAPeerAgent object functions for checking specific information, such as application name or vendor ID.
If your application accepts the service connection request, your application is notified through the SAAgentV2.onServiceConnectionResponse() callback when the service connection is established or a failure occurs. On success, an SASocket object is passed with the callback. If you want to implement a service provider application that can serve multiple service consumer applications at the same time, keep a repository of the SASocket objects for all active service connections, and give an identifier for each SASocket object.
The SAAgentV2.onServiceConnectionResponse() callback is called from a worker thread. If you need to do any heavy lifting or long latency work in the callback, spawn a separate thread.
Call the SASocket.send() function of the SASocket object (passed with the SAAgentV2.onServiceConnectionResponse()callback) to send data on the selected service channel inside an established service connection. The Samsung Accessory Service Framework provides a datagram service. Either all the data is sent or nothing is sent. The service connection encapsulates all service channels as defined by the Accessory Service Profile specification.
public void sendMessage(final String message)
{
if (mProviderServiceSocket != null)
{
new Thread(new Runnable()
{
public void run()
{
try
{
mProviderServiceSocket.send(CHANNEL_ID, message.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}).start();
}
}
public class ProviderServiceSocket extends SASocket
{
@Override
public void onReceive(int channelId, byte[] data)
{
final String message = new String(data);
if (mProviderServiceListener != null && !mProviderServiceListener.isActivityHidden())
{
mProviderServiceListener.onReceiveMessage(message);
}
else
{
Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
sendMessage("I'm sleeping");
}
}
}
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.