Data is not Synced between Watch Emulator and Android Phone

时间:2016-12-09 12:53:45

标签: android android-emulator wear-os android-wear-data-api

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

3 个答案:

答案 0 :(得分:1)

请检查并确保您已拥有Google Play services的最新版本。

正如including the correct libraries中所述,作为项目向导的一部分,在适当的模块build.gradle文件中为您导入了正确的依赖项。

  

要使用可穿戴数据层API在可穿戴设备和手持设备之间同步和发送数据,您需要最新版本的Google Play服务。如果您不使用这些API,请从两个模块中删除依赖项。

此外,您还需要确保在同步时已建立连接。有时,模拟器可能会很有气质,要么不同步所有内容,要么随机断开连接。这种情况应该很少见,但如果发生这种情况,请按照article中所述再次启动流程。

您可能还需要查看本教程以获取更多信息:

答案 1 :(得分:1)

在gradle中使用compileSdkVersion 23。 Android Wear目前与API 23兼容。

答案 2 :(得分:0)

通过使用Android可穿戴应用程序,我遇到了可穿戴设备无法到达手机的不同情况。以下问题列表有助于确定根本原因

  • 他们是否通过蓝牙配对?
  • 端口是否在设备上转发?
  • wearable是否会看到节点?
  • 紧急标志是否已启用以立即同步?
  • 数据是否在设备端缓存,并且没有同步未经过无效的数据?
  • 您使用的可穿戴uri的格式是否与缓存中的数据匹配&#34; wear:// node_id / resource_path&#34; ?