Unable to push data to android wear (Emulator)

无人久伴 提交于 2019-11-28 19:34:54

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

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

Ok I share with you all problems I ran into:

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

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.

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());

I found out in this IO Video

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

I also had this problem, and it took hours to solve. My recommendation? Create a new project using Android Studio and select Android Wear and Phone + Tablet as the project types. This will give you the skeleton of a working project, then its just a matter of porting over to your existing project the differences from the Auto-generated skeleton.

For me, the problem ended up being a combination of the following:

  • In your defaultConfig build entry, the applicationId must be the same for both your wearable and mobile app, for example: defaultConfig { applicationId "com.rukkus.app" ... }
  • The mobile app must add the wearApp as a dependency, like so: dependencies { ... wearApp project(':wear') }

Additionally (and admittedly Im not sure that this is necessary), in the samples the Google Dev's connect to the Google API in the onCreate of the WearableListenerService (instead of in the onDataChanged method as the documentation illustrates). So my onCreate looks like this in the WearableListenerService:

@Override
public void onCreate() {
    super.onCreate();

    // create Google Client
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this.ctx)
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result){

                }
            })
            .addApi(Wearable.API).build();    

    //connect the client
    googleApiClient.connect();
    Log.i(TAG, "**creating google API client now**");
}

This took longer than I'd like to admit to get working, so hopefully this helps some future Googler out.

Your device applicationId and wearable applicationId must match, as @Bobby stated in his answer. I did not need to have the wearable app as a dependency.

sidecarcat

I discovered another cause to add to the checklist. The wearable and mobile apps must be built with the same versions of the wear libraries. Check the versions in your gradle dependencies.

compile 'com.google.android.support:wearable:1.3.0'
compile 'com.google.android.gms:play-services-wearable:8.1.0'

(I do not have enough reputation points to comment)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!