Java Code Examples for com.google.android.gms.wearable.NodeApi#GetConnectedNodesResult

The following examples show how to use com.google.android.gms.wearable.NodeApi#GetConnectedNodesResult . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageReceiverService.java    From ibm-wearables-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 2
Source File: WatchMainActivity.java    From android-wear-gopro-remote with Apache License 2.0 6 votes vote down vote up
void findPhoneNode() {
    PendingResult<NodeApi.GetConnectedNodesResult> pending = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
    pending.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(NodeApi.GetConnectedNodesResult result) {
            if(result.getNodes().size()>0) {
                mPhoneNode = result.getNodes().get(0);
                if(Logger.DEBUG) Logger.debug(TAG, "Found wearable: name=" + mPhoneNode.getDisplayName() + ", id=" + mPhoneNode.getId());
                sendConnectMessage();
            } else {
                mPhoneNode = null;
                updateStatusUi(null);
            }
        }
    });
}
 
Example 3
Source File: ListenerService.java    From xDrip-Experimental with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: QuizReportActionService.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
@Override
public void onHandleIntent(Intent intent) {
    if (intent.getAction().equals(ACTION_RESET_QUIZ)) {
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();
        ConnectionResult result = googleApiClient.blockingConnect(CONNECT_TIMEOUT_MS,
                TimeUnit.MILLISECONDS);
        if (!result.isSuccess()) {
            Log.e(TAG, "QuizListenerService failed to connect to GoogleApiClient.");
            return;
        }
        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
        for (Node node : nodes.getNodes()) {
            Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), RESET_QUIZ_PATH,
                    new byte[0]);
        }
    }
}
 
Example 5
Source File: DataUtils.java    From wear with MIT License 6 votes vote down vote up
protected Void doInBackground(Object... params) {
    GoogleApiClient client = (GoogleApiClient)params[0];

    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi
            .getConnectedNodes(client)
            .await();
    for (Node node : nodes.getNodes()) {
        MessageApi.SendMessageResult result = Wearable.MessageApi
                .sendMessage(client, node.getId(), (String)params[1],
                        ((String)params[2]).getBytes())
                .await();
        if (!result.getStatus().isSuccess()) {
            Log.e(TAG, "could not send message (" + result.getStatus() + ")");
        }
    }

    return null;
}
 
Example 6
Source File: SendByteArrayToNode.java    From BusWear with Apache License 2.0 6 votes vote down vote up
public void run() {
    GoogleApiClient googleApiClient = SendWearManager.getInstance(context);
    googleApiClient.blockingConnect(WearBusTools.CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    for (Node node : nodes.getNodes()) {
        MessageApi.SendMessageResult result;
        if (sticky) {
            result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), WearBusTools.MESSAGE_PATH_STICKY + WearBusTools.CLASS_NAME_DELIMITER + clazzToSend.getName(), objectArray).await();
        } else {
            result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), WearBusTools.MESSAGE_PATH + WearBusTools.CLASS_NAME_DELIMITER + clazzToSend.getName(), objectArray).await();
        }
        if (!result.getStatus().isSuccess()) {
            Log.v(WearBusTools.BUSWEAR_TAG, "ERROR: failed to send Message via Google Play Services to node " + node.getDisplayName());
        }
    }
}
 
Example 7
Source File: SendByteArrayToNode.java    From ExceptionWear with Apache License 2.0 6 votes vote down vote up
public void run() {
    if ((objectArray.length / 1024) > 100) {
        throw new RuntimeException("Object is too big to push it via Google Play Services");
    }
    GoogleApiClient googleApiClient = SendWearManager.getInstance(context);
    googleApiClient.blockingConnect(WearExceptionTools.CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    for (Node node : nodes.getNodes()) {
        MessageApi.SendMessageResult result;
        result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), WearExceptionTools.MESSAGE_EXCEPTION_PATH, objectArray).await();

        if (!result.getStatus().isSuccess()) {
            Log.v(WearExceptionTools.EXCEPTION_WEAR_TAG, "ERROR: failed to send Message via Google Play Services");
        }
    }
}
 
Example 8
Source File: SendToDataLayerThread.java    From NightWatch with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Void doInBackground(DataMap... params) {
    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    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();
            if (result.getStatus().isSuccess()) {
                Log.d("SendDataThread", "DataMap: " + dataMap + " sent to: " + node.getDisplayName());
            } else {
                Log.d("SendDataThread", "ERROR: failed to send DataMap");
            }
        }
    }

    return null;
}
 
Example 9
Source File: Utils.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * Get a list of all wearable nodes that are connected synchronously.
 * Only call this method from a background thread (it should never be
 * called from the main/UI thread as it blocks).
 */
public static Collection<String> getNodes(GoogleApiClient client) {
    Collection<String> results= new HashSet<String>();
    NodeApi.GetConnectedNodesResult nodes =
            Wearable.NodeApi.getConnectedNodes(client).await();
    for (Node node : nodes.getNodes()) {
        results.add(node.getId());
    }
    return results;
}
 
Example 10
Source File: Utils.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Get a list of all wearable nodes that are connected synchronously.
 * Only call this method from a background thread (it should never be
 * called from the main/UI thread as it blocks).
 */
public static Collection<String> getNodes(GoogleApiClient client) {
    Collection<String> results= new HashSet<String>();
    NodeApi.GetConnectedNodesResult nodes =
            Wearable.NodeApi.getConnectedNodes(client).await();
    for (Node node : nodes.getNodes()) {
        results.add(node.getId());
    }
    return results;
}
 
