无法将数据推送到Android的磨损(模拟器)模拟器、磨损、数据、Android

2023-09-04 10:12:19 作者:带你装逼丶带你飞

我一直在试图将数据推到Android磨损模拟器。但一切都是徒劳。我在模拟器上监听器接收不到任何呼叫任何责任。如果任何人试图从事磨损和将数据推穿请帮忙。

I have been trying to push data to the android wear emulator. But all in vain. My listener on the emulator is not receiving any calls whatsoever. If anyone else has tried working on wear and pushing data to wear please HELP.

这是我的接收器code看起来像

This is what my receiver code looks like

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

I also have the following in my manifest for the Android Wear activity

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

P.S。有什么了不起的事,我在做。只是跟着上的开发者网站给出的教程。

推荐答案

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

Sorry that I use Answer, but I need a reputation of 50 to comment :(

我在这里有同样的问题https://stackoverflow.com/... ,但现在我固定它。

I had the same problem here https://stackoverflow.com/... , but now I fixed it.

好吧,我跟大家分享我碰到的所有问题:

Ok I share with you all problems I ran into:

首先在AndroidManifest.xml文件上的移动添加以下内容:

First in the AndroidManifest.xml files on the mobile add the following:

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

二是什么困惑我有点是,这onDataChanged()只调用时,DataItem的内部真正的改变。因此,它的工作原理,也许是第一次,但后来什么也不会发生。

Second what confused me a bit was, that onDataChanged() is only called when the DataItem inside is really "changed". So it works maybe for the first time, but later nothing will happen.

我改变了手机的code,所以每次我尝试发送数据它有一个不同的时间戳记:

I changed the code on the mobile, so everytime I try to send data it has a different timestamp:

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

的code,其余看起来像你的。 我希望这帮助。

The rest of the code looks like yours. I hope this help.