android - 无法将数据推送到 android wear(模拟器)

标签 android wear-os android-wear-data-api

我一直在尝试将数据推送到 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 list 中还有以下内容

<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>

附:我正在做的事情没有什么特别的。刚刚关注tutorial在开发者网站上给出。

最佳答案

对不起,我使用了答案,但我需要 50 的声誉才能发表评论:(

我在这里遇到了同样的问题 https://stackoverflow.com/... ,但现在我修好了。

好的,我与您分享我遇到的所有问题:

首先在手机上的AndroidManifest.xml文件中添加以下内容:

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

第二个让我感到困惑的是,onDataChanged() 仅在内部的 DataItem 真正“更改”时才被调用。所以它可能是第一次工作,但后来什么都不会发生。

我更改了手机上的代码,所以每次我尝试发送数据时都有不同的时间戳:

Asset asset = createAssetFromBitmap(bitmap);
        PutDataMapRequest request = PutDataMapRequest.create("/image");
        DataMap map = request.getDataMap();
        map.putLong("time", new Date().getTime()); // MOST IMPORTANT LINE FOR TIMESTAMP
        map.putAsset("profileImage", asset);
        Wearable.DataApi.putDataItem(mGoogleApiClient, request.asPutDataRequest());

我在这个 IO Video 中发现了

其余的代码看起来像你的。 我希望这会有所帮助。

关于android - 无法将数据推送到 android wear(模拟器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24676165/

相关文章:

android - 如何获得格式正确的完整日期?

android - 我应该在依赖项中使用什么版本的 recyclerview?

android - 在 JAR 文件中使用 R?

android - 禁用 android wear 挂起的 Intent 操作确认

java - 从 Aar 模块 Android 执行 Handheld 类中的方法

Android Wear + 网络服务调用

android - onKeyDown 在 TabActivity 中没有响应

android - 尝试在空对象引用上调用接口(interface)方法 'boolean com.google.common.util.concurrent.ListenableFuture.cancel(boolean)'

android - 如何在 Android Wear 模拟器上模拟来自心率传感器的数据?

安卓穿戴: DataItem API vs Channel API