[英]Data is not Synced between Watch Emulator and Android Phone
我正在嘗試與模擬器同步手表,我已正確按照步驟進行操作,並使手機與手表連接。 到目前為止,我可以從手機上更換表面並處理一些通知。 我已經創建了我的應用程序的表面,但是我不希望在手表上顯示溫度。
這是我與手表同步數據的代碼:
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版的播放服務
請檢查並確保您已經擁有最新版本的Google Play服務 。
如包含正確的庫所述 ,作為項目向導的一部分,正確的依賴項會在適當模塊的build.gradle
文件中為您導入。
要使用可穿戴數據層API在可穿戴設備和手持設備之間同步和發送數據,您需要最新版本的Google Play服務。 如果您不使用這些API,請從兩個模塊中刪除依賴項。
此外,同步時還需要確保已建立連接。 有時,仿真器可能很氣質,要么不同步所有內容,要么隨機斷開連接。 這種情況很少見,但是如果發生這種情況,請按照本文所述再次啟動該過程。
您可能還需要查看本教程以獲取更多信息:
在gradle中使用compileSdkVersion 23。 Android Wear目前與API 23兼容。
通過開發Android可穿戴應用程序,當可穿戴設備無法連接手機時,我遇到了不同的情況。 以下問題列表有助於確定根本原因
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.