GoogleApiClient onConnected not called on Watch - android

I have read this thread but I still faced similar issue:
GoogleApiClient onConnected never called on Wearable device
I tried to follow exactly how this works:
https://developer.android.com/training/wearables/data-layer/events.html
Here are my codes:
public class Main extends Activity implements DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String PATH = "/phonewatch";
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("phone watch", "On Create!");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
client = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
...
}
#Override
protected void onStart() {
Log.d("phone watch", "On Start!");
super.onStart();
client.connect();
}
#Override
protected void onStop() {
Log.d("phone watch", "On Stop!");
if (client != null && client.isConnected()) {
Wearable.DataApi.removeListener(client, this);
client.disconnect();
}
super.onStop();
}
#Override
public void onConnected(Bundle bundle) {
Log.d("phone watch", "On connected! Add listener.");
Wearable.DataApi.addListener(client, this);
}
#Override
public void onConnectionSuspended(int i) {
Log.d("phone watch", "connection suspended.");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("phone watch", "connection failed.");
}
#Override
public void onDataChanged(final DataEventBuffer dataEventBuffer) {
Log.d("phone watch", "Data changed!");
...
}
I only got this:
07-23 20:07:41.730 24874-24874/virtualgs.phonewatch D/phone watch﹕ On Create!
07-23 20:07:41.772 24874-24874/virtualgs.phonewatch D/phone watch﹕ On Start!
On connected and other log messages were not called. Do I miss anything?

Although your activity implements GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener, you have never registered your activity as a listener to receive those callbacks. When building the api client, you need to call the following two: addConnectionCallbacks(this) and addOnConnectionFailedListener(this):
client = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

Related

make GMS location tracking work in background

My Activity is like this:
public class MainMap extends FragmentActivity implements OnMarkerClickListener, OnMapReadyCallback,
ConnectionCallbacks, OnConnectionFailedListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
protected GoogleApiClient mGoogleApiClient;
protected Location mCurrentLocation;
protected LocationRequest mLocationRequest;
// ...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(NativeLib.get_location_update_interval_in_mil());
mLocationRequest.setFastestInterval(NativeLib.get_location_fastest_update_interval_in_mil());
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
#Override
protected void onStart() {
if (debug_mode) { Log.i(TAG, "onStart");}
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
if (debug_mode) { Log.i(TAG, "onStop");}
super.onStop();
mGoogleApiClient.disconnect();
}
#Override
public void onConnected(Bundle connectionHint) {
if (debug_mode) { Log.i(TAG, "onConnected");}
// permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
request_fine_location_permission();
}
// location
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
#Override
public void onLocationChanged(Location location) {
// CALL either do_background or do_foreground
}
private void do_background() {
// DO WORK
}
private void do_foreground() {
// DO WORK
}
}
The code works just fine in the foreground - what I want to do is to make sure that location is constantly tracked (even when the app is in the background or the phone is in sleep mode) and launch the appropriate function in my Activity.

GoogleApiClient stops to work properly after GPS turned on/off

I'm trying to implement abstract Activity, whose inheritants would be able to get Location data. I'm strictly following this guide, that seems pretty straightforward.
But after all I have a few problems. Google API should pass location data even if GPS is off, getting this from network data or even passive data from other apps. But in my case it works only with GPS enabled.
Another question is what happens when I turn GPS off/on. In my case after that I can't het any data at all.
Here's the class that makes everything done.
public abstract class LocationProviderActivity extends BaseDrawerActivity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private final String TAG = "LOCATION";
GoogleApiClient client;
protected Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (client == null) {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
#Override
protected void onStart() {
client.connect();
Log.i(TAG, "Connected");
super.onStart();
}
#Override
protected void onStop() {
client.disconnect();
Log.i(TAG, "Disconnected");
super.onStop();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//TODO
return;
}
location = LocationServices.FusedLocationApi.getLastLocation(client);
}
#Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Location services connected.");
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.i(TAG, "Location services suspended. Please reconnect.");
}
#Override
public void onLocationChanged(Location location) {
}
}

Google Play Games fails to sign-in at the first attemp, success at the second

