DISTRIBUTION OF TIZEN-BASED WATCH APPS HAS BEEN DISCONTINUED

Interact with the application on the Android device

The UI scenario of this sample application works as follows:
1. Send a Hello message from Galaxy Watch to a mobile device.

Figure: Galaxy Watch sends a Hello message

2. Send a message from the Android mobile device to Galaxy Watch.

Figure: Android sends a Hi Gear!! message

Before sending a message, initialize the connection as follows.

Figure: Initialize the connection

Figure: Send messages

To manage the connection:

1. Request service agents.

Request the service agent object array with the webapis.sa.requestSAAgent() method.

function onsuccess(agents)
{
	try
	{
		if (agents.length > 0)
		{
			SAAgent = agents[0];
		}
		else
		{
			alert("Not found SAAgent!!");
		}
	}
	catch(err)
	{
		console.log("exception [" + err.name + "] msg[" + err.message + "]");
	}
}

function connect()
	{
	if (SASocket)
		{
		alert('Already connected!');

		return false;
		}
	try
	{
		webapis.sa.requestSAAgent(onsuccess, function (err)
		{
			console.log("err [" + err.name + "] msg[" + err.message + "]");
		});
	}
	catch(err)
	{
		console.log("exception [" + err.name + "] msg[" + err.message + "]");
	}
}

2. Create a service connection.

A. Set a listener to find Accessory Peer Agents by calling the setPeerAgentFindListener() method.

Search for matching Accessory Peer Agents available for establishing a service connection by calling the findPeerAgents() method.

var peerAgentFindCallback =
{
	onpeeragentfound : function(peerAgent)
	{
		try
		{
			if (peerAgent.appName == ProviderAppName)
			{
			}
			else
			{
				alert("Not expected app!! : " + peerAgent.appName);
			}
		}
		catch(err)
		{
			console.log("exception [" + err.name + "] msg[" + err.message + "]");
		}
	}, onerror : onerror
}

function onsuccess(agents)
{
	try
	{
		if (agents.length > 0)
		{
		SAAgent = agents[0];

		SAAgent.setPeerAgentFindListener(peerAgentFindCallback);
		SAAgent.findPeerAgents();
		}
		else
		{
		alert("Not found SAAgent!!");
		}
	}
	catch(err)
	{
		console.log("exception [" + err.name + "] msg[" + err.message + "]");
	}
}

B. Set a listener for connection event notifications by calling the setServiceConnectionListener() method.

Initiate a service connection with the Accessory Peer Agent by calling the requestServiceConnection() method when a matched Peer Agent is found.

var peerAgentFindCallback =
{
	onpeeragentfound : function(peerAgent)
	{
		try
		{
			if (peerAgent.appName == ProviderAppName)
			{
				SAAgent.setServiceConnectionListener(agentCallback);
				SAAgent.requestServiceConnection(peerAgent);
			}
			else
			{
				alert("Not expected app!! : " + peerAgent.appName);
			}
		}
		catch(err)
		{
			console.log("exception [" + err.name + "] msg[" + err.message + "]");
		}
	}, onerror : onerror
}

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.

var agentCallback =
{
	onconnect : function(socket)
	{
		SASocket = socket;
		createHTML("startConnection");
		SASocket.setSocketStatusListener(function(reason)
		{
			console.log("Service connection lost, Reason : [" + reason + "]");
			disconnect();
		});
	}, onerror : onerror
};

3. Disconnect the service.

A. Call the close() method to terminate the service connection with a remote Accessory Peer Agent. When the connection terminates, the onconnectionlost() callback of the SASocketStatusCallback interface 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.

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

.

SASocket.setSocketStatusListener(function(reason)
{
	console.log("Service connection lost, Reason : [" + reason + "]");
	disconnect();
});
function disconnect()
{
	try
	{
		if (SASocket != null)
		{
			SASocket.close();
			SASocket = null;
			createHTML("closeConnection");
		}
	}
	catch(err)
	{
		console.log("exception [" + err.name + "] msg[" + err.message + "]");
	}
}

4. Send a message.

The service consumer or service provider can send data to its connected Accessory Peer Agent by simply calling the sendData() method with a socket object. The sendData() method sends the message using the channel ID, which is specified in the Accessory Service Profile and must be declared in the Accessory Service Profile XML file. The message string must not be longer than the value of the maxAllowedDataSize attribute in the SAPeerAgent object.

function sendMessage(data)
{
	try
	{
		if (SASocket == null)
		{
			console.log("sap initialize");
			connect();

			return false;
		}
		SASocket.sendData(CHANNELID, data);
	}
	catch(err)
	{
		console.log("exception [" + err.name + "] msg[" + err.message + "]");
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<resources>
	<application name="MyApplication">
		<serviceProfile id="/org/example/myapp/my_message" name="MyMessage"
		                role="consumer" version="1.0">
			<supportedTransports>
				<transport type="TRANSPORT_BT" />
				<transport type="TRANSPORT_WIFI" />
			</supportedTransports>
			<serviceChannel id="110" dataRate="low" priority="low"
			                reliability="enable">
			</serviceChannel>
		</serviceProfile>
	</application>
</resources>

5. Receive a message.

Set a listener by calling the setDataReceiveListener() method. The listener is called when data is received from a remote Accessory Peer Agent. .

SASocket.setDataReceiveListener(onreceive);
function onreceive(channelId, data)
{
	createHTML(data);
}