Example 11
Source File: MainActivity.java    From AndroidWearable-Samples with Apache License 2.0 5 votes vote down vote up
private Collection<String> getNodes() {
    HashSet<String> results = new HashSet<String>();
    NodeApi.GetConnectedNodesResult nodes =
            Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();

    for (Node node : nodes.getNodes()) {
        results.add(node.getId());
    }

    return results;
}
 
Example 12
Source File: SendCommandToNode.java    From BusWear with Apache License 2.0 5 votes vote down vote up
public void run() {
    GoogleApiClient googleApiClient = SendWearManager.getInstance(context);
    googleApiClient.blockingConnect(WearBusTools.CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    for (Node node : nodes.getNodes()) {
        MessageApi.SendMessageResult result;
        result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), path + WearBusTools.CLASS_NAME_DELIMITER + clazzToSend.getName(), objectArray).await();
        if (!result.getStatus().isSuccess()) {
            Log.v(WearBusTools.BUSWEAR_TAG, "ERROR: failed to send Message via Google Play Services to node " + node.getDisplayName());
        }
    }
}
 
Example 13
Source File: MobileMainActivity.java    From android-wear-gopro-remote with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnected(Bundle bundle) {
    PendingResult<NodeApi.GetConnectedNodesResult> pending = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
    pending.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(NodeApi.GetConnectedNodesResult result) {
            if(result.getNodes().size()>0) {
                mWearNode = result.getNodes().get(0);
            } else {
                mWearNode = null;
            }
        }
    });
}
 
Example 14
Source File: WearMessageListenerService.java    From AnkiDroid-Wear with GNU General Public License v2.0 5 votes vote down vote up
private void fireMessage(final byte[] data, final String path,final int retryCount) {

        PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient);
        nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult result) {
                for (int i = 0; i < result.getNodes().size(); i++) {
                    Node node = result.getNodes().get(i);
                    Log.v(TAG, "Phone firing message with path : " + path);

                    PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
                            path, data);
                    messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            Status status = sendMessageResult.getStatus();
                            Timber.d("Status: " + status.toString());
                            if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {
                                if(!status.isSuccess()) {
                                    if (retryCount > 5) return;
                                    soundThreadHandler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            fireMessage(data, path, retryCount + 1);
                                        }
                                    }, 1000 * retryCount);
                                }
                            }
                            if (path.equals(CommonIdentifiers.P2W_CHANGE_SETTINGS)) {
                                Intent messageIntent = new Intent();
                                messageIntent.setAction(Intent.ACTION_SEND);
                                messageIntent.putExtra("status", status.getStatusCode());
                                LocalBroadcastManager.getInstance(WearMessageListenerService.this).sendBroadcast(messageIntent);
                            }
                        }
                    });
                }
            }
        });
    }
 
Example 15
Source File: MainActivity.java    From wearable with Apache License 2.0 5 votes vote down vote up
public void run() {
    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await();
    for (Node node : nodes.getNodes()) {
        MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes()).await();
        if (result.getStatus().isSuccess()) {
            sendmessage("SendThread: message send to " + node.getDisplayName());
            Log.v(TAG, "SendThread: message send to "+ node.getDisplayName());

        } else {
            // Log an error
            sendmessage("SendThread: message failed to" + node.getDisplayName());
            Log.v(TAG, "SendThread: message failed to" + node.getDisplayName());
        }
    }
}
 
Example 16
Source File: MainActivity.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
private String getRemoteNodeId(GoogleApiClient googleApiClient) {
    NodeApi.GetConnectedNodesResult nodesResult =
            Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    List<Node> nodes = nodesResult.getNodes();
    if (nodes.size() > 0) {
        return nodes.get(0).getId();
    }
    return null;
}
 
Example 17
Source File: Utils.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
public static String getRemoteNodeId(GoogleApiClient googleApiClient) {
    NodeApi.GetConnectedNodesResult nodesResult =
            Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    List<Node> nodes = nodesResult.getNodes();
    for (Node node : nodes) {
        if(node.isNearby()) {
            return node.getId();
        }
    }
    if (nodes.size() > 0) {
        return nodes.get(0).getId();
    }
    return null;
}
 
Example 18
Source File: ConnectionHelper.java    From OkWear with Apache License 2.0 5 votes vote down vote up
public void getNodes(@NonNull final NodeChangeListener listener) {
    final List<Node> nodeList = new ArrayList<>();
    final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
    nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(NodeApi.GetConnectedNodesResult result) {
            for (Node node : result.getNodes()) {
                nodeList.add(node);
                listener.onReceiveNodes(nodeList);
            }
        }
    });
}
 
Example 19
Source File: WearMessageHandlerService.java    From android-wear-gopro-remote with Apache License 2.0 5 votes vote down vote up
private boolean findWearableNode() {
    if(mGoogleApiClient != null && mWearableNode == null) {
        NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
        if (result.getNodes().size() > 0) {
            mWearableNode = result.getNodes().get(0);
            if (BuildConfig.DEBUG) {
                Logger.debug(TAG, "Found wearable: name=" + mWearableNode.getDisplayName() +
                        ", id=" + mWearableNode.getId());
            }
        } else {
            mWearableNode = null;
        }
    }
    return mWearableNode != null;
}
 
Example 20
Source File: GoogleApiMessenger.java    From Sensor-Data-Logger with Apache License 2.0 4 votes vote down vote up
public PendingResult<NodeApi.GetConnectedNodesResult> getConnectedNodes(ResultCallback<NodeApi.GetConnectedNodesResult> callback) {
    PendingResult<NodeApi.GetConnectedNodesResult> pendingResult = Wearable.NodeApi.getConnectedNodes(googleApiClient);
    pendingResult.setResultCallback(callback);
    return pendingResult;
}