com.google.android.gms.wearable.PutDataRequest Java Examples

The following examples show how to use com.google.android.gms.wearable.PutDataRequest. 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: SendToDataLayerThread.java    From xDrip-Experimental with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: DeleteQuestionService.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
@Override
public void onHandleIntent(Intent intent) {
    mGoogleApiClient.blockingConnect(CONNECT_TIMEOUT_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();
    dataMap.putBoolean(QUESTION_WAS_DELETED, true);
    PutDataRequest request = putDataMapRequest.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request).await();
    mGoogleApiClient.disconnect();
}
 
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: WatchUpdaterService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private void sendDataReceived(String path, String notification, long timeOfLastEntry, String type, long watch_syncLogsRequested) {//KS
    Log.d(TAG, "sendDataReceived timeOfLastEntry=" + JoH.dateTimeText(timeOfLastEntry) + " Path=" + path);
    forceGoogleApiConnect();
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);
        dataMapRequest.setUrgent();
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putLong("timeOfLastEntry", timeOfLastEntry);
        dataMapRequest.getDataMap().putLong("syncLogsRequested", watch_syncLogsRequested);
        dataMapRequest.getDataMap().putString("type", type);
        dataMapRequest.getDataMap().putString("msg", notification);
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e(TAG, "sendDataReceived No connection to wearable available!");
    }
}
 
Example #5
Source File: WatchUpdaterService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static void sendTreatment(double carbs, double insulin, double bloodtest, String injectionJSON, double timeoffset, String timestring) {
    if ((googleApiClient != null) && (googleApiClient.isConnected())) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(WEARABLE_TREATMENT_PAYLOAD);
        //unique content
        dataMapRequest.setUrgent();
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putDouble("carbs", carbs);
        dataMapRequest.getDataMap().putDouble("insulin", insulin);
        dataMapRequest.getDataMap().putDouble("bloodtest", bloodtest);
        dataMapRequest.getDataMap().putDouble("timeoffset", timeoffset);
        dataMapRequest.getDataMap().putString("timestring", timestring);
        dataMapRequest.getDataMap().putString("injectionJSON", injectionJSON);
        dataMapRequest.getDataMap().putBoolean("ismgdl", doMgdl(PreferenceManager.getDefaultSharedPreferences(xdrip.getAppContext())));
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e(TAG, "No connection to wearable available for send treatment!");
    }
}
 
Example #6
Source File: MainActivity.java    From wearable with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the data, note this is a broadcast, so we will get the message as well.
 */
private void sendData(String message) {
    PutDataMapRequest dataMap = PutDataMapRequest.create(datapath);
    dataMap.getDataMap().putString("message", message);
    PutDataRequest request = dataMap.asPutDataRequest();
    request.setUrgent();

    Task<DataItem> dataItemTask = Wearable.getDataClient(this).putDataItem(request);
    dataItemTask
        .addOnSuccessListener(new OnSuccessListener<DataItem>() {
            @Override
            public void onSuccess(DataItem dataItem) {
                Log.d(TAG, "Sending message was successful: " + dataItem);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Sending message failed: " + e);
            }
        })
    ;
}
 
Example #7
Source File: MainActivity.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(COUNT_PATH);
    putDataMapRequest.getDataMap().putInt(COUNT_KEY, count++);
    PutDataRequest request = putDataMapRequest.asPutDataRequest();

    LOGD(TAG, "Generating DataItem: " + request);
    if (!mGoogleApiClient.isConnected()) {
        return;
    }
    Wearable.DataApi.putDataItem(mGoogleApiClient, request)
            .setResultCallback(new ResultCallback<DataItemResult>() {
                @Override
                public void onResult(DataItemResult dataItemResult) {
                    if (!dataItemResult.getStatus().isSuccess()) {
                        Log.e(TAG, "ERROR: failed to putDataItem, status code: "
                                + dataItemResult.getStatus().getStatusCode());
                    }
                }
            });
}
 
Example #8
Source File: WatchUpdaterService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private void sendDataReceived(String path, String notification, long timeOfLastEntry, String type, long watch_syncLogsRequested) {//KS
    Log.d(TAG, "sendDataReceived timeOfLastEntry=" + JoH.dateTimeText(timeOfLastEntry) + " Path=" + path);
    forceGoogleApiConnect();
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);
        dataMapRequest.setUrgent();
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putLong("timeOfLastEntry", timeOfLastEntry);
        dataMapRequest.getDataMap().putLong("syncLogsRequested", watch_syncLogsRequested);
        dataMapRequest.getDataMap().putString("type", type);
        dataMapRequest.getDataMap().putString("msg", notification);
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e(TAG, "sendDataReceived No connection to wearable available!");
    }
}
 
