Java Code Examples for com.google.android.gms.wearable.PutDataMapRequest#asPutDataRequest()

The following examples show how to use com.google.android.gms.wearable.PutDataMapRequest#asPutDataRequest() . 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: WatchUpdaterService.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
private void sendActionConfirmationRequest(String title, String message, String actionstring) {
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACTION_CONFIRMATION_REQUEST_PATH);
        //unique content
        dataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putString("actionConfirmationRequest", "actionConfirmationRequest");
        dataMapRequest.getDataMap().putString("title", title);
        dataMapRequest.getDataMap().putString("message", message);
        dataMapRequest.getDataMap().putString("actionstring", actionstring);

        log.debug("Requesting confirmation from wear: " + actionstring);

        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        debugData("sendActionConfirmationRequest", putDataRequest);
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e("confirmationRequest", "No connection to wearable available!");
    }
}
 
Example 2
Source File: WatchFaceConfigActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
private void notifyWear(String newBgColor) {
    PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/bg_change");

    dataMapRequest.getDataMap().putString("new_color", newBgColor);

    PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    //check if the message is delivered?
                    //If the status is failed, that means that the currently device is
                    //not connected. The data will get deliver when phone gets connected to the watch.
                    Log.d("Data saving", dataItemResult.getStatus().isSuccess() ? "Success" : "Failed");
                }
            });
}
 
Example 3
Source File: PhoneActivity.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a DataItem that on the wearable will be interpreted as a request to show a
 * notification. The result will be a notification that only shows up on the wearable.
 */
private void buildWearableOnlyNotification(String title, String content, String path) {
    if (mGoogleApiClient.isConnected()) {
        PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path);
        putDataMapRequest.getDataMap().putString(Constants.KEY_CONTENT, content);
        putDataMapRequest.getDataMap().putString(Constants.KEY_TITLE, title);
        PutDataRequest request = putDataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(mGoogleApiClient, request)
                .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                    @Override
                    public void onResult(DataApi.DataItemResult dataItemResult) {
                        if (!dataItemResult.getStatus().isSuccess()) {
                            Log.e(TAG, "buildWatchOnlyNotification(): Failed to set the data, "
                                    + "status: " + dataItemResult.getStatus().getStatusCode());
                        }
                    }
                });
    } else {
        Log.e(TAG, "buildWearableOnlyNotification(): no Google API Client connection");
    }
}
 
Example 4
Source File: UARTConfigurationSynchronizer.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Synchronizes the UART configurations between handheld and wearables.
 * Call this when configuration has been created or altered.
 * @return pending result
 */
public PendingResult<DataApi.DataItemResult> onConfigurationAddedOrEdited(final long id, final UartConfiguration configuration) {
	if (!hasConnectedApi())
		return null;

	final PutDataMapRequest mapRequest = PutDataMapRequest.create(Constants.UART.CONFIGURATIONS + "/" + id);
	final DataMap map = mapRequest.getDataMap();
	map.putString(Constants.UART.Configuration.NAME, configuration.getName());
	final ArrayList<DataMap> commands = new ArrayList<>(UartConfiguration.COMMANDS_COUNT);
	for (Command command : configuration.getCommands()) {
		if (command != null && command.isActive()) {
			final DataMap item = new DataMap();
			item.putInt(Constants.UART.Configuration.Command.ICON_ID, command.getIconIndex());
			item.putString(Constants.UART.Configuration.Command.MESSAGE, command.getCommand());
			item.putInt(Constants.UART.Configuration.Command.EOL, command.getEolIndex());
			commands.add(item);
		}
	}
	map.putDataMapArrayList(Constants.UART.Configuration.COMMANDS, commands);
	final PutDataRequest request = mapRequest.asPutDataRequest();
	return Wearable.DataApi.putDataItem(googleApiClient, request);
}
 
Example 5
Source File: WatchUpdaterService.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
private void sendCancelNotificationRequest(String actionstring) {
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACTION_CANCELNOTIFICATION_REQUEST_PATH);
        //unique content
        dataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putString("cancelNotificationRequest", "cancelNotificationRequest");
        dataMapRequest.getDataMap().putString("actionstring", actionstring);

        log.debug("Canceling notification on wear: " + actionstring);

        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        debugData("sendCancelNotificationRequest", putDataRequest);
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e("cancelNotificationReq", "No connection to wearable available!");
    }
}
 
