简体   繁体   English

Watch Emulator和Android Phone之间的数据未同步

[英]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. 我也没有在LOGCAT中找到任何结果。 I am using play services version 10.0.0 我正在使用10.0.0版的播放服务

Please check and make sure that you already have the latest version of the Google Play services . 请检查并确保您已经拥有最新版本的Google Play服务

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. 包含正确的库所述 ,作为项目向导的一部分,正确的依赖项会在适当模块的build.gradle文件中为您导入。

To sync and send data between wearables and handhelds with the Wearable Data Layer APIs, you need the latest version of Google Play services. 要使用可穿戴数据层API在可穿戴设备和手持设备之间同步和发送数据,您需要最新版本的Google Play服务。 If you're not using these APIs, remove the dependency from both modules. 如果您不使用这些API,请从两个模块中删除依赖项。

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: 您可能还需要查看本教程以获取更多信息:

Use compileSdkVersion 23 in gradle. 在gradle中使用compileSdkVersion 23。 Android Wear is compatible with API 23 for now. Android Wear目前与API 23兼容。

By working on Android wearable app I faced with different situations when wearable can't reach handset. 通过开发Android可穿戴应用程序,当可穿戴设备无法连接手机时,我遇到了不同的情况。 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" ? 您用来从缓存中获取Obtanin数据的可穿戴uri格式是否与“ wear:// node_id / resource_path”匹配?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM