/** * 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 (mGoogleApiClient == null || !mGoogleApiClient.isConnected()) 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(mGoogleApiClient, request); }
/** * Overwrites the current config {@link DataItem}'s {@link DataMap} with {@code newConfig}. * If the config DataItem doesn't exist, it's created. */ public static void putConfigDataItem(GoogleApiClient googleApiClient, DataMap newConfig) { PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(PATH_WITH_FEATURE); DataMap configToPut = putDataMapRequest.getDataMap(); configToPut.putAll(newConfig); PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest(); putDataRequest.setUrgent(); Wearable.DataApi.putDataItem(googleApiClient, putDataRequest) .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "putDataItem result status: " + dataItemResult.getStatus()); } } }); }
/** * Extracts byte array data from an * {@link com.google.android.gms.wearable.Asset}, in a blocking way, hence should not be called * on the UI thread. This may return {@code null}. */ public byte[] loadAssetSynchronous(Asset asset) throws IOException { assertApiConnectivity(); WearUtil.assertNonUiThread(); if (asset == null) { throw new IllegalArgumentException("Asset must be non-null"); } InputStream assetInputStream = Wearable.DataApi.getFdForAsset( mGoogleApiClient, asset).await().getInputStream(); if (assetInputStream == null) { Log.w(TAG, "Requested an unknown Asset."); return null; } ByteArrayOutputStream output = new ByteArrayOutputStream(); int n = 0; byte[] buffer = new byte[4096]; while (-1 != (n = assetInputStream.read(buffer))) { output.write(buffer, 0, n); } return output.toByteArray(); }
private void updateWearWeather(int weather_id, double high_temp, double low_temp){ PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(WEATHER_PATH).setUrgent(); putDataMapRequest.getDataMap().putInt(WEATHER_ID, weather_id); Log.d(LOG_TAG, "value of weather put : "+weather_id); putDataMapRequest.getDataMap().putDouble(HIGH_TEMP, high_temp); putDataMapRequest.getDataMap().putDouble(LOW_TEMP, low_temp); PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest().setUrgent(); PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mWearClient, putDataRequest); pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(@NonNull DataApi.DataItemResult dataItemResult) { if (dataItemResult.getStatus().isSuccess()) { Log.d(LOG_TAG, "Data item set: " + dataItemResult.getDataItem().getUri()); } else { Log.d(LOG_TAG, "Error in sending data to watch"); } } }); }
/** * This method read the UART configurations from the DataApi and populates the adapter with them. */ private void populateConfigurations() { if (mGoogleApiClient.isConnected()) { final PendingResult<DataItemBuffer> results = Wearable.DataApi.getDataItems(mGoogleApiClient, Uri.parse("wear:" + Constants.UART.CONFIGURATIONS), DataApi.FILTER_PREFIX); results.setResultCallback(new ResultCallback<DataItemBuffer>() { @Override public void onResult(final DataItemBuffer dataItems) { final List<UartConfiguration> configurations = new ArrayList<>(dataItems.getCount()); for (int i = 0; i < dataItems.getCount(); ++i) { final DataItem item = dataItems.get(i); final long id = ContentUris.parseId(item.getUri()); final DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap(); final UartConfiguration configuration = new UartConfiguration(dataMap, id); configurations.add(configuration); } mAdapter.setConfigurations(configurations); dataItems.release(); } }); } }
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)); } } ); }
public static void putConfigDataItem(GoogleApiClient googleApiClient, DataMap newConfig, String path) { PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path); DataMap configToPut = putDataMapRequest.getDataMap(); configToPut.putAll(newConfig); Wearable.DataApi.putDataItem(googleApiClient, putDataMapRequest.asPutDataRequest()) .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "putDataItem result status: " + dataItemResult.getStatus()); } } }); }
@Override public void onConnected(Bundle bundle) { Wearable.DataApi.addListener(mGoogleApiClient, this); // get existing data PendingResult<DataItemBuffer> results = Wearable.DataApi.getDataItems(mGoogleApiClient); results.setResultCallback(new ResultCallback<DataItemBuffer>() { @Override public void onResult(@NonNull DataItemBuffer dataItems) { for (DataItem dataItem : dataItems) { switch (Message.getMessageType(dataItem)) { case ACTION_TYPE: int productAction = Message.decodeActionTypeMessage(dataItem); onActionTypeChanged(productAction); break; case INTERACTION_TYPE: int interactionType = Message.decodeInteractionTypeMessage(dataItem); onInteractionTypeChanged(interactionType); break; } } dataItems.release(); } }); }
private Bitmap loadBitmapFromAsset(GoogleApiClient apiClient, Asset asset) { if (asset == null) { throw new IllegalArgumentException("Asset must be non-null"); } /*InputStream assetInputStream = Wearable.DataApi.getFdForAsset( apiClient, asset).await().getInputStream(); */ final InputStream[] assetInputStream = new InputStream[1]; Wearable.DataApi.getFdForAsset(apiClient, asset).setResultCallback(new ResultCallback<DataApi.GetFdForAssetResult>() { @Override public void onResult(DataApi.GetFdForAssetResult getFdForAssetResult) { assetInputStream[0] = getFdForAssetResult.getInputStream(); } }); if (assetInputStream[0] == null) { Log.w(TAG, "Requested an unknown Asset."); return null; } return BitmapFactory.decodeStream(assetInputStream[0]); }
/** * 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)); } } ); }
/** * Load bit map from asset provided * @param asset */ public void loadBitmapFromAsset(Asset asset) { if (asset == null) { Log.e("WATCH", "Asset received on watch face is null"); return; } // convert asset into a file descriptor and get notified when it's ready Wearable.DataApi.getFdForAsset( mGoogleApiClient, asset).setResultCallback(new ResultCallback<DataApi.GetFdForAssetResult>() { @Override public void onResult(DataApi.GetFdForAssetResult getFdForAssetResult) { InputStream assetInputStream = getFdForAssetResult.getInputStream(); if (assetInputStream == null) { Log.w("WATCH", "Requested an unknown Asset."); return; } mWeatherIconBitmap = BitmapFactory.decodeStream(assetInputStream); } }); }
/** * Deletes a data items asynchronously. Caller can specify a {@link ResultCallback} or * pass a {@code null}; if a {@code null} is passed, a default {@link ResultCallback} will be * used that would call {@link DataConsumer#onDeleteDataItemsResult(int)}. */ public void deleteDataItems(final Uri dataItemUri, @Nullable final ResultCallback<? super DataApi.DeleteDataItemsResult> callback) { assertApiConnectivity(); Wearable.DataApi.deleteDataItems(mGoogleApiClient, dataItemUri).setResultCallback( new ResultCallback<DataApi.DeleteDataItemsResult>() { @Override public void onResult(DataApi.DeleteDataItemsResult deleteDataItemsResult) { int statusCode = deleteDataItemsResult.getStatus().getStatusCode(); if (!deleteDataItemsResult.getStatus().isSuccess()) { Log.e(TAG, String.format( "Failed to delete data items (status code=%d): %s", statusCode, dataItemUri)); } if (callback == null) { for (DataConsumer consumer : mDataConsumers) { consumer.onDeleteDataItemsResult(statusCode); } } else { callback.onResult(deleteDataItemsResult); } } }); }
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"); } }); }
@Override public void onConnected(@Nullable Bundle bundle) { Log.d("WatchUpdaterReceiver", "onConnected: " + bundle); new Thread(new Runnable() { @Override public void run() { if(config!=null && config.size()>0) { PutDataMapRequest putDMR = PutDataMapRequest.create(PATH_WITH_FEATURE); putDMR.getDataMap().putAll(config); PutDataRequest request = putDMR.asPutDataRequest().setUrgent(); DataApi.DataItemResult result = Wearable.DataApi.putDataItem(mGoogleApiClient, request).await(); if (result.getStatus().isSuccess()) { Log.v("WatchUpdaterReceiver", "DataMap: " + config + " sent successfully to data layer "); } else { // Log an error Log.v("WatchUpdaterReceiver", "ERROR: failed to send DataMap to data layer"); } } } }).start(); }
/** * 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)); } } ); }
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)); } } ); }
/** * Overwrites the current config {@link DataItem}'s {@link DataMap} with {@code newConfig}. * If the config DataItem doesn't exist, it's created. */ public static void putConfigDataItem(GoogleApiClient googleApiClient, DataMap newConfig) { PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(PATH_WITH_FEATURE); DataMap configToPut = putDataMapRequest.getDataMap(); configToPut.putAll(newConfig); Wearable.DataApi.putDataItem(googleApiClient, putDataMapRequest.asPutDataRequest()) .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "putDataItem result status: " + dataItemResult.getStatus()); } } }); }
public static PendingResult<DataApi.DataItemResult> sendAcceleroMessage(AccelerometerData accelerometerData, GoogleApiClient googleApiClient) { PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACC_PATH); acceleroMessageUri = dataMapRequest.getUri(); DataMap dataMap = dataMapRequest.getDataMap(); //Data set dataMap.putFloatArray(VALUE_STR, new float[]{accelerometerData.getAccX(), accelerometerData.getAccY(), accelerometerData.getAccZ()}); // Data Push PutDataRequest request = dataMapRequest.asPutDataRequest(); PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(googleApiClient, request); return pendingResult; }
private void syncData(PutDataMapRequest putDataMapRequest, boolean isUrgent) { assertApiConnectivity(); if (isUrgent) { putDataMapRequest = putDataMapRequest.setUrgent(); } PutDataRequest request = putDataMapRequest.asPutDataRequest(); Wearable.DataApi.putDataItem(mGoogleApiClient, request) .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(@NonNull DataApi.DataItemResult dataItemResult) { if (dataItemResult.getStatus().isSuccess()) { WearUtil.logD(TAG, "putDataItem success"); } else { String errStr = dataItemResult.getStatus().getStatusMessage(); Log.e(TAG, "ERROR: failed to putDataItem, status code: " + dataItemResult.getStatus().getStatusCode() + ",status message:" + errStr); } } }); }
@Override public void handleMessage(Message msg) { Log.d("PrefSyncService", "handleMessage: " + msg.what); if (googleApiClient.isConnected()) { if (msg.what == ACTION_SYNC_ALL) { Wearable.DataApi.getDataItems(googleApiClient, DATA_SETTINGS_URI, DataApi.FILTER_PREFIX) .setResultCallback(this); } else { processQueue(); } } else { if (msg.what >= pendingAction) { pendingAction = msg.what; } } }
private void sendData(Asset asset) { if (asset == null) { return; } PutDataMapRequest dataMap = PutDataMapRequest.create(WEAR_PATH); byte[] arr = asset.getData(); dataMap.getDataMap().putByteArray(DATA_ASSET_FILE, arr); dataMap.getDataMap().putLong("timestamp", Calendar.getInstance().getTimeInMillis()); PutDataRequest request = dataMap.asPutDataRequest(); PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleAppiClient, request); pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { Log.d(TAG, "onResult result:" + dataItemResult.getStatus()); } }); }
private void sendData() { ArrayList<Double> tempValues = ForecastFragment.getTempValues(); Log.v("TAG", tempValues.size()+""); final DataMap dataMap = new DataMap(); dataMap.putDouble("max", tempValues.get(0)); dataMap.putDouble("min", tempValues.get(1)); dataMap.putInt("icon", ForecastFragment.getIconId()); dataMap.putLong("timestamp", System.currentTimeMillis()); new Thread( new Runnable() { @Override public void run() { PutDataMapRequest dataMapRequest = PutDataMapRequest.create(MOBILE_MESSAGE_PATH).setUrgent(); dataMapRequest.getDataMap().putAll(dataMap); PutDataRequest request = dataMapRequest.asPutDataRequest(); DataApi.DataItemResult result = Wearable.DataApi.putDataItem(mApiClient, request).await(); if(result.getStatus().isSuccess()){ Log.v("TAG", "Sent successfully"); }else { Log.v("TAG", "Failed to send"); } } }).start(); }
/** * Method that puts String data into the Wearable.DataApi layer. * * @param context * @param key key value of the data * @param data the data as string * @param path the path to store * @return Obsservable that emits the result */ public static Observable<DataApi.DataItemResult> putData(Context context, final String key, final String data, final String path) { return getGoogleApiClient(context) .flatMap(new Func1<GoogleApiClient, Observable<DataApi.DataItemResult>>() { @Override public Observable<DataApi.DataItemResult> call(final GoogleApiClient googleApiClient) { return Observable.create(new Observable.OnSubscribe<DataApi.DataItemResult>() { @Override public void call(final Subscriber<? super DataApi.DataItemResult> subscriber) { PutDataMapRequest putDataMapReq = PutDataMapRequest.create(path); putDataMapReq.getDataMap().putString(key, data); PutDataRequest putDataReq = putDataMapReq.asPutDataRequest(); Wearable.DataApi.putDataItem(googleApiClient, putDataReq).setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(@NonNull DataApi.DataItemResult dataItemResult) { subscriber.onNext(dataItemResult); subscriber.onCompleted(); } }); } }); } }); }
/** * An Observable that emits whenever data changes in the wear datalayer * * @param context * @return Observable that emits DataEventBuffer */ public static Observable<DataEventBuffer> getDataEvents(Context context) { return getGoogleApiClient(context) .flatMap(new Func1<GoogleApiClient, Observable<DataEventBuffer>>() { @Override public Observable<DataEventBuffer> call(final GoogleApiClient googleApiClient) { return Observable.create(new Observable.OnSubscribe<DataEventBuffer>() { @Override public void call(final Subscriber<? super DataEventBuffer> subscriber) { Wearable.DataApi.addListener(googleApiClient, new DataApi.DataListener() { @Override public void onDataChanged(DataEventBuffer dataEventBuffer) { subscriber.onNext(dataEventBuffer); } }); } }); } }); }
/** * 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)); } } ); }
/** * This methods gets the string data from the wear DataApi and uses gson to deserialize json to a bundles object. * * @param googleApiClient a connected googleApiClient * @param nodeId the node to get the data from * @return Observable that emits Bundles */ private static Observable<Bundles> getDataItem(final GoogleApiClient googleApiClient, final String nodeId) { return Observable.create(new Observable.OnSubscribe<Bundles>() { @Override public void call(final Subscriber<? super Bundles> subscriber) { final PendingResult<DataApi.DataItemResult> dataItem = Wearable.DataApi.getDataItem(googleApiClient, DatalayerUtil.getUriForDataItem(nodeId, DATA_PATH)); dataItem.setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(@NonNull DataApi.DataItemResult dataItemResult) { if (dataItemResult.getDataItem() == null) { subscriber.onError(new Throwable("No data")); return; } String string = DataMapItem.fromDataItem(dataItemResult.getDataItem()).getDataMap().getString(BUNDLE_KEY); Bundles bundles = new Gson().fromJson(string, Bundles.class); subscriber.onNext(bundles); subscriber.onCompleted(); } }); } }).observeOn(Schedulers.io()); }
public static PendingResult<DataApi.DataItemResult> sendActionTypeMessage(int actionType, GoogleApiClient googleApiClient) { PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACTION_TYPE_PATH); DataMap dataMap = dataMapRequest.getDataMap(); //Data set dataMap.putInt(VALUE_STR, actionType); // Data Push PutDataRequest request = dataMapRequest.asPutDataRequest(); PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(googleApiClient, request); return pendingResult; }
/** * Retrieves a single data item with the given {@code dataItemUri} asynchronously. Caller can * specify a {@link ResultCallback} or pass a {@code null}; if a {@code null} is passed, a * default {@link ResultCallback} will be used that will call * {@link DataConsumer#onGetDataItem(int, DataApi.DataItemResult)}. * * @see DataConsumer#onGetDataItem(int, DataApi.DataItemResult) */ public void getDataItem(Uri dataItemUri, @Nullable final ResultCallback<? super DataApi.DataItemResult> callback) { assertApiConnectivity(); Wearable.DataApi.getDataItem(mGoogleApiClient, dataItemUri).setResultCallback( new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { int statusCode = dataItemResult.getStatus().getStatusCode(); if (!dataItemResult.getStatus().isSuccess()) { Log.e(TAG, "Failed to get the data item, status code: " + statusCode); } if (callback == null) { for (DataConsumer consumer : mDataConsumers) { consumer.onGetDataItem(statusCode, dataItemResult); } } else { callback.onResult(dataItemResult); } } }); }
@Override public void onVisibilityChanged(boolean visible) { super.onVisibilityChanged(visible); if (visible) { if (mGoogleApiClient != null) { mGoogleApiClient.connect(); } registerReceiver(); // Update time zone in case it changed while we weren't visible. mCalendar.setTimeZone(TimeZone.getDefault()); mCalendar.setTime(new Date()); initFormats(); } else { unregisterReceiver(); if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { Wearable.DataApi.removeListener(mGoogleApiClient, this); mGoogleApiClient.disconnect(); } } // Whether the timer should be running depends on whether we're visible (as well as // whether we're in ambient mode), so we may need to start or stop the timer. updateTimer(); }
private void onButtonClicked() { PendingResult<DataApi.DataItemResult> pendingResult = Message.sendActionMessage(mGoogleApiClient); pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(@NonNull DataApi.DataItemResult dataItemResult) { Message.emptyActionMessage(mGoogleApiClient); } }); Intent intent = new Intent(this, ConfirmationActivity.class); intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION); intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.action_sent)); startActivity(intent); }
private void getWeatherInfo() { PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(KEY_PATH); String id = UUID.randomUUID().toString(); putDataMapRequest.getDataMap().putString(KEY_UUID, id); Log.d(LOG_TAG,"getWeatherInfo id=" +id); 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.d("WatchFaceService", "Data call failed"); } } }); }
/** * Sends payload, should be only called from this class and its subclasses * * @param path * @param data */ protected void sendDataItemWithoutConnectionCheck(DataPath path, TimeStampStorable data) { Logger.logD(getClass().getSimpleName(), "Sending " + path); PutDataRequest request = PutDataRequest.create(path.getPath()); final byte[] dataToSend = data.getAsBytes(); // check data size whether to send as and asset or plain data item if (dataToSend.length >= MAX_DATA_ITEM_SIZE_B) { request.putAsset(DataPath.DEFAULT_ASSET_KEY, Asset.createFromBytes(dataToSend)); } else { request.setData(dataToSend); } if (path.isUrgent()) { request.setUrgent(); } PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, request); }
@Override protected Bitmap doInBackground(Asset... params) { if (params.length > 0) { Asset asset = params[0]; InputStream assetInputStream = Wearable.DataApi.getFdForAsset( mGoogleApiClient, asset).await().getInputStream(); if (assetInputStream == null) { Log.w(TAG, "Requested an unknown Asset."); return null; } return BitmapFactory.decodeStream(assetInputStream); } else { Log.e(TAG, "Asset must be non-null"); return null; } }
@Override public void onVisibilityChanged(boolean visible) { super.onVisibilityChanged(visible); if (visible) { mGoogleApiClient.connect(); Log.d(TAG, "Google Api Client connected!"); registerReceiver(); // Update time zone in case it changed while we weren't visible. mTime.clear(TimeZone.getDefault().getID()); mTime.setToNow(); } else { if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { Wearable.DataApi.removeListener(mGoogleApiClient, this); mGoogleApiClient.disconnect(); } unregisterReceiver(); } // Whether the timer should be running depends on whether we're visible (as well as // whether we're in ambient mode), so we may need to start or stop the timer. updateTimer(); }
private void sendTodayForecast(double maxTemp, double minTemp, int weatherId) { PutDataMapRequest dataMap = PutDataMapRequest.create(FORECAST_PATH).setUrgent(); String lowString = Utility.formatTemperature(getContext(), minTemp); String highString = Utility.formatTemperature(getContext(), maxTemp); dataMap.getDataMap().putString(MAX_TEMP_KEY, highString); dataMap.getDataMap().putString(MIN_TEMP_KEY, lowString); int artResource = Utility.getArtResourceForWeatherCondition(weatherId); Asset weatherIcon = Utility.toAsset(artResource, getContext()); dataMap.getDataMap().putAsset(WEATHER_ICON_KEY, weatherIcon); dataMap.getDataMap().putLong(TIMESTAMP_KEY, System.currentTimeMillis()); PutDataRequest request = dataMap.asPutDataRequest(); Wearable.DataApi.putDataItem(mGoogleApiClient, request) .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { if (!dataItemResult.getStatus().isSuccess()) { Log.e(LOG_TAG, "ERROR: failed to putDataItem, status code: " + dataItemResult.getStatus().getStatusCode()); } } }); }
@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; }
@Override public void onConnectionFailed(ConnectionResult result) { if (mResolvingError) { // Already attempting to resolve an error. return; } else if (result.hasResolution() && mActivity != null) { try { mResolvingError = true; result.startResolutionForResult(mActivity, 1000); } catch (IntentSender.SendIntentException e) { // There was an error with the resolution intent. Try again. mGoogleApiClient.connect(); } } else { Log.e(TAG, "Connection to Google API client has failed"); mResolvingError = false; if (mListener != null) mListener.onDataUpdated(); Wearable.MessageApi.removeListener(mGoogleApiClient, this); Wearable.DataApi.removeListener(mGoogleApiClient, this); } }
public static PendingResult<DataApi.DataItemResult> sendInteractionTypeMessage(int interactionBitfield, GoogleApiClient googleApiClient) { PutDataMapRequest dataMapRequest = PutDataMapRequest.create(INTERACTION_TYPE_PATH); DataMap dataMap = dataMapRequest.getDataMap(); //Data set dataMap.putInt(VALUE_STR, interactionBitfield); // Data Push PutDataRequest request = dataMapRequest.asPutDataRequest(); PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(googleApiClient, request); return pendingResult; }
@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]); } } }); }