Example 6
Source File: WearableMainActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
private void addLocationEntry(double latitude, double longitude) {
    if (!mGpsPermissionApproved) {
        return;
    }
    mCalendar.setTimeInMillis(System.currentTimeMillis());
    LocationEntry entry = new LocationEntry(mCalendar, latitude, longitude);
    String path = Constants.PATH + "/" + mCalendar.getTimeInMillis();
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path);
    putDataMapRequest.getDataMap().putDouble(Constants.KEY_LATITUDE, entry.latitude);
    putDataMapRequest.getDataMap().putDouble(Constants.KEY_LONGITUDE, entry.longitude);
    putDataMapRequest.getDataMap()
            .putLong(Constants.KEY_TIME, entry.calendar.getTimeInMillis());
    PutDataRequest request = putDataMapRequest.asPutDataRequest();
    request.setUrgent();

    Task<DataItem> dataItemTask =
            Wearable.getDataClient(getApplicationContext()).putDataItem(request);

    dataItemTask.addOnSuccessListener(dataItem -> {
        Log.d(TAG, "Data successfully sent: " + dataItem.toString());
    });
    dataItemTask.addOnFailureListener(exception -> {
        Log.e(TAG, "AddPoint:onClick(): Failed to set the data, "
                + "exception: " + exception);
    });
}
 
Example 7
Source File: WearableApi.java    From LibreAlarm with GNU General Public License v3.0 5 votes vote down vote up
private static boolean sendData(GoogleApiClient client, PutDataMapRequest putDataMapReq, ResultCallback<DataApi.DataItemResult> listener) {
    if (client.isConnected()) {
        Log.i(TAG, "update settings");
        putDataMapReq.setUrgent();
        PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
        PendingResult<DataApi.DataItemResult> pR =
                Wearable.DataApi.putDataItem(client, putDataReq);
        if (listener != null) pR.setResultCallback(listener);
        return true;
    }
    return false;
}
 
Example 8
Source File: WatchUpdaterService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private void sendRequestExtra(String path, String key, String value) {
    forceGoogleApiConnect();
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);//NEW_STATUS_PATH
        //unique content
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putString(key, value);//"externalStatusString"
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e("sendRequestExtra", "No connection to wearable available!");
    }
}
 
Example 9
Source File: MainActivity.java    From android-Quiz with Apache License 2.0 5 votes vote down vote up
public PutDataRequest toPutDataRequest() {
    PutDataMapRequest request = PutDataMapRequest.create("/question/" + questionIndex);
    DataMap dataMap = request.getDataMap();
    dataMap.putString(QUESTION, question);
    dataMap.putInt(QUESTION_INDEX, questionIndex);
    dataMap.putStringArray(ANSWERS, answers);
    dataMap.putInt(CORRECT_ANSWER_INDEX, correctAnswerIndex);
    PutDataRequest putDataRequest = request.asPutDataRequest();
    putDataRequest.setUrgent();
    return putDataRequest;
}
 
Example 10
Source File: WatchUpdaterService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private void sendRequestExtra(String path, String key, byte[] value) {
    forceGoogleApiConnect();
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putByteArray(key, value);
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        dataMapRequest.setUrgent();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
        Log.d(TAG, "Sending bytes path: " + path + " " + value.length);

    } else {
        Log.e("sendRequestExtra", "No connection to wearable available!");
    }
}
 
Example 11
Source File: WatchUpdaterService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void sendWearToast(String msg, int length) {
    if ((googleApiClient != null) && (googleApiClient.isConnected())) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(WEARABLE_TOAST_NOTIFICATON);
        dataMapRequest.setUrgent();
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putInt("length", length);
        dataMapRequest.getDataMap().putString("msg", msg);
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e(TAG, "No connection to wearable available for toast! " + msg);
    }
}
 
