Search code examples
javaarraylistandroid-wear-data-apidataitem

How do I get past mGoogleApiClient.connect() when it doesn't seem to actually connect?


I'm trying to send an ArrayList to my Android Wear device from the mobile device.

Unfortunately I'm not getting past the mGoogleApiClient.connect() as per my sop statements.

What I've done is set up a DataLayer item and used the onDataChanged() to monitor the code and the DataItem being sent with it, but unfortunately it seems like nothing is getting across.

Console output is at the bottom of the post, if that helps pinpoint anything.

MAIN ACTIVITY - MOBILE

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

Set<String> tasksSet;
ArrayList<String> actsList;
ArrayAdapter adapter;
ListView list;
GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    mGoogleApiClient.connect();

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

    xxxxxxx
    other populating code
    xxxxxxx

    Button btnSend = (Button) findViewById(R.id.sendButton);
    btnSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendCustomSchedule(actsList);
            System.out.println("Button Pressed: Send");
        }
    });
}

private void populateListView() {
    list = (ListView) findViewById(R.id.listViewAdd);
    list.setAdapter(adapter);
}

public MainActivity getActivity() {
    return this;
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

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

}

public void sendCustomSchedule(ArrayList<String> customSchedule) {
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/customSched");

    putDataMapRequest.getDataMap().putStringArrayList("customSched", customSchedule);

    PutDataRequest request = putDataMapRequest.asPutDataRequest().setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if(!dataItemResult.getStatus().isSuccess()) {
                        System.out.println("Apparent Failure!");
                    } else {

                        System.out.println("Apparent Success!");
                    }
                }
            });
    System.out.println("When in doubt...");
    }
}

MAIN ACTIVITY - WEAR

 public class byTimeActivity extends Activity  implements DataApi.DataListener,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener   {
ArrayList<String> choicesList;
ArrayAdapter adapter;
ListView list;
ArrayList<String> data;
String dayExtra;
String howExtra;
String schedExtra;
GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


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

    mGoogleApiClient.connect();

    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

    dayExtra = getIntent().getExtras().getString("dayExtra");
    schedExtra = getIntent().getExtras().getString("schedExtra");
    howExtra = getIntent().getExtras().getString("howExtra");

    System.out.println("dayExtra: " + dayExtra);
    System.out.println("schedExtra: " + schedExtra);
    System.out.println("howExtra: " + howExtra);

    switch(dayExtra) {
        case "Thursday":
            if(schedExtra.equals("Full Schedule")) {
                setStage(thTime);
            }
            if(schedExtra.equals("Custom Schedule")) {
                adapter = new ArrayAdapter<>(this, R.layout.da_item, data);
            }
            break;
        case "Friday":
            setStage(frTime);
            break;
        case "Saturday":
            setStage(saTime);
            break;
        case "Sunday":
            setStage(suTime);
            break;
    }
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            list = (ListView) findViewById(R.id.mainChoiceList);
            list.setAdapter(adapter);
        }
    });
}

private void setStage(String[] dayStage) {
    choicesList = new ArrayList<>(Arrays.asList(dayStage));
    adapter = new ArrayAdapter<>(this, R.layout.da_item, choicesList);
    adapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
    super.onResume();
    mGoogleApiClient.connect();
}

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

@Override
protected void onPause() {
    super.onPause();
    Wearable.DataApi.removeListener(mGoogleApiClient, this);
    mGoogleApiClient.disconnect();
}

@Override
public void onConnectionSuspended(int i) {

}

@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("/customSched") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                data = dataMap.getStringArrayList("customSched");

                System.out.println("Data: " + data);
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            //dataItem Deleted
        }
    }
}

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

}
}

CONSOLE OUTPUT - MOBILE

W/System: ClassLoader referenced unknown path: /data/app/com.conedmiro.schedaroo-2/lib/arm
I/GMPM: App measurement is starting up, version: 8487
I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
E/GMPM: Scheduler not set. Not logging error/warn.
E/GMPM: Uploading is not possible. App measurement disabled
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/Adreno200-EGL: <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.059_msm8960_JB_2.5.5_CL3896081_release_AU (CL3896081)
        Build Date: 06/25/13 Tue
        Local Branch:
        Remote Branch: quic/jb_2.5.5
        Local Patches: NONE
        Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.059 +  NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
I/System.out: When in doubt...
I/System.out: Button Pressed: Send
I/System.out: Apparent Success!

CONSOLE OUTPUT - WEAR

W/art: Suspending all threads took: 17.121ms
I/art: Background sticky concurrent mark sweep GC freed 1601(90KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 508KB/663KB, paused 20.603ms total 53.990ms
W/art: Suspending all threads took: 5.855ms
I/art: Background partial concurrent mark sweep GC freed 168(48KB) AllocSpace objects, 0(0B) LOS objects, 45% free, 611KB/1123KB, paused 8.179ms total 61.166ms
I/System.out: mGoogleApiClient connected!
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8487000 but found 8299574
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
                                                                                       
      [ 04-03 23:16:13.428 15148:15148 D/         ]
      HostConnection::get() New Host Connection established 0xb3edca60, tid 15148
D/Atlas: Validating map...
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f625e0, error=EGL_SUCCESS
I/System.out: mGoogleApiClient disconnected! //dis/connecting in MainActivity, before switching activities
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f628a0, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f62700, error=EGL_SUCCESS
I/System.out: schedExtra: Custom Schedule
I/System.out: dayExtra: Thursday
I/System.out: howExtra: By Time
I/System.out: dayExtra: Thursday
I/System.out: schedExtra: Custom Schedule
I/System.out: howExtra: By Time
I/System.out: mGoogleApiClient connected!
D/AndroidRuntime: Shutting down VM                                                                                       
                                                                                       
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.conedmiro.schedaroo, PID: 15148
      java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8487000 but found 8299574

Solution

  • WearableListenerService is required REGARDLESS

    only briefly mentioned in the documentation [which should definitely be updated]