如何解决react-native-google-fit 不会返回 Google Fit 步骤
我正在使用 react-native-google-fit 从 Google Fit 获取步数数据。它工作正常一个多月,但突然停止工作。它只是返回当前日期没有步数。
fitness api 未超过配额。
我们需要为 fitness api 做任何计费吗?
下面是代码
getAndroidHealthData = async (date,pushFlag) => {
var NowPlusOneDay = moment(date).add(1,'days');
var NowPlusOneDayStr = NowPlusOneDay.format('YYYY-MM-DD');
const callback = (async (error,isAvailable) => {
if (isAvailable) {
let options = {
scopes: [
Scopes.fitness_ACTIVITY_READ_WRITE,Scopes.fitness_ACTIVITY_READ_WRITE,Scopes.fitness_LOCATION_READ,Scopes.fitness_BODY_READ,Scopes.fitness_BODY_READ_WRITE
],}
GoogleFit.authorize(options)
.then(authResult => {
if (authResult.success) {
let steps = 0
let dailydistance = 0
let totalValue = 0
let totalCalories = 0
let totalWalkTime = 0
let optionsAndroid = {
startDate: moment(date).startOf('day').valueOf(),endDate: moment(date).endOf('day').valueOf(),};
GoogleFit.getActivitySamples(optionsAndroid,async (err,res) => {
let optionsAndroidCalories = {
startDate: moment(date).startOf('day').toISOString(),endDate: (moment(date).isSame(moment().format('YYYY-MM-DD'))) ? moment().toISOString() :
moment(date).endOf('day').toISOString(),};
GoogleFit.getDailyStepCountSamples(optionsAndroidCalories)
.then(async (resultssteps) => {
resultssteps.map((item,index) => {
if (item.source == "com.google.android.gms:estimated_steps") {
totalValue = item.steps.length ? item.steps[0].value : 0
return null
}
})
if (res && res.length) {
await res.forEach(function (entry,index) {
entry['distance'] = (res[index].distance) ? res[index].distance / 1000 : 0
let walkTime = moment(entry.end) - moment(entry.start)
entry['walkTime'] = moment.duration(walkTime).asMinutes()
totalWalkTime += entry['walkTime']
entry['value'] = entry.quantity ? entry.quantity : 0
// totalValue += entry.quantity ? entry.quantity : 0
dailydistance += entry['distance']
entry['calories'] = (entry.calories) ? entry.calories / 1000 : 0
totalCalories += entry['calories']
entry['start'] = moment(entry.start).toISOString()
entry['end'] = moment(entry.end).toISOString()
});
let healthData = {}
let device = Platform.OS
healthData['vital'] = 'STEPS'
healthData['date'] = moment().format('YYYY-MM-DD')
healthData['unit'] = ''
healthData['device'] = device.toupperCase()
healthData['vitalInfo'] = res
healthData['caloriesBurned'] = totalCalories.toFixed(2)
healthData['totalValue'] = totalValue
healthData['distanceCovered'] = dailydistance
if (moment(date).isSame(moment().format('YYYY-MM-DD'))) {
this.setState({
totalWalkTime: totalWalkTime.toFixed(2),totalWalkSteps: totalValue,totalCalories: totalCalories.toFixed(1),dailydistance: dailydistance.toFixed(2)
})
}
return this.startCallBackFunctionAndroid(NowPlusOneDayStr,pushFlag)
} else {
return this.startCallBackFunctionAndroid(NowPlusOneDayStr,pushFlag)
}
})
.catch((err) => {
if (moment(date).isSame(moment().format('YYYY-MM-DD'))) {
this.setState({
totalWalkTime: totalWalkTime.toFixed(2),dailydistance: dailydistance.toFixed(2)
})
}
return this.startCallBackFunctionAndroid(NowPlusOneDayStr,pushFlag)
})
// }
});
} else {
this.setState({ stepsLoad: true })
console.log("AUTH_DENIED",authResult.message);
}
}).catch((error) => {
this.setState({ stepsLoad: true })
console.log("AUTH_ERROR");
})
} else {
this.setState({ indicator1: false,alertMessage: I18n.t('needtoInstall'),alertShow: true,alertbtnTwo: I18n.t('playStore'),alertKey: 3,alertMethod: 1 }) // indicator: false,}
});
GoogleFit.isAvailable(callback)
}
`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。