Java Code Examples for com.google.android.gms.wearable.MessageApi#SendMessageResult

The following examples show how to use com.google.android.gms.wearable.MessageApi#SendMessageResult . 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: 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 2
Source File: ConnectionHelper.java    From OkWear with Apache License 2.0 6 votes vote down vote up
/**
 * send message
 * you don't have to send anything
 *
 * @param node     送信先node
 * @param payload
 * @param path
 * @param listener
 */
public void sendMessage(@NonNull final Node node, @Nullable final byte[] payload,
                        @NonNull final String path, @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) {
    final PendingResult<MessageApi.SendMessageResult> messageResult =
            Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, payload);
    messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
        @Override
        public void onResult(final MessageApi.SendMessageResult sendMessageResult) {
            Log.d(TAG, "Status: " + sendMessageResult.getStatus());
            if (listener != null) {
                listener.onResult(sendMessageResult);
            }
        }
    });

}
 
Example 3
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 4
Source File: OkWear.java    From OkWear with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessageAll(@Nullable final byte[] payload, @Nullable String path,
                           @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) {
    if (path == null) {
        path = DEFAULT_MESSAGE_API_PATH;
    }
    final String finalPath = path;
    mHelper.getNodes(new NodeChangeListener() {
        @Override
        public void onReceiveNodes(List<Node> nodes) {
            for (Node node : nodes) {
                sendMessage(node, payload, finalPath, listener);
            }
        }
    });
}
 
Example 5
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 6
Source File: SendMessageTask.java    From OkWear with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(final MessageApi.SendMessageResult result) {
    if (listener != null) {
        listener.onResult(result);
    }
    return;
}
 
Example 7
Source File: SendMessageTask.java    From OkWear with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageApi.SendMessageResult doInBackground(Object... args) {

    final MessageApi.SendMessageResult messageResult =
            Wearable.MessageApi.sendMessage(googleApiClient, id, path, payload).await();

    return messageResult;
}
 
Example 8
Source File: SendMessageTask.java    From OkWear with Apache License 2.0 5 votes vote down vote up
public SendMessageTask(@NonNull final GoogleApiClient googleApiClient, @NonNull final Node node,
                       @Nullable final byte[] payload, @NonNull final String path,
                       @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) {

    this.googleApiClient = googleApiClient;
    this.id = node.getId();
    this.payload = payload;
    this.path = path;
    this.listener = listener;
}
 
Example 9
Source File: OkWear.java    From OkWear with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessage(@NonNull final Node node, @Nullable final byte[] payload,
                        @Nullable String path, @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) {
    if (path == null) {
        path = DEFAULT_MESSAGE_API_PATH;
    }
    mHelper.sendMessage(node, payload, path, listener);
}
 
Example 10
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 11
Source File: WatchMainActivity.java    From android-wear-gopro-remote with Apache License 2.0 5 votes vote down vote up
private boolean sendToPhone(String path, byte[] data,
                         final ResultCallback<MessageApi.SendMessageResult> callback,
                         boolean showSpinner) {

    if (mPhoneNode != null) {
        long diff = System.currentTimeMillis() - mLastMessageToPhone;
        if(diff <= 1000) {
            if(Logger.DEBUG) Logger.debug(TAG, "Last message to phone was %d millis ago", diff);
            return false;
        }
        if(showSpinner) showSpinner();
        if(Logger.DEBUG) Logger.debug(TAG, "Message sent %s", path);
        if (mShakeDetectActivity != null) mShakeDetectActivity.stopDetecting();
        PendingResult<MessageApi.SendMessageResult> pending = Wearable.MessageApi.sendMessage(mGoogleApiClient, mPhoneNode.getId(), path, data);
        pending.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
            @Override
            public void onResult(MessageApi.SendMessageResult result) {
                if (callback != null) {
                    callback.onResult(result);
                }
                if (result.getStatus().isSuccess()) {
                    mLastMessageToPhone = System.currentTimeMillis();
                } else {
                    setFailStatusMessage(R.string.label_watch_disconnected, R.drawable.ic_retry);
                    Logger.warning(TAG, "Failed to send Message: " + result.getStatus());
                }
            }
        });

        return true;
    } else {
        Logger.error(TAG, String.format("Tried to send message (%s) before device was found.", path));
        return false;
    }
}
 
Example 12
Source File: MainActivity.java    From AndroidWearable-Samples with Apache License 2.0 5 votes vote down vote up
private ResultCallback<MessageApi.SendMessageResult> getSendMessageResultCallback() {
    return new ResultCallback<MessageApi.SendMessageResult>() {
        @Override
        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
            if (!sendMessageResult.getStatus().isSuccess()) {
                Log.e(TAG, "Failed to connect to Google Api Client with status "
                        + sendMessageResult.getStatus());
            }
        }
    };
}
 
