5

我正在尝试将字符串数组从我的手机发送到我的穿戴设备,我在手机上创建了一个服务,该服务应该使用以下代码发送数据:

public class SendDataService extends Service {

private static final String TAG = "SendDataService";

@Override
public void onCreate(){
    GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    // Now you can use the data layer API
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
    String[] eventStrings = Events.eventsString(Events.readCalendarEvent(this));
    PutDataMapRequest dataMap = PutDataMapRequest.create("/events");
    dataMap.getDataMap().putStringArray("events", eventStrings);
    PutDataRequest request = dataMap.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
            .putDataItem(mGoogleApiClient, request);
    mGoogleApiClient.disconnect();
}

从手表上,我尝试使用以下代码在我的主要活动中获取它:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_agenda_wear);
    this.context = this;
    GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    // Now you can use the data layer API
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();


    PendingResult<DataItemBuffer> results = Wearable.DataApi.getDataItems(mGoogleApiClient);
    results.setResultCallback(new ResultCallback<DataItemBuffer>() {
        @Override
        public void onResult(DataItemBuffer dataItems) {
            if (dataItems.getCount() != 0) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(dataItems.get(0));

                // This should read the correct value.
                AgendaWear.this.eventString = dataMapItem.getDataMap().getStringArray("events");
            }

            dataItems.release();
        }
    });
    mGoogleApiClient.disconnect();
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            if (AgendaWear.this.eventString == null){
                Toast.makeText(AgendaWear.this, "No data",Toast.LENGTH_LONG).show();
                AgendaWear.this.eventString = new String[0];
            }
            AgendaWear.listItems = eventArray(AgendaWear.this.eventString);
            mListView = (WearableListView) stub.findViewById(R.id.listView);
            mListView.setAdapter(new MyAdapter(AgendaWear.this));
            mListView.setClickListener(AgendaWear.this);
        }
    });
}

但不幸的是,我总是得到“没有数据”的祝酒词,我有点卡在这里,我的<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />应用程序的穿戴和移动部分都有线路,但我似乎没有得到任何数据。预先感谢您的帮助

4

1 回答 1

7

也许我可以帮忙。

此代码适用于我,从设备发送字符串数组以在可穿戴设备中接收:

设备代码:

@Override
public void onCreate() {

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

    googleApiClient.connect();
}    


@Override
public void onConnected(Bundle bundle) {

  String [] myData = new String[]{"data1", "data2", "data3"};
  new DataTask (getActivity(), myData, myListener).execute();
}


class DataTask  extends AsyncTask<Node, Void, Void> {

    private final String[] contents;
    private MyListener myListener;
    Context c;

    public DataTask (Context c, String [] contents, MyListener myListener) {
        this.c = c;
        this.contents = contents;
        this.myListener = myListener;
    }

    @Override
    protected Void doInBackground(Node... nodes) {

        PutDataMapRequest dataMap = PutDataMapRequest.create ("/myapp/myevent");
        dataMap.getDataMap().putStringArray("contents", contents);

        PutDataRequest request = dataMap.asPutDataRequest();

        DataApi.DataItemResult dataItemResult = Wearable.DataApi
                .putDataItem(googleApiClient, request).await();


        Log.d ("[DEBUG] SendDataCoolTask - doInBackground", "/myapp/myevent" status, "+getStatus());
        return null;
    }
}

可穿戴代码:

@Override
public void onConnected(Bundle bundle) {

    Wearable.DataApi.addListener(googleApiClient, this);
}

    @Override
public void onDataChanged(DataEventBuffer dataEvents) {

    for (DataEvent event: dataEvents) {

        Log.d("[DEBUG] DeviceService - onDataChanged",
                "Event received: " + event.getDataItem().getUri());

        String eventUri = event.getDataItem().getUri().toString();

        if (eventUri.contains ("/myapp/myevent")) {

            DataMapItem dataItem = DataMapItem.fromDataItem (event.getDataItem());
            String[] data = dataItem.getDataMap().getStringArray("contents");

            Log.d("[DEBUG] DeviceService - onDataChanged", "Sending timeline to the listener");

            myListener.onDataReceived(data);
        }
    }
}
于 2014-08-21T17:59:31.023 回答