大家好,我正在尝试制作计步器应用程序。我安装了这些软件包。
"react-native-pedometer": "^0.0.7",
"react-native-pedometer-ios-android"//then i deleted it
"@t2tx/react-native-universal-pedometer" // and installed it
以及所有示例代码
import React, {Component} from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import Pedometer, {
PedometerInterface,
} from '@t2tx/react-native-universal-pedometer';
// interface PedoResult : PedometerInterface | string | null;
export default class PCount extends Component {
constructor(props) {
super(props);
this.state = {
data: {},
error: 'none',
};
}
check = () => {
Pedometer.isStepCountingAvailable((error, result) => {
console.log(error, result);
this.setState({
data: {result: result + ''},
error,
});
});
};
start = () => {
Pedometer.startPedometerUpdatesFromDate(
new Date().setHours(0, 0, 0, 0),
(data) => {
this.setState({
data: data || {},
error: '-',
});
},
);
};
stop = () => {
Pedometer.stopPedometerUpdates();
this.setState({
data: {state: 'stoped'},
error: '-',
});
};
load = () => {
const start = new Date();
start.setHours(0, 0, 0, 0);
const end = new Date();
Pedometer.queryPedometerDataBetweenDates(
start.getTime(),
end.getTime(),
(error, data: PedometerInterface | null) => {
console.log(error);
console.log(data);
this.setState({
data: data || {},
error,
});
},
);
};
render() {
return (
<View style={styles.container}>
<View style={styles.row}>
<View style={styles.rowItem}>
<Button title=" ? " onPress={this.check} />
</View>
<View style={styles.rowItem}>
<Button title=" ▶︎ " onPress={this.start} />
</View>
<View style={styles.rowItem}>
<Button title=" ■ " onPress={this.stop} />
</View>
<View style={styles.rowItem}>
<Button title=" ◎ " onPress={this.load} />
</View>
</View>
<View style={[styles.row, styles.rowItem]}>
<Text>ERROR: {this.state.error}</Text>
</View>
<View style={styles.rowItem}>
<Text>DATA:</Text>
</View>
{Object.keys(this.state.data).map((key) => {
return (
<View key={key} style={styles.detailItem}>
<Text>
{key}: {this.state.data[key]}
</Text>
</View>
);
})}
</View>
);
}
}
这也不是我的代码,我从 github repo https://github.com/t2tx/pedometer_example/blob/master/Components/Debug/PedometerComponent.tsx复制了它
这是行不通的。有没有其他方法可以实现这一点。谢谢