Reputation: 1
I'm working on a React Native app that uses Google Fitness API to fetch step count, distance, and calorie data. I'm using the react-native-google-fit and @supersami/rn-foreground-service packages. The data fetching works perfectly on my main Google account but fails on other accounts.
fetchGoogleFit
function):import ...
import ReactNativeForegroundService from '@supersami/rn-foreground-service';
import googleFit from 'react-native-google-fit';
export default function MapActivity() {
// Google Fit data
const [steps, setSteps] = useState(0);
const [distance, setDistance] = useState(0);
const [calories, setCalories] = useState(0);
const [startTime, setStartTime] = useState(new Date().toISOString());
const toggleStarted = () => setToggleActivity(!toggleActivity)
useEffect(() => { // start task everytime activity is toggled
if (toggleActivity) {
startForegroundTask();
return () => stopForegroundTask();
}
}, [toggleActivity]);
const startForegroundTask = () => {
console.log('Added tasks');
ReactNativeForegroundService.add_task(() => fetchGoogleFit(), {
delay: 1000,
onLoop: true,
taskId: 'fetchGoogleFit',
onError: e => console.log(e),
});
};
const stopForegroundTask = () => {
console.log('Removed tasks');
ReactNativeForegroundService.remove_task('fetchGoogleFit');
};
const fetchGoogleFit = async () => {
try {
let auth = !googleFit.isAuthorized ? await googleFit.authorize() : true;
console.log('auth:', auth); // log: true
if (auth) {
const optSteps = {
startDate: startTime,
endDate: new Date().toISOString(),
bucketUnit: 'DAY',
bucketInterval: 1,
};
const currSteps = await googleFit.getDailyStepCountSamples(optSteps);
for (let i = 0; i < currSteps.length; i++) {
if (
currSteps[i].source === 'com.google.android.gms:estimated_steps' &&
currSteps[i].steps.length > 0
) {
setSteps(currSteps[i].steps[0].value);
break;
}
}
const optCalories = {
startDate: startTime,
endDate: new Date().toISOString(),
basalCalculation: true,
bucketUnit: 'DAY',
bucketInterval: 1,
};
const currCalories = await googleFit.getDailyCalorieSamples(
optCalories,
);
if (currCalories.length > 0) {
setCalories(parseFloat(currCalories[0].calorie.toFixed(1)));
}
const optDistance = {...optSteps};
const currDistance = await googleFit.getDailyDistanceSamples(
optDistance,
);
if (currDistance.length > 0) {
setDistance(parseFloat(currDistance[0].distance.toFixed(1)));
}
} else {
console.log('Google Fit authorization failed!');
}
} catch (error) {
console.error(error);
}
};
return (
<View>
...
</View>
);
}
export default MapActivity;
LOG [{"rawSteps": [], "source": "com.google.android.gms:merge_step_deltas", "steps": []}, {"rawSteps": [], "source": "com.xiaomi.hm.health", "steps": []}, {
"rawSteps": [[Object]], "source": "com.google.android.gms:estimated_steps", "steps": [[Object]]}]
// The "rawSteps": [[Object]] gives:
LOG {"rawSteps": [{"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated", "dataTypeName": "com.google.step_count.delta", "deviceManufacturer": "Xiaomi", "deviceModel": "M2101K6P", "deviceType": "phone", "deviceUid": "d396e761", "endDate": 1719125694043, "originDataSourceId": "raw:com.google.step_count.cumulative:Xiaomi:M2101K6P:d396e761:pedometer Non-wakeup", "startDate": 1719125651487, "steps": 10}], "source": "com.google.android.gms:estimated_steps", "steps": [{"date": "2024-06-23", "value": 10}]}
LOG [{"day": "Sun", "distance": 2.0318520069122314, "endDate": "2024-06-23T06:51:03.648Z", "startDate": "2024-06-23T06:51:00.048Z"}]
LOG [{"calorie": 0.8895832896232605, "day": "Sun", "endDate": "2024-06-23T06:51:14.274Z", "startDate": "2024-06-23T06:50:25.843Z", "wasManuallyEntered": false}]
LOG [{"rawSteps": [], "source": "com.google.android.gms:estimated_steps", "steps": []}, {"rawSteps": [], "source": "com.xiaomi.hm.health", "steps": []}, {"rawSteps": [], "source": "com.google.android.gms:merge_step_deltas", "steps": []}]
LOG [] // distance
LOG [] // calories
I have provided the necessary permissions while using all accounts. OAuth Consent Screen OAuth Consent Screen
The issue persists even when I try using the same Google account ([email protected]) on other devices. Even the account ([email protected]) which was used to setup the API on Google Cloud Console does not work. It also does not work even when using the primarily used Google account for that Android device, or any other account.
Works on - Redmi Note 10 Pro (my main device, running Android 12)
Does not work on - Any other Android device (Android version 12, 13, 14)
It has been tested only on Android, which is my only concern at the moment.
Upvotes: 0
Views: 169