conedmiro
conedmiro

Reputation: 103

Sending ArrayList<> DataItem from mobile to wear

Currently my issue is that whenever I try to access the ArrayList<> I've sent via my DataItem layer, it tells me the object is null.

Can someone shed some light as to where the issue is falling?

MOBILE

public class MainActivity extends AppCompatActivity implements  GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

--variables

@Override
protected void onCreate(Bundle savedInstanceState) {

    if(!PreferenceManager.getDefaultSharedPreferences(this)
            .getStringSet("wristaroo", new HashSet<String>()).isEmpty()) {
        tasksSet = PreferenceManager.getDefaultSharedPreferences(this)
                .getStringSet("wristaroo", new HashSet<String>());
        actsList = new ArrayList<>(tasksSet);
    }
    -----

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

    googleApiClient.connect();

    super.onCreate(savedInstanceState);
    -----
    Button btnSave = (Button) findViewById(R.id.sendButton);
    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            PutDataMapRequest dataMap = PutDataMapRequest.create("/wristaroo/customschedule");
            dataMap.getDataMap().putStringArrayList("schedule", actsList);
            PutDataRequest request = dataMap.asPutDataRequest();


            PendingResult<DataApi.DataItemResult> result = Wearable.DataApi
                    .putDataItem((googleApiClient), request);
            System.out.println("Request: " + request);
            System.out.println("Result: " + result);

        }
    });
}

-----

@Override
public void onConnected(Bundle bundle) {

    new DataTask (this, actsList).execute();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

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

    private ArrayList<String> contents;
    Context c;

    public DataTask ( Context c, ArrayList<String> contents) {
        this.c = c;
        this.contents = contents;
    }

    @Override
    protected Void doInBackground(Node... nodes) {
        PutDataMapRequest dataMap = PutDataMapRequest.create("/wristaroo/customschedule").setUrgent();
        dataMap.getDataMap().putStringArrayList("schedule", actsList);

        PutDataRequest request = dataMap.asPutDataRequest().setUrgent();
        Log.i("Data", "data");
        DataApi.DataItemResult dataItemResult = Wearable.DataApi
                .putDataItem((googleApiClient), request).await();
        System.out.println("dataItemResult: " + dataItemResult);

        return null;
    }
}
}

WEAR

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

    super.onCreate(savedInstanceState);

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

@Override
public void onConnected(Bundle bundle) {
    Wearable.DataApi.addListener(googleApiClient, this).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            Log.i("myTag", String.valueOf(status));
        }
    });
    System.out.println("onConnected Activated!!!");
}

@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("/wristaroo/customschedule") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                data = dataMap.getStringArrayList("schedule");
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            // DataItem deleted
        }
    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 

Manifest Mobile

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.michaeldvinci.conedmiro.schedaroo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".EditActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".AddActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

Manifest Wear

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.michaeldvinci.conedmiro.schedaroo">

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".byDayActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byChoiceActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byStageActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byTimeActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".byStageActActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

Output when I press the [Send] Button on the Mobile App

03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:47.948 5246-5246/com.michaeldvinci.conedmiro.schedaroo W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1307.8564, y[0]=2230.7813, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=473778, downTime=468682, deviceId=0, source=0x1002 }
03-29 18:33:48.179 5246-5246/com.michaeldvinci.conedmiro.schedaroo I/System.out: Request: PutDataRequest[dataSz=108, numAssets=0, uri=wear:/wristaroo/customschedule, syncDeadline=0]
03-29 18:33:48.179 5246-5246/com.michaeldvinci.conedmiro.schedaroo I/System.out: Result: com.google.android.gms.wearable.internal.zzx$1@1ded1a8

Upvotes: 1

Views: 769

Answers (1)

roberto tom&#225;s
roberto tom&#225;s

Reputation: 4697

This may end up being an incorrect answer but I think the setUrgent() flag might be important:

Please use this code:

    PutDataMapRequest dataMap = PutDataMapRequest.create("/wristaroo/customschedule");
    dataMap.getDataMap().putStringArrayList("schedule", actsList);

    PutDataRequest request = dataMap.asPutDataRequest();
    request.setUrgent();
    Log.i("Data", "data");
    DataApi.DataItemResult dataItemResult = Wearable.DataApi
            .putDataItem((googleApiClient), request).await();

Upvotes: 0

Related Questions