I'm developing an Android application that interacts with a Wear OS app. I want to start a workout session on the Wear OS app directly from the phone app, even if the Wear OS app is currently killed. Specifically, when a user initiates a workout on the phone, it should automatically open and start the workout on the Wear OS app.
Phone( start a workout) -> wearos app: trigger open start a workout
Data Layer API: I've looked into the Data Layer API for communication between the phone and Wear OS, but I haven't found a way to launch the Wear OS app when it is killed.
WearableListenerService: I've implemented a WearableListenerService
to receive messages on the Wear OS app, but I need guidance on how to trigger the app to open and start a workout, even if it is not running.
// Code to send a message to Wear OS app
val node = nodes[0]
val payload = "start_workout".toByteArray()
Wearable.getMessageClient(context).sendMessage(node.id, "/start_workout", payload)
.addOnSuccessListener {
Log.d("TAG", "Message sent successfully")
}
.addOnFailureListener {
Log.d("TAG", "Failed to send message")
}
I'm 80% sure you can use the data layer api to launch the app even if it is killed, but anyway...
Another option is to use a remote Activity Helper to launch an intent on the watch.
private Node NODES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(() -> {
Task<List<Node>> nodeListTask = Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
try {
NODES = Tasks.await(nodeListTask);
} catch (Exception ignore) {}
}).start();
findViewById(R.id.button_id_here).setOnClickListener((l) -> {
// the variable "i" would be the intent of you exercise app
RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(this, Executors.newSingleThreadExecutor());
for(Node n : NODES) {
remoteActivityHelper.startRemoteActivity(i, n.getId());
}
});
}
Oh, and you will need these dependencies:
implementation 'com.google.android.gms:play-services-wearable:18.0.0'
implementation 'androidx.wear:wear-remote-interactions:1.0.0