我一直在尝试将数据推送到 android wear 模拟器。但一切都是徒劳的。我在模拟器上的听众没有接到任何电话。如果其他人尝试过进行磨损并推送数据以进行磨损,请帮助。
这就是我的接收器代码的样子
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrcode_generation);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
ivQrImage = (ImageView) stub.findViewById(R.id.ivQRImage);
}
});
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED &&
event.getDataItem().getUri().getPath().equals("/image")) {
final DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
final Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage");
final Bitmap bitmap = loadBitmapFromAsset(profileAsset);
Log.d(TAG, ""+bitmap);
if (null != bitmap) {
ivQrImage.setImageBitmap(bitmap);
bitmap.recycle();
}
}
}
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
if (null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
Wearable.DataApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
super.onStop();
}
public Bitmap loadBitmapFromAsset(Asset asset) {
if (asset == null) {
throw new IllegalArgumentException("Asset must be non-null");
}
ConnectionResult result =
mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
if (!result.isSuccess()) {
return null;
}
// convert asset into a file descriptor and block until it's ready
InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
mGoogleApiClient, asset).await().getInputStream();
mGoogleApiClient.disconnect();
if (assetInputStream == null) {
Log.w(TAG, "Requested an unknown Asset.");
return null;
}
// decode the stream into a bitmap
return BitmapFactory.decodeStream(assetInputStream);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG,"Connection Failed");
}
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
Wearable.MessageApi.addListener(mGoogleApiClient, this);
}
这就是我推动的方式
private void pushImageToWear() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_code);
Asset asset = createAssetFromBitmap(bitmap);
PutDataMapRequest dataMap = PutDataMapRequest.create("/image");
dataMap.getDataMap().putAsset("profileImage", asset);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
.putDataItem(mGoogleApiClient, request);
}
我的 Android Wear 活动清单中还有以下内容
<activity
android:name=".QRCodeReceptionActivity"
android:label="@string/app_name"
android:exported="true"
android:allowEmbedded="true"
android:taskAffinity=""
android:theme="@android:style/Theme.DeviceDefault.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
PS我正在做的事情没有什么特别的。只需按照开发人员网站上给出的教程进行操作即可。