Example #9
Source File: MainActivity.java    From WearOngoingNotificationSample with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void sendMessage() {
    if (mGoogleApiClient.isConnected()) {
        PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(Constants.PATH_NOTIFICATION);

        // Add data to the request
        putDataMapRequest.getDataMap().putString(Constants.KEY_TITLE, String.format("hello world! %d", count++));

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Asset asset = createAssetFromBitmap(icon);
        putDataMapRequest.getDataMap().putAsset(Constants.KEY_IMAGE, asset);

        PutDataRequest request = putDataMapRequest.asPutDataRequest();

        Wearable.DataApi.putDataItem(mGoogleApiClient, request)
                .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                    @Override
                    public void onResult(DataApi.DataItemResult dataItemResult) {
                        Log.d(TAG, "putDataItem status: " + dataItemResult.getStatus().toString());
                    }
                });
    }
}
 
Example #10
Source File: TrackActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Send the current status of the step count tracking weather it is running or not. This message
 * is important and we should have guarantee of delivery to maintain the state of tracking status
 * on the phone. That is why we are using DataMap to communicate. So, if the phone is not connected
 * the message won't get lost. As soon as the phone connects, this status message will pass to the
 * phone application.
 *
 * @param isTracking true if the tracking is running
 */
private void sendTrackingStatusDataMap(boolean isTracking) {
    PutDataMapRequest dataMapRequest = PutDataMapRequest.create(STEP_TRACKING_STATUS_PATH);

    dataMapRequest.getDataMap().putBoolean("status", isTracking);
    dataMapRequest.getDataMap().putLong("status-time", System.currentTimeMillis());

    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 #11
Source File: MainActivity.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the asset that was created form the photo we took by adding it to the Data Item store.
 */
private void sendPhoto(Asset asset) {
    PutDataMapRequest dataMap = PutDataMapRequest.create(IMAGE_PATH);
    dataMap.getDataMap().putAsset(IMAGE_KEY, asset);
    dataMap.getDataMap().putLong("time", new Date().getTime());
    PutDataRequest request = dataMap.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request)
            .setResultCallback(new ResultCallback<DataItemResult>() {
                @Override
                public void onResult(DataItemResult dataItemResult) {
                    LOGD(TAG, "Sending image was successful: " + dataItemResult.getStatus()
                            .isSuccess());
                }
            });

}
 
Example #12
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 #13
Source File: MainActivity.java    From android-Quiz with Apache License 2.0 6 votes vote down vote up
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
    if (dataItemResult.getStatus().isSuccess()) {
        PutDataMapRequest request = PutDataMapRequest.createFromDataMapItem(
                DataMapItem.fromDataItem(dataItemResult.getDataItem()));
        DataMap dataMap = request.getDataMap();
        dataMap.putBoolean(QUESTION_WAS_ANSWERED, false);
        dataMap.putBoolean(QUESTION_WAS_DELETED, false);
        if (!mHasQuestionBeenAsked && dataMap.getInt(QUESTION_INDEX) == 0) {
            // Ask the first question now.
            PutDataRequest putDataRequest = request.asPutDataRequest();
            // Set to high priority in case it isn't already.
            putDataRequest.setUrgent();
            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
            setHasQuestionBeenAsked(true);
        } else {
            // Enqueue future questions.
            mFutureQuestions.add(new Question(dataMap.getString(QUESTION),
                    dataMap.getInt(QUESTION_INDEX), dataMap.getStringArray(ANSWERS),
                    dataMap.getInt(CORRECT_ANSWER_INDEX)));
        }
    } else {
        Log.e(TAG, "Failed to reset data item " + dataItemResult.getDataItem().getUri());
    }
}
 