Example 12
Source File: Message.java    From DronesWear with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static PendingResult<DataApi.DataItemResult> sendActionMessage(GoogleApiClient googleApiClient) {
    PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACTION_PATH);
    actionMessageUri = dataMapRequest.getUri();

    // Data Push
    PutDataRequest request = dataMapRequest.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(googleApiClient, request);

    return pendingResult;
}
 
Example 13
Source File: UpdateQuestionService.java    From android-Quiz with Apache License 2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    mGoogleApiClient.blockingConnect(TIME_OUT_MS, TimeUnit.MILLISECONDS);
    Uri dataItemUri = intent.getData();
    if (!mGoogleApiClient.isConnected()) {
        Log.e(TAG, "Failed to update data item " + dataItemUri
                + " because client is disconnected from Google Play Services");
        return;
    }
    DataApi.DataItemResult dataItemResult = Wearable.DataApi.getDataItem(
            mGoogleApiClient, dataItemUri).await();
    PutDataMapRequest putDataMapRequest = PutDataMapRequest
            .createFromDataMapItem(DataMapItem.fromDataItem(dataItemResult.getDataItem()));
    DataMap dataMap = putDataMapRequest.getDataMap();

    // Update quiz status variables, which will be reflected on the phone.
    int questionIndex = intent.getIntExtra(EXTRA_QUESTION_INDEX, -1);
    boolean chosenAnswerCorrect = intent.getBooleanExtra(EXTRA_QUESTION_CORRECT, false);
    dataMap.putInt(QUESTION_INDEX, questionIndex);
    dataMap.putBoolean(CHOSEN_ANSWER_CORRECT, chosenAnswerCorrect);
    dataMap.putBoolean(QUESTION_WAS_ANSWERED, true);
    PutDataRequest request = putDataMapRequest.asPutDataRequest();
    request.setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request).await();

    // Remove this question notification.
    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(questionIndex);
    mGoogleApiClient.disconnect();
}
 
Example 14
Source File: WatchUpdaterService.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
private void sendNotification() {
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(OPEN_SETTINGS_PATH);
        //unique content
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putString("openSettings", "openSettings");
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e("OpenSettings", "No connection to wearable available!");
    }
}
 
Example 15
Source File: WatchUpdaterService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void sendWearToast(String msg, int length) {
    if ((googleApiClient != null) && (googleApiClient.isConnected())) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(WEARABLE_TOAST_NOTIFICATON);
        dataMapRequest.setUrgent();
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putInt("length", length);
        dataMapRequest.getDataMap().putString("msg", msg);
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e(TAG, "No connection to wearable available for toast! " + msg);
    }
}
 
Example 16
Source File: ClimbConfirmation.java    From climb-tracker with Apache License 2.0 5 votes vote down vote up
private void saveClimb() {
    // Create a unique identifier for this data item
    PutDataMapRequest putDataMapReq = PutDataMapRequest.create(Path.CLIMB + '/' + UUID.randomUUID());
    putDataMapReq.getDataMap().putString(Path.ROUTE_GRADE_LABEL_KEY, routeGradeLabelToSave);
    putDataMapReq.getDataMap().putLong(Path.CLIMB_DATE_KEY, (new Date().getTime()));
    PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
}
 
Example 17
Source File: Message.java    From DronesWear with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static PendingResult<DataApi.DataItemResult> sendJoystickMessage(JoystickData joystickData, GoogleApiClient googleApiClient) {
    PutDataMapRequest dataMapRequest = PutDataMapRequest.create(JOYSTICK_PATH);
    joystickMessageUri = dataMapRequest.getUri();
    DataMap dataMap = dataMapRequest.getDataMap();
    //Data set
    dataMap.putFloatArray(VALUE_STR, new float[]{joystickData.getPercentX(), joystickData.getPercentY()});

    // Data Push
    PutDataRequest request = dataMapRequest.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(googleApiClient, request);

    return pendingResult;
}
 
Example 18
Source File: MessageReceiverService.java    From ibm-wearables-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Send the sensors data to the phone
 * @param dataMapRequest data map to send
 * @return send result
 */
@Override
public PendingResult<DataApi.DataItemResult> sendData(PutDataMapRequest dataMapRequest) {
    PutDataRequest dataRequest = dataMapRequest.asPutDataRequest();
    dataRequest.setUrgent();

    return Wearable.DataApi.putDataItem(apiClient, dataRequest);
}
 
