1

我嘗試同步與模擬器我已經正確遵循的步驟,並得到了我的手機,手錶連接手錶之間同步。到目前爲止,我可以通過手機更換手錶並處理幾個通知。我已經創建了我的應用程序的表面,我不會在我的手錶上顯示溫度。數據未觀看仿真器和Android手機

這裏是我的,我正在與手錶同步我的數據的代碼:

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()"); 
         } 
        } 
       }); 
    } 

這是在我從電話中接收數據的代碼。

@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"); 
       } 
      } 
     } 

    } 

我在LOGCAT中也找不到任何結果。我使用的播放服務版本10.0.0

回答

1

使用compileSdkVersion 23 gradle這個。 Android Wear現在與API 23兼容。

1

請檢查並確保你已經有最新版本的Google Play services的。

including the correct libraries中所述,作爲項目嚮導的一部分,將在適當的模塊的build.gradle文件中爲您導入正確的相關性。

要在可穿戴設備和掌上電腦之間使用可穿戴數據層API同步併發送數據,您需要最新版本的Google Play服務。如果您不使用這些API,請從兩個模塊中刪除依賴項。

此外,您還需要確保在同步時建立了連接。有時候模擬器可能會很溫和,或者不能同步你的所有內容或隨意斷開連接。這應該是很少見的,但如果發生這種情況,請按照article中的說明再次啓動此過程。

您可能還需要檢查本教程的其他信息:

+0

我使用最新的Google Play Services 10.0.0 – Contextioner

+0

W/GooglePlayServicesUtil:Google Play服務已過時。需要10084000,但找到9841574 12-12 13:17:20.030 2750-2750/com.sagar。陽光D/SunshineWatch:onConnectionFailedConnectionResult {statusCode = SERVICE_VERSION_UPDATE_REQUIRED,分辨率=空,消息=空} – Contextioner

0

通過在我面對不同的情況下,當可穿戴無法達到手機的Android穿戴式應用工作。以下問題列表有助於確定根本原因

  • 它們是否通過藍牙配對?
  • 端口是否在設備上轉發?
  • 穿戴式可見節點嗎?
  • 緊急標誌是否打開以便同步?
  • 數據是否緩存在設備端,只是不同步不失效的數據?
  • 您可以使用來自緩存中的數據的可穿戴uri格式是否匹配「wear:// node_id/resource_path」?