android-servicelistenerwear-osandroid-wear-data-api

Issue with syncing data between watch and phone


I have developed an Android App which runs on both a smartphone and a smartwatch in parallel. On both devices, (let's say) it reads certain sensor data, processes that data (calculate its mean), and then store that results. The watch sends this result to the phone so all storing takes place on the phone. I used buffer writer to write a number into a text file every 5 seconds.

Now after every 320 data items exchanges from watch to the phone, my app on the phone gets killed and I get "the name of the app" is unfortunately stopped as a message. I can't figure it what why they stop exactly after this time? The app running on the watch continues to work fine. However, I cannot store its data because it cannot communicate to the phone version so I get this message "the app is unfortunately stopped as a message" every time the watch sends a number to phone for storing. The app has one activity which has a service (foreground).

Could it be that there is a limit on the amount of data being shared?

The code on watch:

// Create a data map and put data in it
    private void increaseCounter() {
        PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
        putDataMapReq.getDataMap().putInt(COUNT_KEY, count++); // I add current time here as well
        PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
        PendingResult<DataApi.DataItemResult> pendingResult =
                Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
    }

Code on phone (possible problematic area):

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

Solution

  • You have to use Service with StartForeground notification to be sure app is always working.

    and try to use START_STICKY flag while staring.

    UPDATE

    You have to dealloc memory of dataevent:

      @Override
        public void onDataChanged(DataEventBuffer dataEvents) {
    
            try{
                for(DataEvent dataEvent: dataEvents){
                    if(dataEvent.getType() != DataEvent.TYPE_CHANGED){
                        continue;
                    }
    ////... code
    
     dataEvents.release();
    }catch (Exception e){
                Log.v("SunshineWatchFace",e.getMessage());
            }
        }