1

I have code like this for a watchface in Android Studio. I'm putting a random integer in the datamap so that the receiving side on the phone app can detect it as having been updated. I set this code in onConnected with the intent that every time the watchface is installed or 'chosen' by the user again, it will update data on the watchface - the main app receives this 'installed' status and then sends back updated information to the watch.

 public class MyWatchFace extends CanvasWatchFaceService {    
 ///
 private class Engine extends CanvasWatchFaceService.Engine implements
        DataApi.DataListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener{

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Wearable.DataApi.addListener(mGoogleApiClient, this);

        PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/installed");
        putDataMapReq.getDataMap().putInt(INSTALLED, new Random().nextInt());
        PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
        putDataReq.setUrgent();
        Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
                .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                    @Override
                    public void onResult(DataApi.DataItemResult dataItemResult) {
                        Log.d(TAG, "Sending Install Status was successful: " + dataItemResult.getStatus()
                                .isSuccess());
                    }
                });
    }

The problem is that this section of code

Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
                .setResultCallback

never seems to be called when I debug it, while installing or choosing the watchface again. I have to go to the phone app and update information on there for the information to be sent back to the watch.

I originally put the callback in the onCreate method of the Engine, but that was never called either. This is also where I create and connect the mGoogleApiClient, which IS successfully called.

Is there a more appropriate method to put this installed update code in? Why is it never being called in these 2 methods?

Everything else about the watch works fine - it successfully retrieves data from the phone app when the phone app data changes. PS, I use the same mGoogleApiClient to retrieve this phone data to also send the install status, in case you think that might be a problem. Do I have to create two separate clients?

0 Answers0