1

I am trying to sync watch with emulator I have followed the steps correctly and got my phone connected with the watch. So far I can change the watch faces from my phone and handle a few notifications. I have created watch face of my app and I am not to display temperature on to my watch.

Here is my code for the in which I am syncing my data with the watch:

private void connectToWatchFace() {
        Log.d(LOG_TAG, "connectToWatchFace()");
        mGoogleApiClient = new GoogleApiClient.Builder(getContext())
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }

    private void sendDataToWatchFace(double highTemperature, double lowTemperature, int weatherConditionId) {
        Log.d(LOG_TAG, "sendDataToWatchFace()");
        PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/sunshine").setUrgent();

        putDataMapRequest.getDataMap().putDouble("high_temperature", highTemperature);
        putDataMapRequest.getDataMap().putDouble("low_temperature", lowTemperature);
        putDataMapRequest.getDataMap().putLong("timestamp", new Date().getTime());
        Log.d(LOG_TAG, "High Temperature: " + highTemperature + " " + "Low Temperature: " + lowTemperature);

        int drawableResourceId = Utility.getIconResourceForWeatherCondition(weatherConditionId);
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), drawableResourceId);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
        Asset asset = Asset.createFromBytes(byteArrayOutputStream.toByteArray());
        putDataMapRequest.getDataMap().putAsset("icon", asset);

        PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest)
                .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                    @Override
                    public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                        if (dataItemResult.getStatus().isSuccess()) {
                            Log.d(LOG_TAG, "dataItemResult.getStatus().isSuccess()");
                        } else {
                            Log.d(LOG_TAG, "NOT dataItemResult.getStatus().isSuccess()");
                        }
                    }
                });
    }

This is code in which I am receiving the data from phone.

 @Override
        public void onDataChanged(DataEventBuffer dataEventBuffer) {
            Log.d(TAG, "onDataChanged()");
            for (DataEvent dataEvent : dataEventBuffer) {
                DataItem dataItem = dataEvent.getDataItem();

                String path = dataItem.getUri().getPath();
                Log.d(TAG, "path: " + path);
                if (path.equals("/sunshine")) {
                    DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();

                    mHighTemperature = dataMap.getDouble("high_temperature");
                    mLowTemperature = dataMap.getDouble("low_temperature");
                    Log.d(TAG, "high temperature: " + mHighTemperature + ", low temperature: " + mLowTemperature);
                    Asset iconAsset = dataMap.getAsset("icon");
                    if (iconAsset != null) {
                        new SunshineWatch.Engine.LoadBitmapAsyncTask().execute(iconAsset);
                    }
                    // Force UI update
                    invalidate();
                }
            }
        }

        private class LoadBitmapAsyncTask extends AsyncTask<Asset, Void, Bitmap> {

            @Override
            protected Bitmap doInBackground(Asset... params) {
                if (params.length > 0 && params[0] != null) {
                    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
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    Log.d(TAG, "onPostExecute bitmap is NOT null");
                    mIconBitmap = Bitmap.createScaledBitmap(
                            bitmap,
                            getResources().getDimensionPixelSize(R.dimen.icon_width_height),
                            getResources().getDimensionPixelSize(R.dimen.icon_width_height),
                            false
                    );
                    invalidate();
                } else {
                    Log.d(TAG, "onPostExecute bitmap is NULL");
                }
            }
        }

    }

I don't find any results in the LOGCAT too. I am using play services version 10.0.0

Contextioner
  • 75
  • 11

3 Answers3

1

Please check and make sure that you already have the latest version of the Google Play services.

As mentioned in including the correct libraries, as part of the Project Wizard, the correct dependencies are imported for you in the appropriate module's build.gradle file.

To sync and send data between wearables and handhelds with the Wearable Data Layer APIs, you need the latest version of Google Play services. If you're not using these APIs, remove the dependency from both modules.

Furthermore, you also need to make sure that you have an established connection when syncing. Sometimes the emulator can be temperamental and either not sync all your content or randomly disconnect. This should be rare, but if it happens start the process again as stated in this article.

You may want to also check this tutorial for additional information:

Teyam
  • 7,686
  • 3
  • 15
  • 22
  • I am using the latest Google Play Services 10.0.0 – Contextioner Dec 12 '16 at 07:21
  • W/GooglePlayServicesUtil: Google Play services out of date. Requires 10084000 but found 9841574 12-12 13:17:20.030 2750-2750/com.sagar.sunshine D/SunshineWatch: onConnectionFailedConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null} – Contextioner Dec 12 '16 at 07:48
1

Use compileSdkVersion 23 in gradle. Android Wear is compatible with API 23 for now.

Contextioner
  • 75
  • 11
0

By working on Android wearable app I faced with different situations when wearable can't reach handset. The list of questions below helps to identify root cause

  • Are they paired via bluetooth?
  • Does port forwarded on the device?
  • Does wearable see the node?
  • Does urgent flag turned on to sync immedeatly?
  • Does data is cached on the device side and just doesn't sync not invalidated data?
  • Does format of wearable uri you use to obtanin data from cache are match "wear://node_id/resource_path" ?
Gleichmut
  • 5,953
  • 5
  • 24
  • 32