package com.algobase.service; import java.util.List; import android.os.Bundle; import android.os.Message; import android.content.Context; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import com.google.android.gms.wearable.Node; import com.google.android.gms.wearable.NodeClient; import com.google.android.gms.wearable.Wearable; import com.google.android.gms.wearable.MessageClient; import com.algobase.share.system.*; class Sender { static final String DATA_PATH = "/message_path"; public void writeLog(String msg) {} Context context; MessageClient msgClient; List nodes = null; public Sender(Context ctxt) { context = ctxt; msgClient = Wearable.getMessageClient(context); new MyThread() { public void run() { getNodes(); } }.start(); } String getConnectedDevice() { if (nodes == null || nodes.isEmpty()) return null; return nodes.get(0).getDisplayName(); } void getNodes() { // blocking nodes = null; NodeClient nodeClient = Wearable.getNodeClient(context); Task> nodeListTask = nodeClient.getConnectedNodes(); try { nodes = Tasks.await(nodeListTask); } catch(Exception ex) { } writeLog(""); if (nodes != null && !nodes.isEmpty()) { writeLog("WEARABLE CONNECTED"); writeLog(nodes.get(0).getDisplayName()); } else writeLog("WEARABLE: NOT CONNECTED."); writeLog(""); } public void sendMessage(final String msg) { new MyThread() { public void run() { if (nodes == null) getNodes(); if (nodes == null) return; for (Node node:nodes) { // send message to all connected devices (should be only one) Task sendMessageTask = msgClient.sendMessage(node.getId(),DATA_PATH,msg.getBytes()); try { Integer result = Tasks.await(sendMessageTask); } catch (Exception ex) {} } } }.start(); } }