Search code examples
androidandroid-emulatorwear-osandroid-wear-data-api

Data is not Synced between Watch Emulator and Android Phone


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


Solution

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