Example #14
Source File: WatchUpdaterService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static void sendTreatment(double carbs, double insulin, double bloodtest, String injectionJSON, double timeoffset, String timestring) {
    if ((googleApiClient != null) && (googleApiClient.isConnected())) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(WEARABLE_TREATMENT_PAYLOAD);
        //unique content
        dataMapRequest.setUrgent();
        dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putDouble("carbs", carbs);
        dataMapRequest.getDataMap().putDouble("insulin", insulin);
        dataMapRequest.getDataMap().putDouble("bloodtest", bloodtest);
        dataMapRequest.getDataMap().putDouble("timeoffset", timeoffset);
        dataMapRequest.getDataMap().putString("timestring", timestring);
        dataMapRequest.getDataMap().putString("injectionJSON", injectionJSON);
        dataMapRequest.getDataMap().putBoolean("ismgdl", doMgdl(PreferenceManager.getDefaultSharedPreferences(xdrip.getAppContext())));
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e(TAG, "No connection to wearable available for send treatment!");
    }
}
 
Example #15
Source File: DeleteQuestionService.java    From android-Quiz with Apache License 2.0 6 votes vote down vote up
@Override
public void onHandleIntent(Intent intent) {
    mGoogleApiClient.blockingConnect(CONNECT_TIMEOUT_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();
    dataMap.putBoolean(QUESTION_WAS_DELETED, true);
    PutDataRequest request = putDataMapRequest.asPutDataRequest();
    request.setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request).await();
    mGoogleApiClient.disconnect();
}
 
Example #16
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 #17
Source File: RemoteSensorManager.java    From SensorDashboard with Apache License 2.0 6 votes vote down vote up
private void filterBySensorIdInBackground(final int sensorId) {
    Log.d(TAG, "filterBySensorId(" + sensorId + ")");

    if (validateConnection()) {
        PutDataMapRequest dataMap = PutDataMapRequest.create("/filter");

        dataMap.getDataMap().putInt(DataMapKeys.FILTER, sensorId);
        dataMap.getDataMap().putLong(DataMapKeys.TIMESTAMP, System.currentTimeMillis());

        PutDataRequest putDataRequest = dataMap.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
            @Override
            public void onResult(DataApi.DataItemResult dataItemResult) {
                Log.d(TAG, "Filter by sensor " + sensorId + ": " + dataItemResult.getStatus().isSuccess());
            }
        });
    }
}
 
Example #18
Source File: DeviceClient.java    From SensorDashboard with Apache License 2.0 6 votes vote down vote up
private void sendSensorDataInBackground(int sensorType, int accuracy, long timestamp, float[] values) {
    if (sensorType == filterId) {
        Log.i(TAG, "Sensor " + sensorType + " = " + Arrays.toString(values));
    } else {
        Log.d(TAG, "Sensor " + sensorType + " = " + Arrays.toString(values));
    }

    PutDataMapRequest dataMap = PutDataMapRequest.create("/sensors/" + sensorType);

    dataMap.getDataMap().putInt(DataMapKeys.ACCURACY, accuracy);
    dataMap.getDataMap().putLong(DataMapKeys.TIMESTAMP, timestamp);
    dataMap.getDataMap().putFloatArray(DataMapKeys.VALUES, values);

    PutDataRequest putDataRequest = dataMap.asPutDataRequest();
    send(putDataRequest);
}
 
Example #19
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 #20
Source File: WatchUpdaterService.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
private void sendChangeConfirmationRequest(String title, String message, String actionstring) {
    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACTION_CHANGECONFIRMATION_REQUEST_PATH);
        //unique content
        dataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putString("changeConfirmationRequest", "changeConfirmationRequest");
        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("sendChangeConfirmationRequest", putDataRequest);
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e("changeConfirmRequest", "No connection to wearable available!");
    }
}
 
Example #21
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 #22
Source File: SynchronizedNotificationsFragment.java    From android-SynchronizedNotifications 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();
        request.setUrgent();
        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 #23
Source File: WatchUpdaterService.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
private void sendPreferences() {
    if (googleApiClient.isConnected()) {

        boolean wearcontrol = SP.getBoolean("wearcontrol", false);

        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(NEW_PREFERENCES_PATH);
        //unique content
        dataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis());
        dataMapRequest.getDataMap().putBoolean("wearcontrol", wearcontrol);
        PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
        debugData("sendPreferences", putDataRequest);
        Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
    } else {
        Log.e("SendStatus", "No connection to wearable available!");
    }
}
 
