Reputation: 710
I'm trying to mess around with the Google Fit API. I have a WearOS emulator connected to my actual device (Samsung Galaxy). When I run the application, I am pretty sure I should be getting a screen to pop up asking for permissions, but I am getting no such screen. I went through the process of getting the token in the Google API console.
public class GoalsListActivity extends WearableActivity {
public static final String TAG = "BasicRecordingApi";
private static final int REQUEST_OAUTH_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.goals_screen_layout);
accessGoogleFit();
// Enables Always-on
setAmbientEnabled();
}
/**
* Check if the user has permissions to talk to Fitness APIs; otherwise authetnciate the user and request required permissions
*/
private void accessGoogleFit() {
FitnessOptions fitnessOptions = getFitnessSignInOptions();
if (!hasOAuthPermission()) {
GoogleSignIn.requestPermissions(this, REQUEST_OAUTH_REQUEST_CODE, GoogleSignIn.getLastSignedInAccount(this), fitnessOptions);
} else {
subscribeToRecordingApi();
}
}
/**
* Create a FitnessOptions instance, declaring the Fit API data types and access required by the app
*
* @return FitnessOptions object
*/
private FitnessOptions getFitnessSignInOptions() {
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
return fitnessOptions;
}
/**
* Check if user's account has OAuth permission to Fit API
*
* @return true if user has OAuth permission
*/
private boolean hasOAuthPermission() {
FitnessOptions fitnessOptions = getFitnessSignInOptions();
//Check if the user has previously granted the necessary data access, and if not, inititate the authroization flow
boolean hasAuthO = GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions);
return hasAuthO;
}
//If the user does not have necessary data access, inititate the authorization flow
private void requestOAuthPermission() {
FitnessOptions fitnessOptions = getFitnessSignInOptions();
GoogleSignIn.requestPermissions(
this, //our activity
REQUEST_OAUTH_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);
}
/**
* SUBSCRIBE TO FITNESS DATA
* To record data from the sensors, we need to first create a subscription.
* To create a subscription, invoke the Recording API. As soon as the subscription is active, fitness data will start recording
*/
private void subscribeToRecordingApi() {
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Successfully subscribed!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "There was a problem subscribing");
}
});
}
/**
* Get a list of all active subscriptions
*/
private void getAllActiveSubscriptions() {
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.listSubscriptions(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<List<Subscription>>() {
@Override
public void onSuccess(List<Subscription> subscriptions) {
for (Subscription sc : subscriptions) {
DataType dt = sc.getDataType();
Log.i(TAG, "Active subscription for data type: " + dt.getName());
}
}
});
}
/**
* UNSUBSCRIBE - STOP COLLECTING SENSOR DATA IN THE APP
* Cancels the ACTIVITY_SAMPLE subscription on that {@link DataType}
*/
public void stopRecording() {
final String dataTypeStr = DataType.TYPE_ACTIVITY_SAMPLES.toString();
Log.i(TAG, "Unsubscribing from data type: " + dataTypeStr);
//Invoke the Recording API to unsubscribe from the data type and specify a callback that will check the result
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.unsubscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i("TAG", "Successfully unsubscribed for data type: " + dataTypeStr);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//The subscription was not removed
Log.i(TAG, "Failed to unsubscribe for data type: " + dataTypeStr);
}
});
}
}
The code basically came from enter link description here, which is Google's own example code. I have the API key. For some reason I just can't get it to work. Thank you.
Upvotes: 1
Views: 500
Reputation: 602
Can you replace all your
GoogleSignIn.getLastSignedInAccount(this)
with
GoogleSignIn.getAccountForExtension(this, fitnessOptions);
Upvotes: 1