You first need access to GPS, so you should add the permission to your manifest.xml
file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fitness.sync">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
....
And in your code you should pass the permissions to the sign-in account as follows:
public void signIn(){
FitnessOptions fitnessOptions = FitnessOptions.builder()
// add your read & write permissions here
.addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_WRITE)
.addDataType(DataType.TYPE_LOCATION_TRACK, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_LOCATION_TRACK, FitnessOptions.ACCESS_WRITE)
...
.build();
GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(this, fitnessOptions);
if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
GoogleSignIn.requestPermissions(
this, // your activity
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, // e.g. 1
account,
fitnessOptions);
}
}