使用DataMap发送和接收数据-Android Wearable [英] Send and Receiving Data using DataMap - Android Wearable

查看:205
本文介绍了使用DataMap发送和接收数据-Android Wearable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Android Wearable项目,并且试图将数据从移动设备发送到Wearable模拟器。我正在尝试将接收到的数据记录到可穿戴设备中,但未记录。

I am working on an Android Wearable project and I am trying to send data from the mobile device to the Wearable emulator. I am trying log the received data in the wearable but it is not logging.

这些是我到目前为止所做的。

These are what I've done so far.

1)打开Android Wear应用以使仿真器状态为已连接

1) Open the Android Wear app to get the Emulator status to be "connected"

2)连接我的移动设备

2) Connect my mobile device

3)运行adb -d forward tcp:5601 tcp:5601将移动设备与仿真器连接

3) run adb -d forward tcp:5601 tcp:5601 to connect mobile device with emulator

4)在我的移动设备的发送类中实现GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener并在onCreate中构建客户端:

4) Implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener in the sending class in my mobile device and building client in onCreate:

    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

5)在我的移动设备中,使用完美记录的 weatherDescription创建一个dataItem,可以使用DataApi使用putDataItem发送到可穿戴设备:

5) In my mobile device, create a dataItem, using 'weatherDescription" that logs perfectly, where I can using the DataApi to send to the wearable using putDataItem:

这是weather的日志说明:I / weatherDescription:零星的云

This is the log for weatherDescription: I/weatherDescription: scattered clouds

            putDataMapReq = PutDataMapRequest.create("/data");
            Log.i("weatherDescription", weatherDescription);
            putDataMapReq.getDataMap().putString("weatherDescription", weatherDescription);

            PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
            Wearable.DataApi.putDataItem(googleClient, putDataReq); 

5)在Wearable的接收类(我的自定义表面类)中实施GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener设备并在onCreate中构建客户端:

5)Implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener in the receiving class (My custom watchface class) in my Wearable device and building client in onCreate:

    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

6)在可穿戴设备的onConnect中设置侦听器:

6) Set up the listener in onConnect in the Wearable:

@Override
public void onConnected(Bundle bundle) {

    Wearable.DataApi.addListener(mGoogleApiClient, this).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            Log.i("myTag", String.valueOf(status));
        }
    });
}

似乎在接收/可穿戴端正确连接:

It looks like it's connecting properly on the receiving/wearable end:

I / myTag:状态{statusCode =成功,分辨率=空}

I/myTag: Status{statusCode=SUCCESS, resolution=null}

7)在onDataChanged中,记录接收到的信息:

7) In onDataChanged, log the received information:

    public void onDataChanged(DataEventBuffer dataEventBuffer) {
        Log.i("myTag", "in on Data Changed");
        for (DataEvent event : dataEventBuffer){
            if(event.getType() == DataEvent.TYPE_CHANGED){
                DataItem item = event.getDataItem();

                if(item.getUri().getPath().compareTo("/data") == 0 ){
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();

                    dataMap.getString("weatherDescription");

                    Log.i("myTag", dataMap.getString("weatherDescription"));
                }
            }
        }
    }


推荐答案

我终于知道了。在表面方面,我在WeatherWatchFaceService类(而不是Engine类)中声明了Google Api Client实例mGoogleApiClient。在将它从WeatherWatchFaceService移到Engine之后,表端收到了我试图从移动设备发送的内容。

I finally figured it out. In the watchface side, I declared the Google Api Client instance, mGoogleApiClient, in the WeatherWatchFaceService class as opposed to the Engine class. After I moved it from WeatherWatchFaceService to Engine, the watch side received what I was trying to send from the mobile.

干杯。

这篇关于使用DataMap发送和接收数据-Android Wearable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