Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Wear: Starting a service on Handheld

I'm building a Wear app that will communicate with a WearableListenerService on the handheld. However, i want to make sure that service is up and running when the app starts on the watch.

My initial thought was either send an intent or a broadcast message to get the service started. However, i've been unable to figure out how to get the watch to send that to the paired handheld instead.

On the watch side:

Intent intent = new Intent();
intent.setAction("my.wearable.CONNECT");
sendBroadcast(intent);

On the handheld side:

public class WearableBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, WearableService.class);
        context.startService(startServiceIntent);
    }
}

<receiver android:name=".service.wearable.WearableBroadcastReceiver" >
    <intent-filter>
        <action android:name="my.wearable.CONNECT" />
    </intent-filter>
</receiver>
like image 633
Matt Avatar asked Oct 30 '14 14:10

Matt


1 Answers

There are couple of things that need to clear

  1. The communication interfaces between an Android Wear Device and an Android Device are the following

    • DataApi
    • MessageApi

    Use NodeApi to get the connected Node ID.

  2. You cannot send a intent from wear side and expect to receive it on the phone side.

  3. On the phone side, if you are extending WearableListenerService then this service is self managed by Android OS. Meaning, if the service were to receive a message or data from wear, Android would automatically create the service, service your request and destroy the service if required. You do not have to do any special thing here.

The intent defined in the manifest (copy pasted above) is good enough for Android OS to do the above stated management of the service.

like image 112
Mahesh Renduchintala Avatar answered Oct 26 '22 17:10

Mahesh Renduchintala