Example 13
Source File: MainActivity.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
@Override
protected Void doInBackground(Void... params) {

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(mContext)
            .addApi(Wearable.API)
            .build();

    ConnectionResult connectionResult = googleApiClient.blockingConnect(
            Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

    if (!connectionResult.isSuccess() || !googleApiClient.isConnected()) {
        Log.e("ETSMobile-Wear", connectionResult.getErrorMessage());
        return null;
    }

    MessageApi.SendMessageResult result =
            Wearable.MessageApi.sendMessage(
                    googleApiClient,
                    Utils.getRemoteNodeId(googleApiClient),
                    "/today_req",
                    null)
                    .await();

    if (result.getStatus().isSuccess()) {
        Log.d("wearThread", "SUCCESS : Message sent");
    } else {
        Log.d("wearThread", "ERROR: failed to send Message");
    }

    return null;

}
 
Example 14
Source File: GoogleApiMessenger.java    From Sensor-Data-Logger with Apache License 2.0 5 votes vote down vote up
public MessageApi.SendMessageResult sendMessageToNodeWithResult(final String path, final byte[] data, String nodeId) throws Exception {
    if (!googleApiClient.isConnected()) {
        throw new Exception("Google API client is not connected");
    }
    if (nodeId == null) {
        throw new Exception("Node Id is not set");
    }
    return Wearable.MessageApi.sendMessage(googleApiClient, nodeId, path, data).await();
}
 
Example 15
Source File: ListenerService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private void sendMessagePayload(Node node, String pathdesc, final String path, byte[] payload) {
    Log.d(TAG, "Benchmark: doInBackground sendMessagePayload " + pathdesc + "=" + path + " nodeID=" + node.getId() + " nodeName=" + node.getDisplayName() + ((payload != null) ? (" payload.length=" + payload.length) : ""));

    //ORIGINAL ASYNC METHOD
    PendingResult<MessageApi.SendMessageResult> result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), path, payload);
    result.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
        @Override
        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
            if (!sendMessageResult.getStatus().isSuccess()) {
                Log.e(TAG, "sendMessagePayload ERROR: failed to send request " + path + " Status=" + sendMessageResult.getStatus().getStatusMessage());
            } else {
                Log.d(TAG, "sendMessagePayload Sent request " + node.getDisplayName() + " " + path + " Status=: " + sendMessageResult.getStatus().getStatusMessage());
            }
        }
    });

    //TEST**************************************************************************
    DataMap datamap;
    if (bBenchmarkBgs && path.equals(SYNC_BGS_PATH)) {
        //bBenchmarkBgs = runBenchmarkTest(node, pathdesc+"_BM", path+"_BM", payload, bBenchmarkDup);
        datamap = getWearTransmitterData(1000, 0, 0);//generate 1000 records of test data
        if (datamap != null) {
            bBenchmarkBgs = runBenchmarkTest(node, pathdesc + "_BM", path + "_BM", datamap.toByteArray(), false);
        }
    } else if (bBenchmarkLogs && path.equals(SYNC_LOGS_PATH)) {
        //bBenchmarkLogs = runBenchmarkTest(node, pathdesc+"_BM", path+"_BM", payload, bBenchmarkDup);
        datamap = getWearLogData(1000, 0, 0, -1);//generate 1000 records of test data
        if (datamap != null) {
            bBenchmarkLogs = runBenchmarkTest(node, pathdesc + "_BM", path + "_BM", datamap.toByteArray(), false);
        }
    }
    //Random Test
    if (bBenchmarkRandom) {
        final byte[] randomBytes = new byte[200000];
        ThreadLocalRandom.current().nextBytes(randomBytes);
        bBenchmarkRandom = runBenchmarkTest(node, pathdesc + "_BM_RAND", path + "_BM_RAND", randomBytes, false);
        Log.i(TAG, "Benchmark: DONE!");
    }
    //******************************************************************************
}
 
Example 16
Source File: WearApplication.java    From android-wear-gopro-remote with Apache License 2.0 4 votes vote down vote up
public void sendWearMessage(String path, byte[] data,
final ResultCallback<MessageApi.SendMessageResult> callback);
 
Example 17
Source File: WatchMainActivity.java    From android-wear-gopro-remote with Apache License 2.0 4 votes vote down vote up
@Override
public void sendWearMessage(String path, byte[] data, ResultCallback<MessageApi.SendMessageResult> callback) {
    sendToPhone(path, data, callback, false);
}
 
Example 18
Source File: WearableApi.java    From LibreAlarm with GNU General Public License v3.0 4 votes vote down vote up
public static void sendMessage(final GoogleApiClient client, final String command, final String message, ResultCallback<MessageApi.SendMessageResult> listener) {
    sendMessage(client, command, message.getBytes(Charset.forName("UTF-8")), listener);
}
 
Example 19
Source File: MessageApiImpl.java    From OkWear with Apache License 2.0 2 votes vote down vote up
/**
 * send message to specified device using AsyncTask
 * if you want to call from UI thread
 *
 * @param node
 * @param payload
 * @param path     if you don't distinguish messages, you can use default message api path
 * @param listener
 */
public void sendMessageAsync(@NonNull final Node node, @Nullable final byte[] payload,
                             @Nullable String path, @Nullable final SendResultListener<MessageApi.SendMessageResult> listener);
 
Example 20
Source File: MessageApiImpl.java    From OkWear with Apache License 2.0 2 votes vote down vote up
/**
 * send message to all connected devices
 *
 * @param payload
 * @param path     if you don't distinguish messages, you can use default message api path
 * @param listener
 */
public void sendMessageAll(@Nullable final byte[] payload, @Nullable String path,
                           @Nullable final SendResultListener<MessageApi.SendMessageResult> listener);