Send notification to Gear2 app

gear2
Gear 2 running Tizen so far is the most feature-packed smart watch in the market, however Samsung’s dev document is a bit scattered. I’m baffled when I searched and asked everywhere and couldn’t find the docs on an important feature like push notification. After hours of reverse engineering Samsung’s accessories sdk jar files, I got to write this post. Enough said, here it is!

Updated: Link to working code and instruction to test is at the bottom of this blog.

Key things:

– Your Gear2 app must allow running in the background, to wake up when receiving a notification from the host app.

– It must have this callback function: onrequest.  The function’s job is to handle the connection request from host app.

– Your host app must call findPeerAgents function to initiate connection request with Gear2 app.

I’m assuming you have followed the video tutorials on building an integrated app at Samsung developer blog and grab the source code from there (at the top of that page) and do the modifications per below.

Details, step by step:

Step 1: on Gear2 app, allow it to run in the background.

Add these below lines in your config.xml file

<tizen:privilege name="http://tizen.org/privilege/application.launch" />
<tizen:setting background-support="enable" encryption="disable"
 hwkey-event="enable" />

Step 2: on Gear2 app, adding onrequest function to callback.

First, check to make sure your onsuccess() function set the connection listener, like below

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

Add onrequest function in agentCallback. In onconnect, make sure to set the data receiver to another callback, in this case the function onreceive


var agentCallback = {
	onrequest : function(peerAgent) {
		//accept connection if there is a request from host app
		SAAgent.acceptServiceConnectionRequest(peerAgent);
	},
	onconnect : function(socket) {
		console.log("agentCallback onconnect" + socket);
		
                SASocket = socket;
		SASocket.setDataReceiveListener(onreceive);
		SASocket.setSocketStatusListener(function(reason) {
			console.log("Service connection lost, Reason : [" + reason + "]");
			disconnect();
		});
                //do whatever logic you want like setting up GUI, changing text
	},
	onerror : onerror
};

And of course, make sure you really handle the data in onreceive

function onreceive(channelId, data) {
       //logic here
}

Step 3: Android app calls findPeerAgent to start connection
I’m assuming have already added the compiled Gear2 app to the asset folder of your Android project, and have configured the sapservices.xml file.
You need to trigger the action of finding the gear app by calling findPeerAgent, it’s up to you where to put in your real service. In my example, I put it in onStartCommand when the service starts

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        int result = super.onStartCommand(intent, i, i2);
        findPeerAgents();
        return  result;
    }

And when there is a reply from the Gear2 app, request a connection.

@Override
    protected void onFindPeerAgentResponse(SAPeerAgent arg0, int arg1) {
        Log.d(TAG, "onFindPeerAgentResponse  arg0 =" + arg0);
        try{
            requestServiceConnection(arg0);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

This explains how the function onrequest in Step 2 plays out. Without that function, the Gear2 app cannot passively wait and connect to the host app. Make sure you don’t call requestServiceConnection several times in a row, it will complains “Connection in progress”.
Note, you must open the Tizen app at least once, even without connection to the host app. The reason is it needs to call function onsuccess once and set the listener.

That’s it, enjoy your push notification!

Update:
If you want to see how it works for my company’s app, see here https://www.youtube.com/watch?v=ylcRj3b2iTU

Link to working code (stripped down version of the above app) is here: https://github.com/tungle/Gear2Push.
You must open the Gear app at least once, even without connect to host app, you can close it too. Later on host app, just hit ‘Send message’. Magic will happen 🙂
Have fun!

Advertisements