Android two-way communication between handheld and portable

My goal
I am creating an application / package where wear and handheld can make data-level changes and be notified of those changes. I am using GoogleApiClient and DataApi.DataListener interface.

Current Progress
At this point, the activity on my Wearable is successfully updating the counts in the data layer, and this change is then detected and displayed in Handheld by displaying new counters.

Problem
Detection only works in one direction. Although the change initiated by Wearable successfully refreshes accounts on Handheld, the change initiated by Handheld was not detected on Wearable.

What I have tried
I have been looking at StackExchange and Google for hours and I have not found any cases where both Wearable and Handheld updated data layer objects. Most of the time it was just Handheld update and Wearable recognition, or vice versa. One way communication works fine for me. I need two way.

Snippet of portable code

public class ListenActivity extends Activity implements
    DataApi.DataListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
// ConnectionCallback methods, UI methods not included in snippet

private static final String COUNT_KEY = "com.example.count";
private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listen);

    handler.post(displayCounts);

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

@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
    for (DataEvent event : dataEvents)
    {
        if (event.getType() == DataEvent.TYPE_CHANGED)
        {
            DataItem item = event.getDataItem();
            if (item.getUri().getPath().compareTo("/count") == 0)
            {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                count = dataMap.getInt(COUNT_KEY);
            }
        }
    }
}

// A UI button calls this method to reset the count, but the Wearable isn't calling its onDataChanged() method.
public void resetCount(View view)
{
    PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
    putDataMapReq.getDataMap().putInt(COUNT_KEY, 0);
    PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult =
            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
} }

      

Fragment of worn-out code

public class WearListenActivity extends Activity implements
    DataApi.DataListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

// ConnectionCallback methods, UI methods not included in snippet

private static final String COUNT_KEY = "com.example.count";
private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
    mDismissOverlay.showIntroIfNecessary();
    mGestureDetector = new GestureDetectorCompat(this, new GestureListener());

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApiIfAvailable(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

// This method is never called on the Wearable. 
// It isn't detecting the changes made by Handheld resetCount() method?
@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
    for (DataEvent event : dataEvents)
    {
        if (event.getType() == DataEvent.TYPE_CHANGED)
        {
            DataItem item = event.getDataItem();
            if (item.getUri().getPath().compareTo("/count") == 0)
            {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                count = dataMap.getInt(COUNT_KEY);
            }
        }
    }
}

// User taps the screen, this method is called. Works perfectly fine since handheld updates its counts.
private void updateDataLayer()
{
    PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
    putDataMapReq.getDataMap().putInt(COUNT_KEY, count);
    PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult =
            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
} }

      

+3


source to share


1 answer


Perhaps this is because you are listening to events in the Activity. I am using WearableListenerService and it works both ways.



You can find an example here: Handling Data Layer Events

+2


source







All Articles