static bool
_initialize_agent(sap_info_s* priv)
{
int result_agent = 0;
do
{
result_agent = sap_agent_initialize(priv->agent,
_MY_APPLICATION_ASPID,
SAP_AGENT_ROLE_CONSUMER,
_on_agent_initialized, priv);
} while (result_agent != SAP_RESULT_SUCCESS);
return true;
}
B. The _on_device_status_changed() callback function is triggered when the device status changes. All valid sap_peer_agent and sap_socket handles must be destroyed when the callback is called with the SAP_DEVICE_STATUS_DETACHED device disconnected status.
static void
_on_peer_agent_updated(sap_peer_agent_h peer_agent,
sap_peer_agent_status_e peer_status,
sap_peer_agent_found_result_e result,
void *user_data)
{
sap_info_s *priv = (sap_info_s*)user_data;
switch (result)
{
case SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED:
break;
case SAP_PEER_AGENT_FOUND_RESULT_FOUND:
if (peer_status == SAP_PEER_AGENT_STATUS_AVAILABLE)
{
priv->peer_agent = peer_agent;
g_idle_add(_create_service_connection, priv);
}
else
{
sap_peer_agent_destroy(peer_agent);
}
break;
case SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND:
break;
case SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT:
break;
case SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR:
break;
default:
break;
}
}
B. Initiate a service connection with the Accessory Peer Agent by calling the sap_agent_request_service_connection() function when a matched Peer Agent is found.
C. When the service connection has been successfully established, the requesting Accessory Peer Agent gets a socket that can be used both to send and receive data to and from Accessory Peer Agents, and to handle service connection-related events.
D. Use the sap_socket_set_data_received_cb() function to register the on_data_received() callback that is triggered when data is received on the socket.
static void
_on_service_connection_created(sap_peer_agent_h peer_agent,
sap_socket_h socket,
sap_service_connection_result_e result,
void *user_data)
{
sap_info_s* priv = (sap_info_s*)user_data;
switch (result)
{
case SAP_CONNECTION_SUCCESS:
sap_peer_agent_set_service_connection_terminated_cb(priv->peer_agent,
_on_service_connection_terminated,
priv);
sap_socket_set_data_received_cb(socket, _on_data_received, priv);
priv->socket = socket;
show_message("Connection Established", priv->user_data);
break;
case SAP_CONNECTION_ALREADY_EXIST:
priv->socket = socket;
show_message("Connection Already Exist", priv->user_data);
break;
case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
break;
case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
break;
case SAP_CONNECTION_FAILURE_NETWORK:
break;
case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
break;
case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
break;
case SAP_CONNECTION_FAILURE_UNKNOWN:
break;
}
}
3. Disconnect the service.
Call the sap_peer_agent_terminate_service_connection() function to terminate the service connection with a remote Accessory Peer Agent. When the connection terminates, the on_service_connection_terminated() callback is triggered. Similarly, if the remote Accessory Peer Agent terminates the service connection, your application is notified through the same callback, and all established service channels of the service connection are closed by the Accessory Service Framework.
Other reasons, such as a network failure or 2 devices becoming out of range while on a wireless connection, can also result in the loss of the service connection. Your service provider and service consumer applications are expected to implement handling mechanisms in the callback for such cases.
The service consumer or service provider can send data to its connected Accessory Peer Agent by simply calling the sap_socket_send_data() method with a socket handle (passed by the sap_service_connection_established_type callback) and a service channel ID. The data is sent on a selected service channel inside the established service connection.
The _on_data_received() callback is set initially after the service connection is made using the sap_set_on_data_receive_cb() callback.
static void
_on_data_received(sap_socket_h socket,
unsigned short int channel_id,
unsigned int payload_length,
void *buffer,
void *user_data)
{
sap_info_s *priv = (sap_info_s*)user_data;
show_message(buffer, priv->user_data);
}
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.