Example #24
Source File: WearableUtil.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
public static void sendApkToWatch(Context context, File apkFile, final ResultCallback callback) {
	Uri apkUri = FileProvider.getUriForFile(context, "com.calsignlabs.apde.fileprovider", apkFile);
	Asset asset = Asset.createFromUri(apkUri);
	PutDataMapRequest dataMap = PutDataMapRequest.create("/apk");
	dataMap.getDataMap().putAsset("apk", asset);
	dataMap.getDataMap().putLong("timestamp", System.currentTimeMillis());
	PutDataRequest request = dataMap.asPutDataRequest();
	request.setUrgent();
	
	Task<DataItem> putTask = Wearable.getDataClient(context).putDataItem(request);
	putTask.addOnCompleteListener(new OnCompleteListener<DataItem>() {
		@Override
		public void onComplete(@NonNull Task<DataItem> task) {
			if (task.isSuccessful()) {
				callback.success();
			} else {
				callback.failure();
			}
		}
	});
}
 
Example #25
Source File: DeviceClient.java    From wearabird with MIT License 6 votes vote down vote up
public void sendSensorData(final int sensorType, final int accuracy, final long timestamp, final float[] values) {
	ConnectionManager.getInstance(context).sendMessage(new ConnectionManager.ConnectionManagerRunnable(context) {
		@Override
		public void send(GoogleApiClient googleApiClient) {
			PutDataMapRequest dataMap = PutDataMapRequest.create("/sensors/" + sensorType);

			dataMap.getDataMap().putInt(DataMapKeys.ACCURACY, accuracy);
			dataMap.getDataMap().putLong(DataMapKeys.TIMESTAMP, timestamp);
			dataMap.getDataMap().putFloatArray(DataMapKeys.VALUES, values);

			PutDataRequest putDataRequest = dataMap.asPutDataRequest();

			Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
		}
	});
}
 
Example #26
Source File: WatchServices.java    From WearPay with GNU General Public License v2.0 6 votes vote down vote up
@DebugLog
private void sendToWatch(String code) {
    Bitmap bitmapQR = EncodingHandlerUtils.createQRCode(code, 600);
    Bitmap bitmapBar = EncodingHandlerUtils.createBarcode(code, 600, 200);
    PutDataMapRequest dataMap = PutDataMapRequest.create(Common.PATH_QR_CODE);
    dataMap.getDataMap().putAsset(Common.KEY_QR_CODE, toAsset(bitmapQR));
    dataMap.getDataMap().putAsset(Common.KEY_BAR_CODE, toAsset(bitmapBar));
    PutDataRequest request = dataMap.asPutDataRequest();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                @DebugLog
                public void onResult(DataApi.DataItemResult dataItemResult) {
                    if (dataItemResult.getStatus().isSuccess()) {
                        System.out.println("发送成功");
                    } else {
                        System.out.println("发送失败");
                    }
                }
            });

    //sendMessageToAllNodes(Common.PATH_CODE, code.getBytes());
}
 
Example #27
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 #28
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the asset that was created from the photo we took by adding it to the Data Item store.
 */
private void sendPhoto(Asset asset) {
    PutDataMapRequest dataMap = PutDataMapRequest.create(IMAGE_PATH);
    dataMap.getDataMap().putAsset(IMAGE_KEY, asset);
    dataMap.getDataMap().putLong("time", new Date().getTime());
    PutDataRequest request = dataMap.asPutDataRequest();
    request.setUrgent();

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

    dataItemTask.addOnSuccessListener(
            new OnSuccessListener<DataItem>() {
                @Override
                public void onSuccess(DataItem dataItem) {
                    LOGD(TAG, "Sending image was successful: " + dataItem);
                }
            });
}
 
Example #29
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 #30
Source File: WearService.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
public void run() {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
            .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;
    }

    PutDataMapRequest dataMapReq = PutDataMapRequest.create(path);

    ArrayList<DataMap> dataMapArrayList = new ArrayList<>();
    for (Seances seance : seances) {
        dataMapArrayList.add(seance.putData());
    }
    dataMapReq.getDataMap().putDataMapArrayList("list_seances", dataMapArrayList);

    PutDataRequest request = dataMapReq.asPutDataRequest();

    DataApi.DataItemResult dataItemResult = Wearable
            .DataApi
            .putDataItem(googleApiClient, request)
            .await();

    if (dataItemResult.getStatus().isSuccess()) {
        if (BuildConfig.DEBUG) Log.d("SendToDataLayerThread", "Data sent successfully!!");
    } else {
        // Log an error
        if (BuildConfig.DEBUG)
            Log.d("SendToDataLayerThread", "ERROR: failed to send Message");
    }
}