I'm experiencing a really silly problem in the Google Play Games API, when a user joins, he needs to click the sign in again to be signed-in.
I have the auto-sign in at startup, so when the user opens the app, it shows the sign-in dialog, choose an email, then closes without any message, after that the user must click on the sign in again to be signed.
So, it's unknown why its happening, the popular problem that people are having is not signing in, but this is failing at the first attemp.
My code:
public class MainActivity extends FragmentActivity implements
View.OnClickListener,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Games.API)
.addScope(Games.SCOPE_GAMES)
.build();
}
#Override
protected void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
#Override
public void onConnected(#Nullable Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, 2);
} catch (IntentSender.SendIntentException e) {
mGoogleApiClient.connect();
} catch (Exception e) {
startActivityForResult(null, 3);
}
}
}
You have to call mGoogleApiClient.connect() in your onActivityResult() method:
#Override
protected void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data);
mGoogleApiClient.connect();
}
As otherwise the result from connectionResult.startResolutionForResult does not update the GoogleApiClient state.

Android Wearable Data Layer won't sync.

I am trying to use Data sync between watch and my phone but I am failing miserably on that.
Mobile side :
package com.off.testcomm;
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setOnClickListener((Button) findViewById(R.id.button));
initGoogleAPIClient();
}
private void setOnClickListener(Button button) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("OFF", "increase counter");
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt("COUNT_COUNT", 7);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
#Override
public void onResult(DataApi.DataItemResult dataItemResult) {
Log.i("OFF", "SaveConfig: " + dataItemResult.getStatus() + ", " + dataItemResult.getDataItem().getUri());
}
});
}
});
}
private void initGoogleAPIClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
#Override
public void onConnected(Bundle connectionHint) {
Log.d("OFF", "onConnected: " + connectionHint);
// Now you can use the Data Layer API
}
#Override
public void onConnectionSuspended(int cause) {
Log.d("OFF", "onConnectionSuspended: " + cause);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("OFF", "onConnectionFailed: " + result);
}
})
// Request access only to the Wearable API
.addApi(Wearable.API)
.build();
}
#Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
When I click the button I get :
SaveConfig: Status{statusCode=SUCCESS, resolution=null}, wear://ad9cb4db-c697-4777-9088-0b29b8584043/count
So I assume it is sent.
On Wear :
package com.off.testcomm;
public class MyWatchFace extends CanvasWatchFaceService {
...
private class Engine extends CanvasWatchFaceService.Engine implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,DataApi.DataListener, MessageApi.MessageListener {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
#Override
public void onVisibilityChanged(boolean visible) {
if (visible) {
Log.i("OFF","is visible");
registerReceiver();
mGoogleApiClient.connect();
} ...
#Override
public void onConnected(Bundle bundle) {
Log.i("OFF", "OnConnected ");
Wearable.DataApi.addListener(mGoogleApiClient, this);
}
#Override
public void onConnectionSuspended(int i) {
Log.i("OFF","onConnectionSuspended");
}
#Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
Log.i("OFF","OnDataChanged");
}
#Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.i("OFF","onMessageReceived");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i("OFF","onConnectionFailed");
}
...
On wear last log I am getting is : "OnConnected" . When I click button on mobile nothing seems to be synced on wear.
As you can see both classes are in the same packages.
As for manifests both have :
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
any ideas?
thanks
w.
Since you are adding the same data each time the button is clicked, there is no "change" in the exiting data, hence you don't receive an onDataChanged() callback; Android Wear framework looks at the content of your data and decided if it is a new one or not and if it doesn't see any change in the content (assuming that its path doesn't change either), it won't considers that a new one. Add a timestamp to your data and that should make it a new data each time, and should trigger the callback:
putDataMapReq.getDataMap().putLong("timestamp", System.currentTimeMillis());

GoogleApiClient.ConnectionCallbacks methods not being called after connecting to the GoogleApiClient

I've got some code which is connecting to the GoogleApiClient but onConnected is not being called.
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGoogleApiClient();
}
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();
mApiClient.connect(); // On completion onConnected() will be called
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
protected void onDestroy() {
super.onDestroy();
mApiClient.disconnect();
}
#Override
public void onConnectionFailed(com.google.android.gms.common.ConnectionResult connectionResult) {
}
None of the four #Override methods are being called, why is that?
You need to call addConnectionCallbacks() and addOnConnectionFailedListener() on your GoogleApiClient.Builder:
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

Categories

Resources