Example 19
Source File: WearListenerService.java    From wear-notify-for-reddit with Apache License 2.0 5 votes vote down vote up
private void sendComments(final List<Comment> comments) {
    PutDataMapRequest mapRequest = PutDataMapRequest.create(Constants.PATH_COMMENTS);
    mapRequest.getDataMap().putString(Constants.KEY_REDDIT_POSTS, mGson.toJson(comments));
    mapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis());

    PutDataRequest request = mapRequest.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request)
            .setResultCallback(dataItemResult -> Timber.d("Sent " + comments.size() + " comments onResult: " + dataItemResult
                    .getStatus()));
}
 
Example 20
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
/**
 * Transfer the required data over to the wearable
 * @param attractions list of attraction data to transfer over
 */
private void sendDataToWearable(List<Attraction> attractions) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    // It's OK to use blockingConnect() here as we are running in an
    // IntentService that executes work on a separate (background) thread.
    ConnectionResult connectionResult = googleApiClient.blockingConnect(
            Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

    // Limit attractions to send
    int count = attractions.size() > Constants.MAX_ATTRACTIONS ?
            Constants.MAX_ATTRACTIONS : attractions.size();

    ArrayList<DataMap> attractionsData = new ArrayList<>(count);

    for (int i = 0; i < count; i++) {
        Attraction attraction = attractions.get(i);

        Bitmap image = null;
        Bitmap secondaryImage = null;

        try {
            // Fetch and resize attraction image bitmap
            image = Glide.with(this)
                    .load(attraction.imageUrl)
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(Constants.WEAR_IMAGE_SIZE_PARALLAX_WIDTH, Constants.WEAR_IMAGE_SIZE)
                    .get();

            secondaryImage = Glide.with(this)
                    .load(attraction.secondaryImageUrl)
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(Constants.WEAR_IMAGE_SIZE_PARALLAX_WIDTH, Constants.WEAR_IMAGE_SIZE)
                    .get();
        } catch (InterruptedException | ExecutionException e) {
            Log.e(TAG, "Exception loading bitmap from network");
        }

        if (image != null && secondaryImage != null) {

            DataMap attractionData = new DataMap();

            String distance = Utils.formatDistanceBetween(
                    Utils.getLocation(this), attraction.location);

            attractionData.putString(Constants.EXTRA_TITLE, attraction.name);
            attractionData.putString(Constants.EXTRA_DESCRIPTION, attraction.description);
            attractionData.putDouble(
                    Constants.EXTRA_LOCATION_LAT, attraction.location.latitude);
            attractionData.putDouble(
                    Constants.EXTRA_LOCATION_LNG, attraction.location.longitude);
            attractionData.putString(Constants.EXTRA_DISTANCE, distance);
            attractionData.putString(Constants.EXTRA_CITY, attraction.city);
            attractionData.putAsset(Constants.EXTRA_IMAGE,
                    Utils.createAssetFromBitmap(image));
            attractionData.putAsset(Constants.EXTRA_IMAGE_SECONDARY,
                    Utils.createAssetFromBitmap(secondaryImage));

            attractionsData.add(attractionData);
        }
    }

    if (connectionResult.isSuccess() && googleApiClient.isConnected()
            && attractionsData.size() > 0) {

        PutDataMapRequest dataMap = PutDataMapRequest.create(Constants.ATTRACTION_PATH);
        dataMap.getDataMap().putDataMapArrayList(Constants.EXTRA_ATTRACTIONS, attractionsData);
        dataMap.getDataMap().putLong(Constants.EXTRA_TIMESTAMP, new Date().getTime());
        PutDataRequest request = dataMap.asPutDataRequest();

        // Send the data over
        DataApi.DataItemResult result =
                Wearable.DataApi.putDataItem(googleApiClient, request).await();

        if (!result.getStatus().isSuccess()) {
            Log.e(TAG, String.format("Error sending data using DataApi (error code = %d)",
                    result.getStatus().getStatusCode()));
        }

    } else {
        Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG,
                connectionResult.getErrorCode()));
    }
    googleApiClient.disconnect();
}