/** * Handle the message sending action in the provided background thread. */ private void handleActionSendMessage(String mode) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); if (!(mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())) { mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT, TimeUnit.SECONDS); } NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); // Send the Do Not Disturb mode message to all devices (nodes) for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), DND_SYNC_PREFIX, mode.getBytes()).await(); if (!result.getStatus().isSuccess()){ Log.e(TAG, "Failed to send message to " + node.getDisplayName()); } else { Log.i(TAG, "Successfully sent message " + mode + " to " + node.getDisplayName()); } } }
/** * Handle the message sending action in the provided background thread. */ private void handleActionSendMessage(String mode) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); if (!(mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())) { mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT, TimeUnit.SECONDS); } NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); // Send the Do Not Disturb mode message to all devices (nodes) for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), DND_SYNC_PREFIX, mode.getBytes()).await(); if (!result.getStatus().isSuccess()){ Log.e(TAG, "Failed to send message to " + node.getDisplayName()); } else { Log.i(TAG, "Successfully sent message " + mode + " to " + node.getDisplayName()); } } //mGoogleApiClient.disconnect(); }
@Override public void onConnected(@Nullable Bundle bundle) { Wearable.NodeApi.getConnectedNodes(mGoogleApiClient) .setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult nodes) { for (Node node : nodes.getNodes()) { if (node != null && node.isNearby()) { mNode = node; Log.d("packtchat", "Connected to " + mNode.getDisplayName()); } } if (mNode == null) { Log.d("packtchat", "Not connected!"); } } }); }
/** * Sends the given message to the handheld. * @param path message path * @param message the message */ private void sendMessageToHandheld(final @NonNull Context context, final @NonNull String path, final @NonNull String message) { new Thread(new Runnable() { @Override public void run() { final GoogleApiClient client = new GoogleApiClient.Builder(context) .addApi(Wearable.API) .build(); client.blockingConnect(); final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(client).await(); for(Node node : nodes.getNodes()) { final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(client, node.getId(), path, message.getBytes()).await(); if (!result.getStatus().isSuccess()){ Log.w(TAG, "Failed to send " + path + " to " + node.getDisplayName()); } } client.disconnect(); } }).start(); }
/** * Sends the given command to the handheld. * * @param command the message */ private void sendMessageToHandheld(final @NonNull Context context, final @NonNull String command) { new Thread(new Runnable() { @Override public void run() { final GoogleApiClient client = new GoogleApiClient.Builder(context) .addApi(Wearable.API) .build(); client.blockingConnect(); final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(client).await(); for (Node node : nodes.getNodes()) { final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(client, node.getId(), Constants.UART.COMMAND, command.getBytes()).await(); if (!result.getStatus().isSuccess()) { Log.w(TAG, "Failed to send " + Constants.UART.COMMAND + " to " + node.getDisplayName()); } } client.disconnect(); } }).start(); }
/** * Sends the given message to all connected wearables. If the path is equal to {@link Constants.UART#DEVICE_DISCONNECTED} the service will be stopped afterwards. * @param path message path * @param message the message */ private void sendMessageToWearables(final @NonNull String path, final @NonNull String message) { if(mGoogleApiClient.isConnected()) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); for(Node node : nodes.getNodes()) { Logger.v(getLogSession(), "[WEAR] Sending message '" + path + "' to " + node.getDisplayName()); final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await(); if(result.getStatus().isSuccess()){ Logger.i(getLogSession(), "[WEAR] Message sent"); } else { Logger.w(getLogSession(), "[WEAR] Sending message failed: " + result.getStatus().getStatusMessage()); Log.w(TAG, "Failed to send " + path + " to " + node.getDisplayName()); } } if (Constants.UART.DEVICE_DISCONNECTED.equals(path)) stopService(); } }).start(); } else { if (Constants.UART.DEVICE_DISCONNECTED.equals(path)) stopService(); } }
@SuppressLint("LongLogTag") private void findAllWearDevices() { Log.d(TAG, "findAllWearDevices()"); PendingResult<NodeApi.GetConnectedNodesResult> pendingResult = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient); pendingResult.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) { if (getConnectedNodesResult.getStatus().isSuccess()) { mAllConnectedNodes = getConnectedNodesResult.getNodes(); verifyNodeAndUpdateUI(); Log.e("Connected Nodes", "->"+mAllConnectedNodes.toString()); findWearDevicesWithApp(); } else { Log.d(TAG, "Failed NodeApi: " + getConnectedNodesResult.getStatus()); } } }); }
private void sendMessageToDevice(final String key) { new Thread(new Runnable() { @Override public void run() { if (mGoogleApiClient == null) { mGoogleApiClient = (new com.google.android.gms.common.api.GoogleApiClient.Builder(ConfigDataListenerService.this)).addConnectionCallbacks(ConfigDataListenerService.this).addOnConnectionFailedListener(ConfigDataListenerService.this).addApi(Wearable.API).build(); } NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); if (nodes != null && nodes.getNodes() != null) { for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), key, null).await(); if (!result.getStatus().isSuccess()) { Log.e(TAG, "Error during sending message"); } else { Log.i(TAG, "Success!! sent to: " + node.getDisplayName()); } } } } }).start(); }
public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback, final String path) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(path) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
/** * Handle messages from the phone * @param messageEvent new message from the phone */ @Override public void onMessageReceived(MessageEvent messageEvent) { if (messageEvent.getPath().equals("/start")) { dataManager.turnSensorOn(Integer.valueOf(new String(messageEvent.getData()))); } else if (messageEvent.getPath().equals("/stop")){ dataManager.turnSensorOff(Integer.valueOf(new String(messageEvent.getData()))); } else if (messageEvent.getPath().equals("/stopAll")){ dataManager.turnAllSensorsOff(); } else if (messageEvent.getPath().equals("/ping")){ NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(apiClient).await(); for(Node node : nodes.getNodes()) { Wearable.MessageApi.sendMessage(apiClient, node.getId(), "/connected", null).await(); } } }
/** * Asynchronously fetches the current config {@link DataMap} for {@link com.dimitrioskanellopoulos.athletica.WatchFaceService} * and passes it to the given callback. * <p/> * If the current config {@link DataItem} doesn't exist, it isn't created and the callback * receives an empty DataMap. */ public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(@NonNull NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(ConfigurationHelper.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
@Override public void onConnected(@Nullable Bundle bundle) { Log.v("app", "onConnected"); Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) { int nodesSize = getConnectedNodesResult.getNodes().size(); Log.v("app", "getConnectedNodes: " + nodesSize); if (nodesSize > 0) { mNode = getConnectedNodesResult.getNodes().get(0); sendConnected(); //createChannel(); } } }); }
/** * Send the current step count to the phone. This does not require guarantee of message delivery. As, if * the message is not delivered, the count will get updated when second message gets delivered. * So, we are using messages api for passing the data that are usful at the current moment only. * <p> * <B>Note: </B> Messages will block the UI thread while sending. So, we should send messages in background * thread only. * * @param stepCount current step count. */ private void sendStepCountMessage(final String stepCount) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( mGoogleApiClient, node.getId(), STEP_COUNT_MESSAGES_PATH, stepCount.getBytes()).await(); //check if the message is delivered? Log.d("Messages Api", result.getStatus().isSuccess() ? "Sent successfully" : "Sent failed."); } } }).start(); }
/** * Asynchronously fetches the current config {@link DataMap} for {@link SonicBoomFace} * and passes it to the given callback. * <p> * If the current config {@link DataItem} doesn't exist, it isn't created and the callback * receives an empty DataMap. */ public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(WatchFaceUtil.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(WatchFaceUtil.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
public PrefHandler(Context context) { appContext = context.getApplicationContext(); googleApiClient = new GoogleApiClient.Builder(appContext) .addApi(Wearable.API) .addConnectionCallbacks(this) .build(); googleApiClient.connect(); if (localNodeID == null) { Wearable.NodeApi.getLocalNode(googleApiClient) .setResultCallback(new ResultCallback<NodeApi.GetLocalNodeResult>() { public void onResult(@NonNull NodeApi.GetLocalNodeResult getLocalNodeResult) { localNodeID = getLocalNodeResult.getNode().getId(); } }); } }
/** * Method to get all the remote node Ids * * @param googleApiClient * @return Observable that emits every remotenodeId */ public static Observable<String> getRemoteNodeId(final GoogleApiClient googleApiClient) { return Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(final Subscriber<? super String> subscriber) { Wearable.NodeApi.getConnectedNodes(googleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) { for (Node node : getConnectedNodesResult.getNodes()) { subscriber.onNext(node.getId()); } subscriber.onCompleted(); } }); } }); }
private void setLocalNodeName () { forceGoogleApiConnect(); PendingResult<NodeApi.GetLocalNodeResult> result = Wearable.NodeApi.getLocalNode(googleApiClient); result.setResultCallback(new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { if (!getLocalNodeResult.getStatus().isSuccess()) { Log.e(TAG, "ERROR: failed to getLocalNode Status=" + getLocalNodeResult.getStatus().getStatusMessage()); } else { Log.d(TAG, "getLocalNode Status=: " + getLocalNodeResult.getStatus().getStatusMessage()); Node getnode = getLocalNodeResult.getNode(); localnode = getnode != null ? getnode.getDisplayName() + "|" + getnode.getId() : ""; Log.d(TAG, "setLocalNodeName. localnode=" + localnode); } } }); }
@Override public void onConnected(@Nullable Bundle bundle) { Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) { for(Node node : getConnectedNodesResult.getNodes()) { if(node != null && node.isNearby()) { mNode = node; showToast("Connected To "+ node.getDisplayName()); Log.d(WEARABLE_MAIN,"Connected to " + node.getDisplayName()); } else { showToast("Not Connected"); Log.d(WEARABLE_MAIN,"NOT CONNECTED"); } } } }); }
public void updateLastConnectedNodes() { getConnectedNodes(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) { status.setLastConnectedNodes(getConnectedNodesResult.getNodes()); status.setLastConnectedNodesUpdateTimestamp(System.currentTimeMillis()); status.updated(statusUpdateHandler); StringBuilder sb = new StringBuilder(); sb.append("Connected Nodes:"); for (Node connectedNode : status.getLastConnectedNodes()) { sb.append("\n - ").append(connectedNode.getDisplayName()); sb.append(": ").append(connectedNode.getId()); if (connectedNode.isNearby()) { sb.append(" (nearby)"); } } Log.v(TAG, sb.toString()); } }); }
public void sendMessageToNearbyNodes(final String path, final byte[] data) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult getConnectedNodesResult = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); status.setLastConnectedNodes(getConnectedNodesResult.getNodes()); status.setLastConnectedNodesUpdateTimestamp(System.currentTimeMillis()); for (Node node : status.getLastConnectedNodes()) { try { if (!node.isNearby()) { continue; } sendMessageToNodeWithResult(path, data, node.getId()); } catch (Exception ex) { Log.w(TAG, "Unable to send message to node: " + ex.getMessage()); } } } }).start(); }
public void sendMessage( final String path, final byte[] data ) { new Thread( new Runnable() { @Override public void run() { while (mGoogleApiClient == null) initGoogleApi(); Log.i("DEVm", "Getting nodes!"); if (nodes == null) nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); Log.i("DEVm", "Got " + nodes.getNodes().size() + " nodes!"); for(Node node : nodes.getNodes()) { Log.i("DEVm", "Sent message! Path: " + path + " Data: " + new String(data)); Wearable.MessageApi.sendMessage( mGoogleApiClient, node.getId(), path, data ).await(); } } }).start(); Log.i("DEVm","Should've sent message!"); }
@Override public void onConnected(Bundle bundle) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult connectedNodesResult = Wearable.NodeApi.getConnectedNodes(mApiClient).await(); String tag = MyWatchFace.class.getSimpleName(); for (Node node : connectedNodesResult.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( mApiClient, node.getId(), "/RequestBatteryLevel", "RequestBatteryLevel".getBytes()) .await(); if (result.getStatus().isSuccess()) { Log.d(tag, "バッテリーレベル要求の送信に成功"); } else { Log.d(tag, "バッテリーレベル要求の送信に失敗 (" + result.getStatus().getStatusMessage() + ")"); } } } }).start(); }
public static void sendMessage(final GoogleApiClient client, final String command, final byte[] message, final ResultCallback<MessageApi.SendMessageResult> listener) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( client ).await(); for(Node node : nodes.getNodes()) { Log.i(TAG, "sending to " + node.getId() + ", command: " + command); PendingResult<MessageApi.SendMessageResult> pR = Wearable.MessageApi.sendMessage(client, node.getId(), command, message); if (listener != null) pR.setResultCallback(listener); } } }).start(); }
@Override protected Void doInBackground(DataMap... params) { try { final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(15, TimeUnit.SECONDS); for (Node node : nodes.getNodes()) { for (DataMap dataMap : params) { PutDataMapRequest putDMR = PutDataMapRequest.create(path); putDMR.getDataMap().putAll(dataMap); PutDataRequest request = putDMR.asPutDataRequest(); DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleApiClient, request).await(15, TimeUnit.SECONDS); if (result.getStatus().isSuccess()) { Log.d(TAG, "DataMap: " + dataMap + " sent to: " + node.getDisplayName()); } else { Log.d(TAG, "ERROR: failed to send DataMap"); } } } } catch (Exception e) { Log.e(TAG, "Got exception sending data to wear: " + e.toString()); } return null; }
/** * Asynchronously fetches the current config {@link DataMap} for { WatchFace} * and passes it to the given callback. * <p> * If the current config {@link DataItem} doesn't exist, it isn't created and the callback * receives an empty DataMap. */ public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(DigitalWatchFaceUtil.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
@Override public void onConnected(Bundle bundle) { Wearable.DataApi.addListener(mGoogleApiClient, this); if (mDrone != null) { Message.sendActionTypeMessage(mDrone.getCurrentAction(), mGoogleApiClient); } sendInteractionType(); // launch the app on the wear Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback( new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) { for (Node node : getConnectedNodesResult.getNodes()) { Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), Message.OPEN_ACTIVITY_MESSAGE, new byte[0]); } } }); }
private void sendMessage(final String path, final String message) { new Thread( new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await(); for(Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( mApiClient, node.getId(), path, message.getBytes() ).await(); } runOnUiThread( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "m to phon" + message, Toast.LENGTH_SHORT).show(); } }); } }).start(); }
private void sendAsyncMessage(final String path, final String message) { Wearable.NodeApi.getConnectedNodes( mApiClient ).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult nodes) { for(Node node : nodes.getNodes()) { Wearable.MessageApi.sendMessage( mApiClient, node.getId(), path, message.getBytes() ).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) { if (sendMessageResult.getStatus().isSuccess()){ showToastMT(message); } } }); } } }); }
@Override protected Void doInBackground(Void... voids) { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); if (wearExchangeInterface == null || googleApiClient == null) { return null; } if (nodes != null && !nodes.getNodes().isEmpty()) { wearExchangeInterface.wearConnectionMade(nodes.getNodes().get(0).getId()); } else { wearExchangeInterface.wearConnectionLost(NO_NODE_ID); } return null; }
/** * Asynchronously fetches the current config {@link DataMap} for {@link SunsetsWatchFace} * and passes it to the given callback. * <p> * If the current config {@link DataItem} doesn't exist, it isn't created and the callback * receives an empty DataMap. */ public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(SunsetsWatchFaceUtil.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
public static void fetchConfigDataMap(final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client).setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(SunsetsWatchFaceUtil.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } } ); }
@Override public void onConnected(final Bundle bundle) { setNodeListener(); setMessageListener(); getNodes(new OnNodeResultListener() { @Override public void onResult(final NodeApi.GetConnectedNodesResult result) { List<Node> nodes = result.getNodes(); if (nodes != null) { synchronized (mNodeCache) { for (Node node : nodes) { if (!mNodeCache.containsKey(node.getId())) { mNodeCache.put(node.getId(), node); mLogger.info("getNodes: name = " + node.getDisplayName() + ", id = " + node.getId()); notifyOnNodeConnected(node); } } } } } }); }
/** * メッセージをWearに送信する. * * @param dest 送信先のWearのnodeId * @param action メッセージのアクション * @param message メッセージ * @param listener メッセージを送信した結果を通知するリスナー */ public void sendMessageToWear(final String dest, final String action, final String message, final OnMessageResultListener listener) { sendMessageToWear(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); MessageApi.SendMessageResult result = null; for (Node node : nodes.getNodes()) { if (node.getId().indexOf(dest) != -1) { result = Wearable.MessageApi.sendMessage( mGoogleApiClient, node.getId(), action, message.getBytes()).await(); } } if (result != null) { if (listener != null) { listener.onResult(result); } } else { if (listener != null) { listener.onError(); } } } }); }
private void retrieveDeviceNode() { final GoogleApiClient client = getGoogleApiClient(this); new Thread(new Runnable() { @Override public void run() { client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(client).await(); List<Node> nodes = result.getNodes(); if (nodes.size() > 0) { nodeId = nodes.get(0).getId(); } client.disconnect(); } }).start(); }
@Override protected Void doInBackground(Void... params) { if (googleApiClient.isConnected()) { if (System.currentTimeMillis() - lastRequest > 20 * 1000) { // enforce 20-second debounce period lastRequest = System.currentTimeMillis(); NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); for (Node node : nodes.getNodes()) { Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), WEARABLE_RESEND_PATH, null); } } } else googleApiClient.connect(); return null; }