【发布时间】:2016-12-09 12:53:44
【问题描述】:
我正在尝试将手表与模拟器同步我已正确执行步骤并将手机与手表连接。到目前为止,我可以通过手机更改表盘并处理一些通知。我已经创建了我的应用程序的表盘,我不会在我的手表上显示温度。
这是我将数据与手表同步的代码:
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
【问题讨论】:
标签: android android-emulator wear-os android-wear-data-api