Java 类com.google.android.gms.wearable.NodeApi 实例源码

项目:DoNotDisturbSync    文件:WearMessageSenderService.java   
/**
 * 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());
        }
    }
}
项目:DoNotDisturbSync    文件:WearMessageSenderService.java   
/**
 * 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();
}
项目:Android-Wear-Projects    文件:MainActivity.java   
@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!");
                    }
                }
            });
}
项目:Android-Wear-Projects    文件:MainActivity.java   
@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!");
                    }
                }
            });
}
项目:Android-DFU-App    文件:ActionReceiver.java   
/**
 * 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();
}
项目:Android-DFU-App    文件:UARTCommandsActivity.java   
/**
 * 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();
}
项目:Android-DFU-App    文件:UARTService.java   
/**
 * 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();
    }
}
项目:Excuser    文件:DeviceWearConnectionFragment.java   
@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());
            }
        }
    });
}
项目:Advanced_Android_Development_Wear    文件:ConfigDataListenerService.java   
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();
}
项目:Advanced_Android_Development_Wear    文件:DigitalWatchFaceUtil.java   
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));
                }
            }
    );
}
项目:ibm-wearables-android-sdk    文件:MessageReceiverService.java   
/**
 * 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();
        }
    }
}
项目:Athletica    文件:ConfigurationHelper.java   
/**
 * 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));
                }
            }
    );
}
项目:WeaRemote    文件:Communicator.java   
@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();
            }
        }
    });
}
项目:android-samples    文件:TrackActivity.java   
/**
 * 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();
}
项目:adrenaline_watch_face    文件:WatchFaceUtil.java   
/**
 * 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));
                }
            }
    );
}
项目:adrenaline_watch_face    文件:WatchFaceUtil.java   
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));
                }
            }
    );
}
项目:ScribaNotesApp    文件:ActionReceiver.java   
/**
 * 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();
}
项目:ScribaNotesApp    文件:UARTCommandsActivity.java   
/**
 * 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();
}
项目:prefsyncservice    文件:PrefSyncService.java   
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();
                    }
                });
    }
}
项目:Smartwatch    文件:NodeUtil.java   
/**
 * 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();
                }
            });
        }
    });
}
项目:xDrip    文件:ListenerService.java   
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);
            }
        }
    });
}
项目:xDrip-plus    文件:ListenerService.java   
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);
            }
        }
    });
}
项目:Mycroft-Android    文件:MainActivity.java   
@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");
                }
            }
        }
    });
}
项目:Sensor-Data-Logger    文件:GoogleApiMessenger.java   
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());
        }
    });
}
项目:Sensor-Data-Logger    文件:GoogleApiMessenger.java   
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();
}
项目:stayawhile-android    文件:WearMessageHandler.java   
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!");
    }
项目:MirageWatch    文件:MyWatchFace.java   
@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();
}
项目:LibreAlarm    文件:WearableApi.java   
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();
}
项目:AndroidAPS    文件:SendToDataLayerThread.java   
@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;
}
项目:SunshineApp    文件:DigitalWatchFaceUtil.java   
/**
 * 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));
                }
            }
    );
}
项目:DronesWear    文件:MainActivity.java   
@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]);
            }
        }
    });
}
项目:android-wear-bilateral-communication    文件:WearActivity.java   
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();
}
项目:android-wear-bilateral-communication    文件:MainActivity.java   
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);
                            }
                        }
                    });
                }

            }
        });


    }
项目:WearExchange    文件:WearExchangeController.java   
@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;
}
项目:american_sunsets_watch_face    文件:SunsetsWatchFaceUtil.java   
/**
 * 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));
                }
            }
    );
}
项目:american_sunsets_watch_face    文件:SunsetsWatchFaceUtil.java   
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));
                }
            }
    );
}
项目:DeviceConnect-Android    文件:WearManager.java   
@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);
                        }
                    }
                }
            }
        }
    });
}
项目:DeviceConnect-Android    文件:WearManager.java   
/**
 * メッセージを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();
                }
            }
        }
    });
}
项目:TheForceMediaCenter    文件:MainActivity.java   
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();
}
项目:xDrip-Experimental    文件:ListenerService.java   
@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;
}